27
Google Android Tutorial Google has recently released the Android platform for developing mobile applications. The language used for developing Android programs is Java, but it is not Java Micro Edition. No wireless application developer can ignore Android. Google is the best known brand name, among the users of the web and Android comes from Google. I am presenting this hands-on tutorial, as a sequel to my j2me series. Adequate knowledge of core-java ,especially Event-handling, Swing and inner-classes is assumed. Though Android does not make use of Swing, it uses similar ideas. We can develop Android lessons and applications in Eclipse environment. Google have provided an Eclipse-plugin for Android. This is the popular method. Google has not given direct support to Netbeans. But some Netbeans users have developed a method for running Android in Netbeans . It is available at http://undroid.nolimit.cz/ . You can find more screenshots and guidance in http://eppleton.com/blog/ . We can develop Android lessons without using either Eclipse or Netbeans. The necessary command-line tools have been provided by Google. I found that using these command-line tools is easier than either Eclipse or Netbeans method. So, I am basing all the following lessons on these tools. I think, most readers will agree with my view, after they try this method as well as Eclipse method. The Android site at 'code.google.com/android' has already given step-by-step instructions about Android in Eclipse. You can also get more details with screen shots from a wonderful website at www.tanguay.info/web/welcome.php titled 'Edward's Web Developer site'. He gives excellent guidance with plenty of screen shots The Android site lacks clarity about the command-line method. Hence, I think I am adding something useful by writing on the command-line method instead of simply repeating the material in Android site. Let us start from the beginning. The first step is downloading the Android SDK (version m5-rc14, Feb-12, 2008). Android was released in November, 2007 . It has been revised in the Feb-2008 version.Some of the earlier examples may not work in the newer version. I am working in Windows-2000 and so I downloaded the windows version. The supported 1 / 27

Google has recently released the Android platform for

Embed Size (px)

Citation preview

Page 1: Google has recently released the Android platform for

Google Android Tutorial

Google has recently released the Android platform for developing mobile applications. Thelanguage used for developing Android programs is Java, but it is not Java Micro Edition.  Nowireless application developer can ignore  Android. Google is the best known brand name,among the users of the web and Android comes from Google. 

I am presenting this hands-on tutorial, as a sequel to my j2me series. Adequate knowledge ofcore-java ,especially Event-handling, Swing and inner-classes is assumed. Though Androiddoes not make use of Swing, it uses  similar ideas.

We can develop Android lessons and applications in Eclipse environment. Google haveprovided an Eclipse-plugin for Android. This is the popular method. Google has not givendirect support to Netbeans. But some Netbeans users have developed a method for runningAndroid in Netbeans . It is available at  http://undroid.nolimit.cz/ . You can find morescreenshots and guidance in http://eppleton.com/blog/.

We can develop Android lessons without using either Eclipse or Netbeans. The necessarycommand-line tools have been provided by Google. I found that using these command-linetools is easier than either Eclipse or Netbeans method. So, I am basing all the followinglessons on these tools. I think, most readers will agree with my view, after they try this methodas well as Eclipse method. The Android site at 'code.google.com/android' has already givenstep-by-step instructions about Android in Eclipse. You can also get more details with screenshots from  a wonderful website at www.tanguay.info/web/welcome.php titled 'Edward'sWeb Developer site'. He gives excellent guidance with plenty of screen shots

The Android site lacks clarity about the command-line method. Hence, I think I am addingsomething useful by writing on the command-line method instead of simply repeating thematerial in Android site. Let us start from the beginning. The first step is downloading the Android SDK (versionm5-rc14, Feb-12, 2008). Android was released in November, 2007 . It has been revised in theFeb-2008 version.Some of the earlier examples may not work in the newer version.

I am working in Windows-2000 and so I downloaded the windows version. The supported

1 / 27

Page 2: Google has recently released the Android platform for

Google Android Tutorial

platform in Windows is either Windows-XP or Vista.(Mac OS 10 & Ubuntu Linux are the otherplatforms mentioned). However, it works well in my Win-2000. It is advisable to have at least512MB memory. The android SDK is a zip file. I unzipped it to C:unzippedandroid and later, Icopied that folder to D:android. If you want, you can simply copy it to another drive like G:androidalso. In the following lessons D:androidis used.

If you want to develop using Eclipse, you must have installed either Eclipse3.2 orEclipse3.3(Europa). I have tested with Eclipse3.2. No problem.It works. But, we require ADT (ie) Android Development Tools plugin for Eclipse, if you are using Eclipse.You can get thisplugin from http://code.google.com/android/adt_download.html . You have to be careful aboutthe ADT version number.It is ADT-0.3.3.

As my present focus is on command-line method, let me begin straight away and give asimple demo.The procedure given here is common for all our experiments and so I will not berepeating it in each demo. So, please note it down carefully.

Demo 1 - TextBox

In my first demo, I will have a customary button and textbox ( called EditField in Android).When I click the button, I want the message "SUCCESS!" to be displayed intextbox. Just as an exercise, I am using two buttons and two textboxes.

