Sunday 29 September 2013

Android Hello World

This is the post for creating your first Android Hello World application.

Open eclipse, Right click to the project explorer window, select New-->>Others, the new wizard will be displayed like below



In new wizard select Android and expand the android option and select Android Application Project and then click Next button. The next screen will look like the below image



Application Name : Enter your application name. After entering your application name, the Project Name and the Package name will be inserted.

If you are entering Application name as "HelloWorld", then the project name will be same as your application and the package name will be displayed as "com.example.helloworld". You can change the Project name and the Package name as you like if you not go with the default values in the Project and Package name.

If you are installed latest android SDK, then the Minimum Required SDK, Target SDK, Compile With and Theme options are set by default.

Minimum Required SDK is the lowest version of Android that your app support.

Target SDK  Indicates the highest version of Android with which you have tested with your application.

Compile With is the platform version against which you will compile your app. By default, this is set to the latest Android version in your SDK.

Theme specifies the Android UI style to apply for your app.

Click Next button in wizard. The Project configure screen will be displayed.



In this screen, The check box "Create custom launcher icon" is selected. It is used to create the launcher icon of your application. You can choose your custom launcher icon for your app in next screen.

Create Activity check box is selected, used to create a Main activity of your application.

Click Next button in wizard. The Configure Launcher Icon screen will be displayed.



In this screen, you can choose your launcher icon by clicking the browse button. If no image will be selected, by default the android image will become the launcher icon for your application.

Click Next button in wizard, The Create Activity screen will be displayed.



In this screen you can choose your activity.

Blank Activity : Create a blank activity with contain header.
Full screen Activity : Create an activity without header.
Master/Detail Activity : Create an activity master on left and details on right of the screen. This activity used for tablets.

Click next button, The Blank Activity screen will be displayed.



In this screen, you should have to enter the followings.

Activity Name : Name of your Activity, By default it is MainActivity.
Layout Name : Name of your layout, By default it is activity_main. Layout name will be in lower case.
Navigation Type : None.

Click Finish button to close the wizard. After clicking the finish button, a new android project will be created in eclipse and below is the project structure of your application.



All your source code will go under the src directory. All layout will go under the layout folder. All images used in your app will go under drawable-hdpi for high density devices, drawable-ldpi for low density devices, drawable-mdpi for medium density devices, drawable-xhdpi for extra high density devices and drawable-xxhdpi for larger density devices.

All your string values will go under the strings.xml file under values folder.

For hello world application you dont need to code anything. Once you created an android project successfully, the hello world application is created by default.

Below is the code for MainActivity.java. It will coded by default.

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;
    }
    
}

Below is the code for activity_main.xml. It will coded by default.

<LinearLayout 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:orientation="horizontal">

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

</LinearLayout>

The text  android:text="@string/hello_world" will be refered from strings.xml file that are present under values directory.

Below is the code for strings.xml file. It will also coded by default.

<?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>

For each application an Android manifest xml file will be created (AndroidManifest.xml). Below is the code for AndroidManifest.xml file

<?xml version="1.0" encoding="utf-8"?>
<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="18" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.helloworld.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>


Run the Android Application:

To run your android application, click the run configuration button in eclipse. the run configuration screen will be displayed like below.



Browse your project main program . Select "Launch Default Activity" radio button. Click Target tab in run configuration screen



In this screen, select the target device that you are going to run your application. Click Common tab in run configuration screen.



In this screen select the check box Run. Click Apply and Run button in Run configuration screen. your application will be launched in selected device. If device is an emulator, then your application will be launched in emulator.

The below is the result of Android Hello World Application.




No comments:

Post a Comment