First android program

Lets start by developing a simple Android application called "Print name".

The application will have the following features:

  1. A text box to enter name
  2. Button to submit the entered name
  3. The application will read the entered name and print "Hello" followed by the name as output.

( I assume your system is set with the android sdk environment and eclipse with the ADT.
else follow these links Installing the SDK and Eclipse with ADT installation )

Note: I am using Android SDK 2.3.3 with Java 1.6 in my windows Vista.

=============================================================


1. Create an android project in eclipse :




Create project named PrintName



Android 2.3.3 used in this tutorial


PrintNameActivity is the main/default activity of the app




2. Project structure created by the eclipse plugin:

default files and folders created by the ADT plugin


  • src - Contains your source code. You can see the main activity PrintNameActivity.java created here.
  • res - Resouce folder which will contain your layout xmls , string constants xmls, icons, images etc
  • gen - It contains the R.java . DO NOT ALTER THIS FILE. R.java is generated by the android plugin and used by android to link your java source with the xmls defined by you. Will see later in the tutorial.
  • AndroidManifest.xml - Your application's deployment descriptor. Here you define the intentFilters, receivers and more.

3. Edit the UI file main.xml

Open main.xml and then select its Graphical layout (bottom of your eclipse)

graphical layout of main.xml

  • We can drag and drop components from the left menu which contains palette, Text fields, Laoyouts and more.
  • First we will remove the TextView which says "HelloWorld, PrintNameActivity". Right click it and select delete.
  • Now add a new EditText. From left columns select Text Fields > Plain text and drag it onto the top of the layout. Right click and give it an ID like inputText
  • Now add a button . Form Widgets > Button . Place it below the EditText field.  Right click and give it an ID like submitBtn
  • Place a TextView below the Button. Form Widgets >  TextView.  Right click and give it an ID like output. Also, clear the default text present.
  • When ever you give an ID to a UI component, a corresponding value is added in R.java  . You are not suppose to alter R.java . We only use R.java in the activity class to get a link on the UI components, constants etc. 

The final main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/inputText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >
    EditText>

    <Button
        android:id="@+id/submitBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="278dp"
        android:layout_height="wrap_content" />

LinearLayout>



4. Edit PrintNameActivity.java





 1

 2

 3

 4

 5

 6

 7

 8

 9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52
package com.myapp.printname;



import android.app.Activity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.TextView;



public class PrintNameActivity extends Activity

{

 

 // We create a final static tag for helping us in logging

 private static final String tag = "PrintName";

 

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState)

    {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        // Get the handle on the UI components 

        // Notice how we use the static values from R.java to get a link to the UI components 

        final EditText inputText = (EditText) findViewById(R.id.inputText);

        final TextView outputResult = (TextView) findViewById(R.id.output);

        final Button button = (Button) findViewById(R.id.submitBtn);

       

        // We need a know when a user clicks the button so attach a listener

        button.setOnClickListener(new Button.OnClickListener()

        {

         public void onClick(View v)

         {

          try

          {

           

           // Get the name

           String name = inputText.getText().toString();

           Log.i(tag,"Named entered is :" + name);

           

           //Set the name to the output text field

           outputResult.setText("Hello "+name);

          }

          catch (Exception e)

          {

     Log.e(tag, "Error in processing name");

    }

         }

        });

    }

}






The comments are self explanatory.


5. AndroidManifest.xml

Nothing much to edit here as we just made a simple one activity application



 1

 2

 3

 4

 5

 6

 7

 8

 9

10

11

12

13

14

15

16

17

18

19

20

21

22

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

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.myapp.printname"

    android:versionCode="1"

    android:versionName="1.0" >



    <uses-sdk android:minSdkVersion="10" />



    <application

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name" >

        <activity

            android:name=".PrintNameActivity"

            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>




  •  - It means the min version of android needed for this app
  • Note that our activity is defined in AndroidManifest.xml with the name ".PrintNameActivity" (Notice the period before the name. It is required in android)
  • android:label="@string/app_name" - @string means the value will be substituted. The application name is fetched from strings.xml.
  • One intent-filter defined. 
  • android.intent.action.MAIN means this is the entry point to the application. Like main() in a Java program.
  • android.intent.category.LAUNCHER means that the application will be set to the home launcher screen i.e. added to the list of apps in the phone/emulator


6. Start the emulator and wait for it to load the home screen. Then run the application.

App running in the emulator



No comments:

Post a Comment