Google Android Tutorial

From the toolbox, we learn that we are having controls like button,check,radio,spinner,edittext(textbox) and  textview (label) etc. There is also a combo  to choose  layout, in the canvas screen.  I am choosing 'absolute layout'. I simply drag and drop a button and an editview on the canvas.(Drag and drop, do not click and drop! It won't work).

You will notice a number of tabs in toolbox. Select 'properties' tab.. After clicking the button on the canvas, give the id property of button as @id/button1. Similarly, for editview as @id/text1. Also, button2 and text2 After this, just click the 'generate' button at the bottom of  the blank window. We get the following XML file(main.xml) automatically generated.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:padding="10px">
	<EditText android:id="@+id/text1" android:layout_width="fill_parent"
		android:layout_height="wrap_content" />
	<Button android:id="@+id/button1" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_below="@id/text1"
		android:layout_alignParentRight="true" android:layout_marginLeft="10px"
		android:text="click" />
	<EditText android:id="@+id/text2" android:layout_width="fill_parent"
		android:layout_height="wrap_content" android:layout_below="@id/button1" />
	<Button android:id="@+id/button2" android:layout_width="wrap_content"
		android:layout_height="wrap_content" android:layout_below="@id/text2"
		android:layout_alignParentRight="true" android:layout_marginLeft="10px"
		android:text="click" />
</RelativeLayout>

We can now create our java source file. The code refers to main.xml for id of the controls.
(d:\android\mydemos\ex1\demo.java).This is our work folder.

package mypack.mydemos;


import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.*;


public class demo extends Activity  
{

Button     button1,button2;
EditText   text1,text2;

    @Override

    public void onCreate(Bundle icicle)
    {
       	super.onCreate(icicle);
       	setContentView(R.layout.main);
	text1=  (EditText) findViewById(R.id.text1);
	button1 = (Button) findViewById(R.id.button1);

	text2=  (EditText) findViewById(R.id.text2);
	button2 = (Button) findViewById(R.id.button2);
	button1.setOnClickListener(new clicker());
 	button2.setOnClickListener(new clicker());
   }

 //-----------------------------------

 class  clicker implements Button.OnClickListener
  {               
  public void onClick(View v)
     {
     if(v==button1){text1.setText("welcome"); }
     if(v==button2){text2.setText("hello");   }     
     }

  }

}

The Android documentation and sample programs use anonymous inner class. I think it is quite unnecessary and is very tedious to follow. Instead, I have used user-defined 'clicker'. This makes the code cleaner and more readable. This is just like any Swing program. In Android Terminology, an Activity is like a frame in swing (a screen). Just like ActionListener, here also we have, OnClickListener. I am not going into detailed theory now. I just  want to show how to develop and run a program first. Theory will follow later.

We have to copy this file(demo.java) to D:\android\tools\demo\src\mypack\mydemos

Go to d:\android\tools\demo

Give path= c:\winNT\system32;c:\jdk1.5\bin;e:\ant1.6\bin

(carefully note that this will not work with jdk1.4.2. It requires jdk1.5).

Secondly, how about the reference to Ant? Ant is a famous build tool from Apache Software foundation. Android requires the latest version of Ant for Windows(ie) Ant1.6. Do I have to know how to write the Ant's build.xml file? NO. It is automatically created by the command.

So, I downloaded ant1.6 from the Apache website. It is a compact zip file. I have unzipped it and placed it as E:\ant1.6). Now use the command 'ant'

d:\android\tools\demo>ant

We will get a series of messages. If we had done the previous steps correctly, we will get the message 'BUILD SUCCESSFUL".  Otherwise, we will get error messages, with line numbers where the errors occurred. We can correct them and build again.

The build process would have created a zip file named 'demo.apk' in demo\bin folder. All that remains now is to run the program in the emulator. As you remember, we have already started the emulator and it is running.

Now copy d:\android\tools\demo\bin\demo.apk  to d:\android\tools. After copying, give the command as:

...\tools>adb   install    demo.apk

After giving this command. go to the emulator window. You will find a checkpattern displayed for a while. Then an additional button appears in the screen with caption 'demo'. Our program has been installed. We can test it by clicking on this 'demo'button.

 


You can execute the program 'demo' now. Two textboxes and two buttons will appear. Click on button1. 'welcome' will appear in text1. Click on button2.'how are you?' will appear in text2. The result is shown  below.