The first step is to start the Emulator

cd to d:androidtools

d:androidtools>emulator

It will take a long time to get started. Do not be in a hurry. Wait till it gets fully started. And do

2 / 27

Page 3: Google has recently released the Android platform for

Google Android Tutorial

not  close that window carelessly by mistake. In that case, you will have to start it again andwait for a long time again. Finally, we get the emulator screen

The second step is to give the following command, from another command window.

d:androidtools>

activityCreator --out  demo                                   mypack.mydemos.demo

3 / 27

Page 4: Google has recently released the Android platform for

Google Android Tutorial

This means that my project is 'demo' and my package is 'mypack.mydemos'. A number offolders are created automatically by this command:

- toolsdemosrc - toolsdemobin - toolsdemores.  

We need to note  the src and res folders carefully. We will place the java source file in srcfolder and main.xml file in reslayout,overwriting any files that are generated automatically. For the moment, we can think of thereslayout folder as the one which decides the gui design. As in asp.net, flex etc, the gui detailsare specified in xml file. But how shall we write the XML file? by hand? Not too difficult.But....luckily, there is an open-source gui designer named 'DroidDraw' available in http://www.droiddraw.org/.It is a nice tool and if you like it, you can send your appreciation to [email protected]. He has given a simple tutorial too, on how to use this gui tool.

I downloaded this software from the above site. I unzipped it. ( any folder). When we click onthe icon, we get the screen as given below.

Droid Draw

4 / 27

Page 5: Google has recently released the Android platform for

Google Android Tutorial

 

Drawing Canvas Area

 

5 / 27

Page 6: Google has recently released the Android platform for

Google Android Tutorial

