Saturday, September 7, 2013

Hello World app

After installing necessary tools for Android development, this post provides you step by step guide to create your first app for Android mobile devices.

1. Go to the folder where you put your Android SDK. In the eclipse directory, double-click the eclipse.exe file to launch the eclipse program.

app workspace


2. When the Workspace Laucher opens, enter or select a directory that will be the workspace for your app. Then wait one or two seconds for eclipse to prepare the workspace for you.
3. To create your new app, select the File->New->Android Application Project to open the  New Android Application window. In the Application Name textbox, enter your app name. For this first app, just enter Helloworld for this textbox.

new app creation


Then click Next until you see the Finish button. Click the Finish button to complete the app creation task. The below screen displays after you click the Finish button.

app is created


Run Helloworld app

To run the Helloworld app, firstly you will need to create a emulator. Your app will run on this emulator. The steps below help you to create an emulator to run your app.
1. On the Window menu, select Android Visual Device Manager. On the Android Visual Device Manger window, click New... to create a new emulator.
2. In the AVD Name textbox, enter the name of the emulator. For the Device drop down list select a device for the emulator. See the picture below.

creating emulator


3. After filling the form, click OK to create the emulator.

Now you have a simulator setup. To run the Helloworld app, select Run->Run Configuration... to open the Run Configuration window. On this window, double-click Android Application. In the Project textbox, type Helloworld and under Launch Option, select Launch. From the Launch drop down list select com.example.helloworld.MainActivity. Then click Run to run the app. You will wait until the emulator displays the result as shown in the picture below. This generally takes two to fives minutes for the first run. When the result is shown, don't close the emulator. For the next run, it will be faster.

run app


When the app created, some directories and files are created in your app workspace directory. These directories and files are anatomy of the Android app. The important directories and files are shown below:
1. bin directory contains the binary .class files and .apk file of your app (helloworld.apk). This apk file can be installed on the mobile devices.
2. gen directory contains .R file that references all resources found in your app.
3. res/drawable directory contain drawable resources such as images, shapes defined in xml file format for extra high, high, medium, and low screen resolution.
4. res/layout directory contains xml files that are used as layouts of your app interfaces.
5. res/menu directory contains a xml file that defines menu and its items in your app.
6. res/values contains xml files that define the default screen margins, string values, and styles to be used in your app.
7. src and its sub-directories contains the java source files of your app.
8. AndroidManifest.xml file defines the characteristics of your app such as app version, minimum and target SDK versions, activities, etc.

MainActivity.java file

The MainActivity.java file is the starting point of your app. In this file, MainActivity class extends the Activity class.
package com.example.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
 
}

The Activity class has the following methods that you should know:

1. onCreate method is invoked to create interface for the main activity. The activity_main.xml file is the source for the app interface. Components that will be parts of the interface are define here. You can find the activity_main.xml file in res/layout directory.

activity_main.xml file
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</RelativeLayout>

2. onStart method is immediately invoked after the onCreate method to start the activity. However, the user still can't interact with the app interface.
3. onResume method is invoked after the onStart method to bring the interface to the front so that the user can interact with the app interface.
4. onPause method works when an activity is obscured by another activity. When the activity in the pause state, it does not receive input or execute any code.
5. onStop method is invoked when the user leaves the activity. This method always called after the onPause method is called. When the activity is in stop state, app interface is invisible and the focus is on a separate activity.
5. onRestart method is invoked when the user returns to the previously stopped activity. The methods that are invoked immediately after the onRestart methods are onStart, and onResume.
6. onDestroy method is invoked when the system dispatches a final signal of clearing the app from the memory. In this method, you should close the connection to the database, or long-run processes that can leak your memory.

strings.xml file

In the strings.xml file (in res/values directory) string resources are defined. A string entry has two parts--name and value. For example, the first string entry name is app_name and its values is Helloworld.

<?xml version="1.0" encoding="utf-8"?>

<resources>

    <string name="app_name">Helloworld</string>
    <string name="action_settings">Settings</string>
    <string name="hello_world">Hello world!</string>

</resources>

The strings defined in the strings.xml file can be used in other layouts xml files.
For example, the string hello_world is used as the text of the TextView component in the activity_main.xml file: android:text="@string/hello_world".

No comments:

Post a Comment