Latest web development tutorials

Android Hello World Examples

Let's start the real Android-based programming framework. Before you begin using the Android SDK to write the first example, make sure that you have followed Android - environment to build tutorial complete Android development environment to build your presentation. At the same time, I assume you have some knowledge of the Eclipse IDE.

Now let's write a simple Android application that can print out "Hello World".

Creating Android Applications

The first step is to create a simple Android application via Eclipse IDE. Follow option File -> New -> Project, the final choice Android New Application Wizard from the list. Now, use the following window wizard application named HelloWorld:

image

Next, follow the instructions provided to keep all the default input until the last step. Once the project is successful, the project will see the following interface -

image


Android application profiler

Before you run the application, you need to know some of the Android project file directory and file -

Hello World example

No. Folders, files and notes
1 src: contains all the project .java source file, by default, it includes a MainActivity.java source file corresponding activity class, when the icon to launch the application through the application, it will run.
2 gen: This contains generated by the compiler .R file references all project resources. The file can not be modified.
3 bin: This folder contains files Android .apk package built by the APT, as well as all the other things to run Android applications require.
4 res / drawable-hdpi: this directory contains all of the design required for high-density screen drawable objects.
5 res / layout: This directory is because the definition of the user interface file.
6 res / values: This directory contains a variety of XML file contains a list of resources, such as the definition of strings and colors.
7 AndroidManifest.xml: This is the application's manifest file that describes the basic features of the application, the definition of its various components.

The following sections give an overview of some important application files.


The main active file

Main activity code MainActivity.java Java file. This is the actual application file will be converted to Dalvik executable and run. The following is the default code for the Hello World application deployment wizard generated -

package com.example.helloworld;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;

public class MainActivity extends Activity {

   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.activity_main, menu);
      return true;
   }
}

Here, R.layout.activity_main quoted from under res / layout directory activity_main.xml file. onCreate () is one of the loaded method is called after numerous activities.


Manifest file

No matter what you have developed components as the application part, you need to declare all the components in the manifest.xml file in the application root directory of the project. This file is the interface of the Android operating system and your application between, so if you do not declare component in this file, the operating system will not be recognized. For example, a default manifest file looks like this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.example.helloworld"
   android:versionCode="1"
   android:versionName="1.0" >

   <uses-sdk
      android:minSdkVersion="8"
      android:targetSdkVersion="22" />

   <application
       android:icon="@drawable/ic_launcher"
       android:label="@string/app_name"
       android:theme="@style/AppTheme" >

       <activity
          android:name=".MainActivity"
          android:label="@string/title_activity_main" >

          <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER"/>
          </intent-filter>

       </activity>

   </application>
</manifest>

Here, ... Component is application dependent between label. andnroid: icon attribute indicates located in res / drawable-hdpi following application icon. This application uses the drawable folder named ic_launcher.png pictures.

Tag is used to specify an activity, android: name attribute specifies a subclass of Activity full name. android: label attribute specifies the string used to the activity name. can use Tag to specify multiple activities.

Intent filter action is named android.intent.action.MAIN, indicating that this activity is used as the entry application. Intent filter category is named android.intent.category.LAUNCHER, it indicates that the application by starting the device icon to start.

@tring refers strings.xml (described later). Thus, @ string / app_name refers is defined in strings.xml the app_name, actually "Hello World". Similarly, the application of the other string is also very popular.

Below is a list of documents used in your tags, used to specify different Android application components:

  • Active elements
  • Service Element
  • Broadcast receiver element
  • Content Provider element

Strings Files

strings.xml file res / value folder that contains the application to use all the text. For example, buttons, name tags, the default text, and other similar strings. This text file is responsible for their content. A default strings file looks like this:

<resources>
   <string name="app_name">HelloWorld</string>
   <string name="hello_world">Hello world!</string>
   <string name="menu_settings">Settings</string>
   <string name="title_activity_main">MainActivity</string>
</resources>

R file

gen / com.example.helloworld / R.java Java file is the active document, such as MainActivity.java and resources such as glue between strings.xml. This is an automatically generated file, do not modify the contents R.java file. Here is an example of a R.java file:

/* AUTO-GENERATED FILE.  DO NOT MODIFY.
 *
 * This class was automatically generated by the
 * aapt tool from the resource data it found.  It
 * should not be modified by hand.
 */

package com.example.helloworld;

public final class R {
   public static final class attr {
   }

   public static final class dimen {
      public static final int padding_large=0x7f040002;
      public static final int padding_medium=0x7f040001;
      public static final int padding_small=0x7f040000;
   }

   public static final class drawable {
      public static final int ic_action_search=0x7f020000;
      public static final int ic_launcher=0x7f020001;
   }

   public static final class id {
      public static final int menu_settings=0x7f080000;
   }

   public static final class layout {
      public static final int activity_main=0x7f030000;
   }

   public static final class menu {
      public static final int activity_main=0x7f070000;
   }

   public static final class string {
      public static final int app_name=0x7f050000;
      public static final int hello_world=0x7f050001;
      public static final int menu_settings=0x7f050002;
      public static final int title_activity_main=0x7f050003;
   }

   public static final class style {
      public static final int AppTheme=0x7f060000;
   }
}

Layout file

activity_main.xml is a layout file in the res / layout directory. When an application to build its interface is referenced. You will very frequently modify this file to change the layout of the application. In the "Hello World" application, this file has a default layout, as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent" >

   <TextView
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_centerVertical="true"
      android:padding="@dimen/padding_medium"
      android:text="@string/hello_world"
      tools:context=".MainActivity" />

</RelativeLayout>

This is an example of a simple RelativeLayout, more content will be explained in a separate chapter. Android TextView control is used to build a graphical user interface. It contains many different attributes, such as android: layout_width, android: layout_height the like is used to set the width and height. @string refers to the res / values ​​folder strings.xml file. Thus, @ string / hello_world refers is defined in strings.xml string called hello: "Hello World!".

Run the application

Let's try to run the newly established Hello World! Application. Assumptions used in building environment you have created the AVD. Run the application from Eclipse, open your project in an active file, and click on the toolbar image icon. Eclipse install applications on AVD, and start it. If all goes well, the following window will display the simulator -

image

Congratulations, you've developed the first Android application, in accordance with the rest of the next tutorial step by step, you will become Android developers a cow B.