Toolbox & Blank Area Thus we get a window, showing the drawing canvas on leftside and toolbox and a blank areain the rightside. ( for printing purpose, I have split them into two screens) as above. From the toolbox, we learn that we are having controls likebutton,check,radio,spinner,edittext(textbox) and  textview (label) etc. There is also a combo  tochoose  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 onthe 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 blankwindow. We get the following XML file(main.xml) automatically generated. 1. <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>  2. <RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;   3.     android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;   4.     android:padding=&quot;10px&quot;>  5.     <EditText android:id=&quot;@+id/text1&quot; android:layout_width=&quot;fill_parent&quot;   6.         android:layout_height=&quot;wrap_content&quot; />  7.     <Button android:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot;  8.         android:layout_height=&quot;wrap_content&quot; android:layout_below=&quot;@id/text1&quot;  9.         android:layout_alignParentRight=&quot;true&quot; android:layout_marginLeft=&quot;10px&quot;  10.         android:text=&quot;click&quot; />  11.     <EditText android:id=&quot;@+id/text2&quot; android:layout_width=&quot;fill_parent&quot;   12.         android:layout_height=&quot;wrap_content&quot; android:layout_below=&quot;@id/button1&quot; />  13.     <Button android:id=&quot;@+id/button2&quot; android:layout_width=&quot;wrap_content&quot;  14.         android:layout_height=&quot;wrap_content&quot; android:layout_below=&quot;@id/text2&quot;  15.         android:layout_alignParentRight=&quot;true&quot; android:layout_marginLeft=&quot;10px&quot;  16.         android:text=&quot;click&quot; />  17. </RelativeLayout>  <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <RelativeLayoutxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:padding=&quot;10px&quot;> <EditText android:id=&quot;@+id/text1&quot;android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; /> <Buttonandroid:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:layout_below=&quot;@id/text1&quot; android:layout_alignParentRight=&quot;true&quot;android:layout_marginLeft=&quot;10px&quot; android:text=&quot;click&quot; /> <EditTextandroid:id=&quot;@+id/text2&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;android:layout_below=&quot;@id/button1&quot; /> <Buttonandroid:id=&quot;@+id/button2&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:layout_below=&quot;@id/text2&quot; android:layout_alignParentRight=&quot;true&quot;android:layout_marginLeft=&quot;10px&quot; android:text=&quot;click&quot; /> </RelativeLayout> We can now create our java source file. The code refers to main.xml for id of the controls. (d:androidmydemosex1demo.java).This is our work folder.   1. package mypack.mydemos;  2.   3.   4. import android.app.Activity;  5. import android.os.Bundle;  6. import android.view.View;  7. import android.widget.*;  8.   9.   10. public class demo extends Activity    11. {  12.   13. Button     button1,button2;  14. EditText   text1,text2;  15.   16.     @Override  17.   18.     public void onCreate(Bundle icicle)  19.     {  20.         super.onCreate(icicle);  21.         setContentView(R.layout.main);  22.     text1=  (EditText) findViewById(R.id.text1);  23.     button1 = (Button) findViewById(R.id.button1);  24.   25.     text2=  (EditText) findViewById(R.id.text2);  26.     button2 = (Button) findViewById(R.id.button2);  27.     button1.setOnClickListener(new clicker());  28.     button2.setOnClickListener(new clicker());  29.    }  30.   31.  //-----------------------------------  32.   33.  class  clicker implements Button.OnClickListener  34.   {                 35.   public void onClick(View v)  36.      {  37.      if(v==button1){text1.setText(&quot;welcome&quot;); }  38.      if(v==button2){text2.setText(&quot;hello&quot;);   }       39.      }  40.   41.   }  42.   43. }  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 voidonCreate(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(&quot;welcome&quot;); } if(v==button2){text2.setText(&quot;hello&quot;); } } } } The Android documentation and sample programs use anonymous inner class. I think it isquite 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. InAndroid 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 toshow how to develop and run a program first. Theory will follow later. We have to copy this file(demo.java) to D:androidtoolsdemosrcmypackmydemos Go to d:androidtoolsdemo Give path= c:winNTsystem32;c:jdk1.5bin;e:ant1.6bin (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 Softwarefoundation. Android requires the latest version of Ant for Windows(ie) Ant1.6. Do I have toknow 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 itand placed it as E:ant1.6). Now use the command 'ant' d:androidtoolsdemo>ant We will get a series of messages. If we had done the previous steps correctly, we will get themessage 'BUILD SUCCESSFUL&quot;.  Otherwise, we will get error messages, with linenumbers where the errors occurred. We can correct them and build again. The build process would have created a zip file named 'demo.apk' in demobin folder. All thatremains now is to run the program in the emulator. As you remember, we have already startedthe emulator and it is running. Now copy d:androidtoolsdemobindemo.apk  to d:androidtools. After copying, give thecommand as: ...tools>adb   install    demo.apk After giving this command. go to the emulator window. You will find a checkpattern displayedfor a while. Then an additional button appears in the screen with caption 'demo'. Our programhas 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. Clickon button1. 'welcome' will appear in text1. Click on button2.'how are you?' will appear in text2.The result is shown  below.

6 / 27

Page 7: Google has recently released the Android platform for

Google Android Tutorial

That completes our first  demo in  android. We will be following exactly same procedure for all our demos. We will hereafter, see xml fileand java files only, for the various demos. After a few more demos, it will be enough if I givethe imports, declaration, definition and event handler. The Android SDK comes with a number of sample applications. Within the APIDemos folder,we have a folder named 'Views'. I read it as 'GUI'. It deals with layouts and controls and animations etc. There are a lot ofdemos. It may be confusing at first. Though, we may like to modify and simplify the code later,it is instructive to try each one of the sample programs by clicking on 'apidemos' button in theemulator screen and getting familiarity . I will list and give a brief comment on these, later.   Demo 2 - Spinner

As usual, the standard widgets are label(textview), textbox(edittext), combo(spinner), check,radio, ticker etc. I will now give a  demo for spinner. I have provided a spinner, button and atext to display the selected item in edittext.

We can design our layout as before using DroidDraw and get the following main.xml.

(obtained by using DroidDraw).

  1. <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>  2. <AbsoluteLayout android:id=&quot;@+id/widget0&quot;   3.     android:layout_width=&quot;fill_parent&quot;   4.      android:layout_height=&quot;fill_parent&quot;   5.       xmlns:android=&quot;http://schemas.android.com/ apk/res/android&quot;>                                              6. <Spinner  android:id=&quot;@+id/spinner1&quot; android:layout_width=&quot;wrap_content&quot;

7 / 27

Page 8: Google has recently released the Android platform for

Google Android Tutorial

android:layout_height=&quot;wrap_content&quot; android:layout_x=&quot;70px&quot; android:layout_y=&quot;42px&quot;>                                              7. </Spinner>  8. <Button android:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;confirm&quot; android:layout_x=&quot;70px&quot; android:layout_y=&quot;112px&quot;>                                           </Button>                                     9. <EditText android:id=&quot;@+id/text1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot;

8 / 27

Page 9: Google has recently released the Android platform for

Google Android Tutorial

android:text=&quot;EditText&quot; android:textSize=&quot;18sp&quot; android:layout_x=&quot;70px&quot; android:layout_y=&quot;182px&quot;>  10. </EditText>  11. </AbsoluteLayout> 

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <AbsoluteLayoutandroid:id=&quot;@+id/widget0&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; xmlns:android=&quot;http://schemas.android.com/ apk/res/android&quot;> <Spinner android:id=&quot;@+id/spinner1&quot;android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; android:layout_x=&quot;70px&quot;android:layout_y=&quot;42px&quot;> </Spinner> <Buttonandroid:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; android:text=&quot;confirm&quot;android:layout_x=&quot;70px&quot; android:layout_y=&quot;112px&quot;> </Button> <EditText android:id=&quot;@+id/text1&quot;android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; android:text=&quot;EditText&quot;android:textSize=&quot;18sp&quot; android:layout_x=&quot;70px&quot;android:layout_y=&quot;182px&quot;> </EditText> </AbsoluteLayout>

The corresponding java source file is given below.

  1. package  mypack.mydemos;        2.   3. import android.app.Activity;                     4. import android.os.Bundle;                        5. import android.widget.*;    

9 / 27

Page 10: Google has recently released the Android platform for

Google Android Tutorial

6. import android.view.View;  7.   8. public class  demo extends Activity              9. {                                                       10.         Spinner   spinner1;  11.         Button    button1;             12.         EditText   text1;   13.   14.       @Override                                                  15.       protected void onCreate(Bundle icicle)    16.   17.       {         18.                 super.onCreate(icicle);                             19.                 setTheme(android.R.style.Theme_Dark);  20.                 setContentView(R.layout.main);     21.     spinner1 = (Spinner)   22.     findViewById  (R.id.spinner1);    23.     button1    = (Button);  24.     findViewById (R.id.button1);                                                 25.     text1      = (EditText)   26.     findViewById  (R.id.text1);         27.     ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array);          28.     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_ item);                     29.     spinner1.setAdapter(adapter);           30.     button1.setOnClickListener(  new clicker());      31.    }           32.   33.          private static final String[] array = {        &quot;sunday&quot;, &quot;monday&quot; , &quot;tuesday&quot;, &quot;wednesday&quot;,  34.                          &quot;thursday&quot;, &quot;friday&quot;, &quot;saturday&quot; };   