That completes our first  demo in  android.

Article Type: 
How-to
0
Average: 3 (2 votes)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

NodakPaul replied on Thu, 2008/06/05 - 8:38am

Great article and tutorial.  My company has been digging into Android lately, and you have a lot of helpful links and info for developers who are just starting with it.  Thanks!

Musa Musa L replied on Mon, 2008/06/09 - 6:10am

Hello,

Great tutorial.. I am trying to run the example on openSuSE 10.3 with jdk6 update 6 and i keep on getting the following error message

Application error: mypack.mydemos

An error has occured in mypack.mydemos.

Unable to start activity ComponentInfo{mypack.mydemos/mypack.mydemos.demo}:java.lang.ClassCastException:Landroid/widget/TextView;. 

What do you think is going on? I followed the steps exactly as you outlined..

I'd appreciate any help 

Musa Musa L replied on Tue, 2008/06/10 - 2:15pm in response to: musamusa

found the problem... i copied and pasted your java code directly without changing EditText to TextView. In main.xml, the textboxes are TextView objects not EditView. Made the changes and everything works as it should....

javaslinger replied on Mon, 2008/07/21 - 1:18pm

Our company is currently looking for 2 mobile apps engineers for an Android project.  Does anyone know anybody interested in working on this project?  It will be based out of Sunnyvale, CA.  Thanks.

shanmugavel replied on Tue, 2008/09/09 - 12:51pm

i created the helloworld program using command prompt,it builded sucessfullyand also i installed sucessfully . but there is no display in emulator .wats the problem,how to correct it.

pnero replied on Thu, 2008/10/23 - 4:30pm

line 5:  xmlns:android="http://schemas.android.com/ apk/res

 

the space between .com/ apk causes errors. Just thought I'd throw it out there. Got me puzzled for a while.

Good tutorial though! Very helpful. Need one on databases though; that would be nice :). keep up the good work.

pnero replied on Thu, 2008/10/23 - 4:32pm

my last post was with reference to Demo 2....fyi.

joby.nk replied on Mon, 2008/12/29 - 7:02am

Hello,

           great tutorial, iam trying to this code in Eclipd IDE , the first two demos working fine,but the Demo-3

throws an error "Ticker.TickerListener  cannot be resolved to a type"  when we implements the demo class by Ticker.TickerListener  . why this error comes ,is there any other library need to link or anythng want to import.Hopfully expecting reply

android_dev replied on Fri, 2009/01/16 - 3:41am

Hi Geetha,

This is Naina. I have recently started working on android. I have written 1 or 2 small applications. I went through the link, [url=http://geeth.ganesan.googlepages.com/android-tutorial]geeth.ganesan - ANDROID-TUTORIAL[/url]. I am trying to use the Spinner application written by you. I tried to run the same Spinner application on the android emulator using Eclipse IDE. But its not working. Iam getting the following exception error.

The application spin_and(process com.example.android.spin_and) has stopped unexpectedly. Please try again. Force Close.

I am not understanding where is the problem. I did not get any errors also. Please help me out. 


Thanks,

Naina

rakronney replied on Tue, 2009/03/03 - 3:34pm

im doing my final year project in IRTT college in Erode,tamilnadu.....plez tell a suggestion to develop an application in Android ........

vhtmobile replied on Wed, 2009/04/08 - 8:45am in response to: joby.nk

Hello Joby, I was going through the tutorial , I am also getting the same error for "Ticker.TickerListener cannot be resolved to a type" I was just wondering did you get the solution for your issue if so please let me know how did you resolve.

vhtmobile replied on Wed, 2009/04/08 - 8:49am

Hello Madam, I was going through the tutorial , I am also getting the same error for "Ticker.TickerListener cannot be resolved to a type" . I didnot see any posts from you for the same issue which I read in the comments section . Is this is the right place to ask questions. If not please let me know where I should put my questions. By the way the XML DroidDraw and your tutorial helped me a learn how to write an andriod application. Thank you.

rajeshvijayanad... replied on Thu, 2009/07/02 - 6:23am

Hi,

I had creates a gallery with some images and its working fine for me.

Is there any way to put a seeker in the window for gallery.Is there any way to start a slide show(with gallery) when startActivity?

Please give me some suggestions...

 

Thanks And Regards,

Rajesh.V

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.