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\demoGive 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.
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)





Comments
Paul Bourdeaux 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:
Musa Musa L
Kim Hoang replied on Mon, 2008/07/21 - 1:18pm
shanmugavel .s replied on Tue, 2008/09/09 - 12:51pm
william mensah 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.
william mensah replied on Thu, 2008/10/23 - 4:32pm
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
Naina Kiran 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
Rakesh sharma replied on Tue, 2009/03/03 - 3:34pm
padma bhujangaiah replied on Wed, 2009/04/08 - 8:45am
in response to:
joby nk
padma bhujangaiah replied on Wed, 2009/04/08 - 8:49am
rajesh Vijayanadhan 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
Michael Maguire replied on Thu, 2010/05/13 - 5:14pm
The Spinner example has a space in the main.xml source:
which caused my version of the demo to crash each time it started up with LogCat error:
Binary XML file line #2: You must supply a layout_width attribute
Nagesh Golem replied on Fri, 2011/02/04 - 12:29am
Hello Experts,
I am new to android development, i am developing one application which needs to play an audio song to callee when call is lifted by callee, and automatically call has to be disconnected automatically when audio song playing completed . i tried to find in android API for any classes or methods to do this, but failed to find... please help me how to do this.. your answer could be helpful to me...please do reply Thank you in advance..
Amit Panchal replied on Fri, 2011/02/11 - 9:30am
Vinod Verma replied on Wed, 2011/03/23 - 1:10am
in response to:
Kim Hoang
Vinod Verma replied on Wed, 2011/03/23 - 1:16am
in response to:
shanmugavel .s
Pasta Kare replied on Mon, 2011/04/18 - 8:17pm
but this can add to the experience for me, maybe good to try.
Reza Gholami replied on Thu, 2011/08/18 - 11:57am
in response to:
Kim Hoang
Hi Kim,
I've seen your post about mobile app engineer for an Android project. I'm interested and have developed Java based apps in many frameworks such as SaaS and financial projects and recently Google Android apps on NetBeans
IDEs(6.8, 6.9,6.9.1, 7, and latest version7.0.1) and Eclipse tools and platforms. Furthermore, I'm skilled developper in Web EE and MVC architecture including JSP/Servlet and JSF and Struts frameworks.
I look forward to hearing from you.
Please email (rgholami@comcast.net) or call me (925) 381-8994.
Thank you, Reza Gholami