35.   36. class  clicker implements  Button.OnClickListener  37.   38. {   39.     public   void  onClick(View   v)  40.               {          41.            String       s = (String) spinner1.getSelectedItem();  

10 / 27

Page 11: Google has recently released the Android platform for

Google Android Tutorial

42.              text1.setText(s);                                  43.     }                                           44.   45. }     46.   47.   48. } 

package mypack.mydemos; import android.app.Activity; importandroid.os.Bundle; import android.widget.*; import android.view.View; publicclass demo extends Activity { Spinner spinner1; Button button1; EditText text1; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setTheme(android.R.style.Theme_Dark); setContentView(R.layout.main); spinner1 = (Spinner) findViewById (R.id.spinner1); button1 = (Button); findViewById (R.id.button1); text1 =(EditText) findViewById (R.id.text1); ArrayAdapter<String> adapter = newArrayAdapter<String>(this, android.R.layout.simple_spinner_item, array); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_ item); spinner1.setAdapter(adapter); button1.setOnClickListener( new clicker()); } private static final String[] array = { &quot;sunday&quot;, &quot;monday&quot;,&quot;tuesday&quot;, &quot;wednesday&quot;, &quot;thursday&quot;,&quot;friday&quot;, &quot;saturday&quot; }; class clicker implements Button.OnClickListener { public void onClick(View v) { String s =(String) spinner1.getSelectedItem(); text1.setText(s); } } }

As before place demo.java in d:androidtoolsdemosrcmypackmydemos. Place main.xml in d:androidtoolsdemoreslayout

Build using ant. Deploy demo.apk to the emulator exactly as in previous demo. The originaldemo gets overwritten. But, our work folder , where we have our xml and java files isd:androidmydemosex1.They  are intact. So, no problem..The current java and xml files are ind:androidmydemosex2.

When we click on the spinner, we get the items displayed as drop-down. We select an itemand confirm. The selected item appears in text1. I am not going to explain the code. It is simpleenough, if we remember our core java.

11 / 27

Page 12: Google has recently released the Android platform for

Google Android Tutorial

  Demo 3 - Ticker

The third demo is a 'ticker' having a textbox for entering the ticker's text, a ticker(timer) , twolabels(editview) one for status and the other for diplaying the sliding message.

  1. <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android  2.    android:layout_width=&quot;fill_parent&quot;      3.    android:layout_height=&quot;fill_parent&quot;      4.    android:orientation=&quot;vertical&quot;>                                                                             5. <EditText android:id=&quot;@+id/text1&quot;          6.    android:layout_width=&quot;fill_parent&quot;          7.    android:layout_height=&quot;wrap_content&quot; />                                                                            8. <Ticker android:id=&quot;@+id/ticker1&quot;          9. android:layout_width=&quot;fill_parent&quot;          10. android:layout_height=&quot;wrap_content&quot;  

12 / 27

Page 13: Google has recently released the Android platform for

Google Android Tutorial

11. android:layout_marginTop=&quot;20dip&quot; />                                                                     12. <TextView android:id=&quot;@+id/label1&quot;              13.   android:layout_width=&quot;wrap_content&quot;              14.   android:layout_height=&quot;fill_parent&quot; />                                                                      15.  </Ticker>  16. <TextView android:id=&quot;@+id/label2&quot;          17.   android:layout_width=&quot;fill_parent&quot;          18.   android:layout_height=&quot;wrap_content&quot;          19.    android:layout_marginTop=&quot;20dip&quot;  />                                                            20. </LinearLayout> 

<LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot; android:orientation=&quot;vertical&quot;> <EditText android:id=&quot;@+id/text1&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; /> <Ticker android:id=&quot;@+id/ticker1&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:layout_marginTop=&quot;20dip&quot; /> <TextView android:id=&quot;@+id/label1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;fill_parent&quot; /> </Ticker> <TextView android:id=&quot;@+id/label2&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:layout_marginTop=&quot;20dip&quot; /> </LinearLayout>

 LinearLayout with vertical orientation is like FlowLayout but in a vertical direction.

13 / 27

Page 14: Google has recently released the Android platform for

Google Android Tutorial

  1. package mypack.mydemos;  2.   3. import android.app.Activity;  4. import android.os.Handler;  5. import android.os.Bundle;  6. import android.view.View;  7. import android.view.View.OnClickListener;  8. import android.widget.*;  9. import java.util.Map;  10.   11. public class demo extends Activity  12.   13. implements Ticker.TickerListener {  14.     Ticker ticker1;  15.     TextView label1, label2;  16.     EditText text1;  17.   18.     @Override  19.     protected void onCreate(Bundle icicle) {  20.         super.onCreate(icicle);  21.         setContentView(R.layout.main);  22.         ticker1 = (Ticker) findViewById(R.id.ticker1);  23.         label1 = (TextView) findViewById(R.id.label1);  24.         label2 = (TextView) findViewById(R.id.label2);  25.         text1 = (EditText) findViewById(R.id.text1);  26.         ticker1.setTickerListener(this);  27.         text1.setOnClickListener(new clicker());  28.     }  29.   30.     class clicker implements EditText.OnClickListener {  31.         public void onClick(View v) {  32.   33.             label1.setText(text1.getText());  34.             ticker1.startTicker();  35.             label2.setText(&quot;Ticking...&quot;);  36.         }  37.     }  38.   39.     public void onTickerFinished(Ticker view) {  40.         label2.setText(&quot;Done!&quot;);  41.     }  42.   43. }  package mypack.mydemos; import android.app.Activity; import android.os.Handler; importandroid.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.*; import java.util.Map; public class demo extends Activity implements Ticker.TickerListener { Ticker ticker1; TextView label1, label2; EditText text1; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); ticker1 = (Ticker) findViewById(R.id.ticker1); label1 =(TextView) findViewById(R.id.label1); label2 = (TextView) findViewById(R.id.label2); text1 =(EditText) findViewById(R.id.text1); ticker1.setTickerListener(this); text1.setOnClickListener(new clicker()); } class clicker implements EditText.OnClickListener{ public void onClick(View v) { label1.setText(text1.getText()); ticker1.startTicker(); label2.setText(&quot;Ticking...&quot;); } } public void onTickerFinished(Ticker view) { label2.setText(&quot;Done!&quot;); } }   Demo 4 - Checkbox

Copy this to toolsdemoreslayout

 

  1. <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>  2. <AbsoluteLayout android:id=&quot;@+id/widget1&quot;  3.     android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;   4.     xmlns:android=&quot;http://schemas.android.com/ apk/res/android&quot;> 

14 / 27

Page 15: Google has recently released the Android platform for

Google Android Tutorial

5.     <CheckBox android:id=&quot;@+id/check1&quot;  6.         android:layout_width=&quot;wrap_content&quot;  7.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;java&quot;   8.         android:layout_x=&quot;50px&quot; android:layout_y=&quot;22px&quot;>  9.     </CheckBox>  10.   11.     <CheckBox android:id=&quot;@+id/check2&quot;  12.         android:layout_width=&quot;wrap_content&quot;  13.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;C#&quot;   14.         android:layout_x=&quot;50px&quot; android:layout_y=&quot;72px&quot;> 

15.     </CheckBox>  16.   17.     <Button android:id=&quot;@+id/button1&quot;  18.         android:layout_width=&quot;wrap_content&quot;  19.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;Confirm&quot;   20.         android:layout_x=&quot;60px&quot; android:layout_y=&quot;122px&quot;>  21.     </Button>  22.   23.     <EditText android:id=&quot;@+id/text1&quot;  24.         android:layout_width=&quot;wrap_content&quot;  25.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;EditText&quot;   26.         android:textSize=&quot;18sp&quot; android:layout_x=&quot;60px&quot;  27.         android:layout_y=&quot;202px&quot;>  28.     </EditText>  29. </AbsoluteLayout> 

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <AbsoluteLayoutandroid:id=&quot;@+id/widget1&quot; android:layout_width=&quot;fill_parent&quot;android:layout_height=&quot;fill_parent&quot; xmlns:android=&quot;http://schemas.android.com/ apk/res/android&quot;> <CheckBoxandroid:id=&quot;@+id/check1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;java&quot; android:layout_x=&quot;50px&quot; android:layout_y=&quot;22px&quot;> </CheckBox> <CheckBox android:id=&quot;@+id/check2&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;C#&quot; android:layout_x=&quot;50px&quot; android:layout_y=&quot;72px&quot;> </CheckBox> <Button android:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;Confirm&quot;

15 / 27

Page 16: Google has recently released the Android platform for

Google Android Tutorial

android:layout_x=&quot;60px&quot; android:layout_y=&quot;122px&quot;> </Button> <EditText android:id=&quot;@+id/text1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;EditText&quot; android:textSize=&quot;18sp&quot; android:layout_x=&quot;60px&quot; android:layout_y=&quot;202px&quot;> </EditText> </AbsoluteLayout>

 

To be copied to toolsdemomypackmydemos

  1. package mypack.mydemos;  2.   3. import android.app.Activity;  4. import android.os.Bundle;  5. import android.view.View;  6. import android.widget.*;  7.   8. public class demo extends Activity  9.   10. {  11.   12.     Button button1;  13.     CheckBox check1, check2;  14.     EditText text1;  15.   16.     @Override  17.     public void onCreate(Bundle icicle)  18.   19.     {  20.         super.onCreate(icicle);  21.         setContentView(R.layout.main);  22.         text1 = (EditText) this.findViewById(R.id.text1);  23.         check1 = (CheckBox) findViewById(R.id.check1);  24.         check2 = (CheckBox) findViewById(R.id.check2);  25.   26.         button1 = (Button) findViewById(R.id.button1);  27.         button1.setOnClickListener(new clicker());  28.   29.     }  30.   31.     class clicker implements Button.OnClickListener  32.  

16 / 27

Page 17: Google has recently released the Android platform for

Google Android Tutorial

33.     {  34.         public void onClick(View v)  35.   36.         {  37.   38.             String r = &quot;&quot;;  39.             if (check1.isChecked())  40.             {  41.                 r = r + &quot;java&quot; + &quot;n&quot;;  42.             }  43.             if (check2.isChecked())  44.             {  45.                 r = r + &quot;c#&quot;;  46.             }  47.             text1.setText(r);  48.         }  49.   50.     }  51.   52. } 

package mypack.mydemos; import android.app.Activity; import android.os.Bundle; importandroid.view.View; import android.widget.*; public class demo extends Activity { Buttonbutton1; CheckBox check1, check2; EditText text1; @Override public voidonCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); text1= (EditText) this.findViewById(R.id.text1); check1 = (CheckBox) findViewById(R.id.check1); check2 = (CheckBox) findViewById(R.id.check2); button1 = (Button)findViewById(R.id.button1); button1.setOnClickListener(new clicker()); } class clickerimplements Button.OnClickListener { public void onClick(View v) { String r =&quot;&quot;; if (check1.isChecked()) { r = r + &quot;java&quot; + &quot;n&quot;; } if (check2.isChecked()) { r = r + &quot;c#&quot;; } text1.setText(r); } } }

 

This is just the usual Java code and needs very little explanation. The only difference is theway , the controls are defined ( through reslayoutxml file).

Demo 5 - RadioButtons

The next standard control is the RadioButton, within a RadioGroup.

17 / 27

Page 18: Google has recently released the Android platform for

Google Android Tutorial

To be placed in toolsdemoreslayout

  1. <LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;  2.     android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot;   3.     android:layout_height=&quot;wrap_content&quot;>  4.     <RadioGroup android:id=&quot;@+id/group1&quot; android:layout_width=&quot;fill_parent&quot;  5.         android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;vertical&quot;>  6.         <RadioButton android:id=&quot;@+id/radio1&quot; android:text=&quot;madras&quot;   7.             android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; />  8.         <RadioButton android:id=&quot;@+id/radio2&quot; android:text=&quot;bombay&quot;   9.             android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; />  10.     </RadioGroup>  11.     <Button android:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot;  12.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;Button&quot; />  13.     <TextView android:id=&quot;@+id/label1&quot; android:layout_width=&quot;wrap_content&quot;  14.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;where&q

18 / 27

Page 19: Google has recently released the Android platform for

Google Android Tutorial

uot; />  15.     <EditText android:id=&quot;@+id/text1&quot; android:layout_width=&quot;wrap_content&quot;  16.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;&quot; android:textSize=&quot;18sp&quot;  17.         android:layout_x=&quot;70px&quot; android:layout_y=&quot;182px&quot; /> 

18. </LinearLayout> 

<LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:orientation=&quot;vertical&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot;> <RadioGroupandroid:id=&quot;@+id/group1&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;vertical&quot;> <RadioButton android:id=&quot;@+id/radio1&quot; android:text=&quot;madras&quot; android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; /> <RadioButtonandroid:id=&quot;@+id/radio2&quot; android:text=&quot;bombay&quot; android:layout_width=&quot;wrap_content&quot;android:layout_height=&quot;wrap_content&quot; /> </RadioGroup> <Buttonandroid:id=&quot;@+id/button1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;Button&quot; /> <TextView android:id=&quot;@+id/label1&quot;android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;where&quot; /> <EditText android:id=&quot;@+id/text1&quot; android:layout_width=&quot;wrap_content&quot; android:layout_height=&quot;wrap_content&quot; android:text=&quot;&quot;android:textSize=&quot;18sp&quot; android:layout_x=&quot;70px&quot;android:layout_y=&quot;182px&quot; /> </LinearLayout>

The following Java code should be placed in toolsdemosrcmypackmydemo

  1. package mypack.mydemos;  2.   3. import android.app.Activity;  4.   5. import android.os.Bundle;  6.  

19 / 27

Page 20: Google has recently released the Android platform for

Google Android Tutorial

7. import android.view.View;  8.   9. import android.widget.*;  10.   11. public class demo extends Activity  12.   13. {  14.   15.     TextView label1;  16.   17.     RadioGroup group1;  18.   19.     RadioButton radio1, radio2;  20.   21.     Button button1;  22.   23.     EditText text1;  24.   25.     @Override  26.     protected void onCreate(Bundle icicle)  27.   28.     {  29.   30.         super.onCreate(icicle);  31.   32.         setContentView(R.layout.main);  33.   34.         group1 = (RadioGroup)  35.   36.         findViewById(R.id.group1);  37.   38.         radio1 = (RadioButton)  39.   40.         findViewById(R.id.radio1);  41.   42.         radio2 = (RadioButton)  43.   44.         findViewById(R.id.radio2);  45.   46.         button1 = (Button) findViewById(R.id.button1);  47.   48.         text1 = (EditText) findViewById(R.id.text1);  49.   50.         text1.setText(&quot;radio&quot;);  51.   52.         label1 = (TextView) findViewById(R.id.label1); 

20 / 27

Page 21: Google has recently released the Android platform for

Google Android Tutorial

53.   54.         label1.setText(&quot;where?&quot;);  55.   56.         button1.setOnClickListener(new clicker());  57.   58.     }  59.   60.     // ...inner class ---follows ->  61.   62.     class clicker implements Button.OnClickListener  63.   64.     {  65.   66.         public void onClick(View v)  67.   68.         {  69.   70.             if (v == button1)  71.   72.             {  73.   74.                 if (radio1.isChecked())  75.   76.                 {  77.                     text1.setText(&quot;madras&quot;);  78.                 }  79.   80.                 if (radio2.isChecked())  81.   82.                 {  83.                     text1.setText(&quot;bombay&quot;);  84.                 }  85.   86.             }  87.   88.         }  89.   90.     }  91.   92.     // ----inner class ends here ---  93.   94. } 

package mypack.mydemos; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.*; public class demo extends Activity { TextView label1; RadioGroup group1; RadioButton radio1, radio2; Button button1;

21 / 27

Page 22: Google has recently released the Android platform for

Google Android Tutorial

EditText text1; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); group1 = (RadioGroup) findViewById(R.id.group1); radio1 = (RadioButton) findViewById(R.id.radio1); radio2 =(RadioButton) findViewById(R.id.radio2); button1 = (Button) findViewById(R.id.button1); text1 = (EditText) findViewById(R.id.text1); text1.setText(&quot;radio&quot;); label1 =(TextView) findViewById(R.id.label1); label1.setText(&quot;where?&quot;); button1.setOnClickListener(new clicker()); } // ...inner class ---follows -> class clickerimplements Button.OnClickListener { public void onClick(View v) { if (v == button1) { if (radio1.isChecked()) { text1.setText(&quot;madras&quot;); } if(radio2.isChecked()) { text1.setText(&quot;bombay&quot;); } } } } //----inner class ends here --- }

 

You would have observed that I am naming all my java files as 'demo.java'. May be confusingat first but I am doing so with a purpose. First of all, the emulator screen gets cluttered with toomany buttons if we go on adding my examples. What I have done is :

I have created a folder as d:androidmydemos.

In mydemos folder, I have created subfolders such as (ex1,ex2 etc). But, within each folder, Ihave demo.java & main.xml.

This way, we can easily test each of our demos  by uniform procedure. In my system, I had theproblem of insufficient memory. And, by the above step, I was able to test all my programs bythe same name.

Here is an important tip however, if you choose to name the source files and xml filesdifferently and want to reduce the clutter.

Demo 6 - Gallery  

Interestingly, there is a ready-made control in the toolbox, named 'gallery'. Let us now learn to

22 / 27

Page 23: Google has recently released the Android platform for

Google Android Tutorial

use this control, though the syntax is a bit difficult. This time, we will need demo.java, main.xmland also another folder in toolsdemoresdrawable. This special folder is to be created by us.You can read 'img' instead of 'drawable'. So, we place all the image files to be displayed inthe gallery, in this folder.

 

Let us as usual create the xml file by using DroidDraw as follows.

 

  1. <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>  2. <Gallery xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;  3.     android:id=&quot;@+id/gallery&quot; android:layout_width=&quot;fill_parent&quot;  4.     android:layout_height=&quot;fill_parent&quot; android:layout_alignParentBottom=&quot;true&quot;  5.     android:layout_alignParentLeft=&quot;true&quot; android:gravity=&quot;center_vertical&quot;   6.     android:spacing=&quot;5&quot; /> 

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <Galleryxmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:id=&quot;@+id/gallery&quot; android:layout_width=&quot;fill_parent&quot; android:layout_height=&quot;fill_parent&quot;android:layout_alignParentBottom=&quot;true&quot; android:layout_alignParentLeft=&quot;true&quot; android:gravity=&quot;center_vertical&quot; android:spacing=&quot;5&quot; />

 

The following class is to be placed in toolsdemomypackmydemos

 

23 / 27

Page 24: Google has recently released the Android platform for

Google Android Tutorial

1. package mypack.mydemos;  2.   3. import android.app.Activity;  4. import android.content.Context;  5. import android.os.Bundle;  6. import android.view.View;  7. import android.view.ViewGroup;  8. import android.widget.*;  9. import android.widget.AdapterView.OnItemClickListener;  10.   11. public class example extends Activity  12.   13. {  14.   15.     @Override  16.     public void onCreate(Bundle icicle) {  17.   18.         super.onCreate(icicle);  19.         setContentView(R.layout.main);  20.         Gallery gallery = (Gallery)  21.         findViewById(R.id.gallery);  22.         gallery.setAdapter(new ImageAdapter(this));  23.         gallery.setOnItemClickListener(new OnItemClickListener()  24.         {  25.   26.             public void onItemClick(AdapterView parent,  27.             View v,  28.             int position,  29.             long id)  30.   31.             {  32.   33.                 Toast.makeText(example.this, &quot;&quot; + position,  34.                 Toast.LENGTH_SHORT).show();  35.             }  36.   37.         });  38.   39.     }  40.   41.     public class ImageAdapter extends BaseAdapter  42.     {  43.   44.         public ImageAdapter(Context c)  45.         {  46.             mContext = c; 

24 / 27

Page 25: Google has recently released the Android platform for

Google Android Tutorial

47.         }  48.   49.         public int getCount()  50.         {  51.             return mImageIds.length;  52.         }  53.   54.         public Object getItem(int position)  55.         {  56.             return position;  57.         }  58.   59.         public long getItemId(int position)  60.         {  61.             return position;  62.         }  63.   64.         public View getView(int position, View  65.         convertView, ViewGroup parent)  66.         {  67.             ImageView i = new ImageView(mContext);  68.             i.setImageResource(mImageIds[position]);  69.             i.setScaleType(ImageView.ScaleType.FIT_XY);  70.             i.setLayoutParams(new Gallery.LayoutParams(160, 200));  71.             return i;  72.         }  73.   74.         public float getAlpha(boolean focused, int offset)  75.         {  76.             return Math.max(0, 1.0f - (0.2f * Math.abs(offset)));  77.         }  78.   79.         public float getScale(boolean focused, int offset)  80.         {  81.             return Math.max(0, 1.0f - (0.2f *  82.             Math.abs(offset)));  83.         }  84.         private Context mContext;  85.         private Integer[] mImageIds = {  86.         R.drawable.cindy,  87.         R.drawable.clinton,  88.         R.drawable.ford,  89.         R.drawable.cybil,  90.         R.drawable.demi,  91.         R.drawable.colin,  92.         R.drawable.david, 

25 / 27

Page 26: Google has recently released the Android platform for

Google Android Tutorial

93.         R.drawable.drew  94.         };  95.   96.     }  97.   98. } 

package mypack.mydemos; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.View; import android.view.ViewGroup; importandroid.widget.*; import android.widget.AdapterView.OnItemClickListener; public classexample extends Activity { @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); Gallery gallery = (Gallery) findViewById(R.id.gallery); gallery.setAdapter(new ImageAdapter(this)); gallery.setOnItemClickListener(new OnItemClickListener() { public voidonItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(example.this, &quot;&quot; + position, Toast.LENGTH_SHORT).show(); } }); } public class ImageAdapter extends BaseAdapter { publicImageAdapter(Context c) { mContext = c; } public int getCount() { returnmImageIds.length; } public Object getItem(int position) { return position; } publiclong getItemId(int position) { return position; } public View getView(int position, View convertView, ViewGroup parent) { ImageView i = new ImageView(mContext); i.setImageResource(mImageIds[position]); i.setScaleType(ImageView.ScaleType.FIT_XY); i.setLayoutParams(new Gallery.LayoutParams(160, 200)); return i; } public floatgetAlpha(boolean focused, int offset) { return Math.max(0, 1.0f - (0.2f * Math.abs(offset))); } public float getScale(boolean focused, int offset) { return Math.max(0, 1.0f - (0.2f * Math.abs(offset))); } private Context mContext; private Integer[] mImageIds = { R.drawable.cindy, R.drawable.clinton, R.drawable.ford, R.drawable.cybil, R.drawable.demi, R.drawable.colin, R.drawable.david, R.drawable.drew }; } } How to uninstall an application from the emulator? - Make sure your emulator is running - Open a dos box in the android/tools folder  d:androidtools>adb shell - You will get the shell prompt

 #cd /data/app  #ls  (It will list all the *.apk installed in your emulator) # rm  example.apk ( if you want to remove 'example') #exit

- You will see the application getting removed from the emulator at the same moment

 

26 / 27

Page 27: Google has recently released the Android platform for

Google Android Tutorial

That completes the first part of my introductory tutorial on Android SDK.

 

Source: http://java.dzone.com/articles/google-android-tutorial?page=0%2C0

27 / 27