Android Simple Application, continued

After setting up the environment in the previous post, let’s create at last a simple application. the application will have a set of 4-5 screens and each screen will have buttons to move from one screen to the other. Also, if this proves to be easy, next step will be to have some shared state between the screens, say an object with 4-5 properties. Each screen will be able to set some property and display some other, or something like that.A screen in Android is called an Activity. Each activity has associated state, also has a certain lifecycle and this lifecycle is managed by something called ActivityManager. This guy decides when to free memory, how to store and reload an Activity and all that nitty-gritty. Also, initially loading an Activity is a demanding task – remember battery times. All that mean one should study carefully  how activity state is managed. For now, let’s avoid all these and try to get our hands dirty in the code.

Home Screen

When we created the project, the home screen layout is named main.xml and resides in  src/res/layout/main.xml. Since our activity is named HelloAndroidActivity, let’s rename main.xml to helloandroid.xml for consistency. It is good that Eclipse manages this change to the Manifest file and everywhere else needed! If you don’t know how to do this, it is secondary clickRefactor / Rename.

Now click on the helloandroid.xml. You’ll meet an editor. Just drag two buttons on the black screen. The two butons will have default names button1 and button2. Ignore, for now, Uncle Bob and Clean Code.  Right click on each, and select Edit Text. It gets a big dialog, where we can create a new string property. Create two new string resources, I finally have two buttons with labels Page 1 and Page 2.

WARNING: The xml files need to have lowercase (pattern [a-z,0-9]+). If you go with camel case or anything else, you’ll break the build process. You ‘ll notice that by having an empty gen directory.

Pages 1 and 2

To create new layouts for the activities Page 1 and Page 2, we need to create the layout XML files. Secondary click on layout directory and select New / Andoid XML file. Select a name (page1.xml and page2.xml). What’s important is to select the type of resource to be layout.

Navigation on home page

Do a secondary click on the buttons (button1 and button 2) on Home Page and select Properties -> onClick and set onClick to page1Click and page2Click respectively.

Now we need to implement these methods in the HelloAndroidActivity.java file.

create the following static field and two methods:

static final String TAG = "HelloAndroid";
	public void page1Click(View arg0) {
		Log.d(TAG, "Page 1 Click fired");
	}

	public void page2Click(View arg0) {
		Log.d(TAG, "Page 2 Click fired");
	}

Now, run the project and set the active Eclipse Perspective to DDMS. You can see log messages in the LogCatwindow and see when the events are fired.

Register Page1 Activity

Before we can enter the Page1 and Page2 activities, we need to declare them as part of the application in the manifest. So, go to the AndroidManifest.xml, go to the Application tab and in the application nodes add a new activity. On the right hand a properties will be visible. I only set Name (Page1Activity) and  Label (@string/lbl_page1)

Now, in order to start the Page1Activity from the onClick event, we need to use the Intent mechanism. Avoiding what this is, the following opens the Page1 screen.

Intent i = new Intent(this, Page1Activity.class);
startActivity(i);

In order for Page2 to work, we need to do the same steps and don’t forget to register the activity in the Manifest. We don’t currently need anything to return to Home page since Android has this nice back button available.

Ok, that’s quite some progress today. Next topic is to share some state between the activities. Keep in mind that these posts are my notes as I am learning the platform. It is not teaching material, lecture notes or whatever, it is not the optimal way to do things.

Advertisements

Posted on August 10, 2011, in Android and tagged . Bookmark the permalink. Leave a comment.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: