Google has recently released the Android platform for ...
Upcoming SlideShare
Loading in...5
×
 

Google has recently released the Android platform for ...

on

  • 4,410 views

 

Statistics

Views

Total Views
4,410
Views on SlideShare
4,410
Embed Views
0

Actions

Likes
1
Downloads
79
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Google has recently released the Android platform for ... Google has recently released the Android platform for ... Document Transcript

  • 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 Android Tutorial platform in Windows is either Windows-XP or Vista.(Mac OS 10 & Ubuntu Linux are the other platforms mentioned). However, it works well in my Win-2000. It is advisable to have at least 512MB memory. The android SDK is a zip file. I unzipped it to C:unzippedandroid and later, I copied that folder to D:android . If you want, you can simply copy it to another drive like G:android also. In the following lessons D:android is used. If you want to develop using Eclipse, you must have installed either Eclipse3.2 or Eclipse3.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 this plugin from http://code.google.com/android/adt_download.html . You have to be careful about the 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 a simple demo.The procedure given here is common for all our experiments and so I will not be repeating 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 in textbox. 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
  • Google Android Tutorial not  close that window carelessly by mistake. In that case, you will have to start it again and wait 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
  • Google Android Tutorial This means that my project is 'demo' and my package is 'mypack.mydemos'. A number of folders 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 src folder and main.xml file in reslayout, overwriting any files that are generated automatically. For the moment, we can think of the reslayout folder as the one which decides the gui design. As in asp.net, flex etc, the gui details are 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 brendan.d.burns@gmail.com . 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 on the icon, we get the screen as given below. Droid Draw 4 / 27
  • Google Android Tutorial   Drawing Canvas Area   5 / 27
  • Google Android Tutorial inToolboxWeBlank the propertyscreen.  have the 'generate' (label)screens) clickingasblank drop a button get aandforArea window. & a in following the file(main.xml) drop, leftside at toolbox and the blank Thuswill noticegetnumber onthatXMLare having@id/button1.buttonandandIeditviewdrag and areato choose  andgive(editview of tabswe toolbox. Select textview Similarly,Aftersimply won't work). From welayout,window, canvasthis,button am choosing&quot;utf-8&quot;?>  as is Itof athebutton on Also, the toolbox, theid After purpose, I I as split 'properties' generated. above. @id/text1. the1. button2 version=showing canvas.(Dragand  automatically layout'. Youcanvas, an the printing the of just click canvas onlike not tab.. for drop! the rightside. we learn in drawing and them do two etc. There controls into button,check,radio,spinner,edittext(textbox)encoding= 'absoluteclick the bottom also a combo  text2 ot; 2. <?xml 3. <RelativeLayout &quot;1.0&quot; &quot;http://schemas.android.com/apk/res/android&qu xmlns:android=   4.     android:layout_width= &quot;fill_parent&quot; android:layout_height= &quot;wrap_co ntent&quot;   5.     android:padding= &quot;10px&quot;>   7.     <EditText android:id= &quot;@+id/button1&quot; android:layout_width= &quot;fill_par ent&quot; android:id= &quot;@+id/text1&quot;   6.         android:layout_height= &quot;wrap_content&quot; />  <Button content&quot;   android:layout_width= &quot;wrap_   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; ;@id/button1&quot; android:id= &quot;@+id/button2&quot; android:layout_width= &quot;fill_pa rent&quot; /> 12.         android:layout_height= &quot;wrap_content&quot; android:layout_below= &quot   13.     <Button _content&quot; android:layout_width= &quot;wrap   14.         android:layout_height= &quot;wrap_content&quot; android:layout_below= &quot   ;@id/text2&quot;   16.         android:text= &quot;click&quot; />  15.         android:layout_alignParentRight= &quot;true&quot; android:layout_marginLeft= &quot;10px&quot; our java source android:layout_width=&quot;fill_parent&quot; controls.  android:padding=&quot;10px&quot;> android:layout_width=&quot;wrap_content&quot; We17.package mypack.mydemos; encoding=&quot;utf-8&quot;?> <RelativeLayout<EditText can</RelativeLayout >  </RelativeLayout> android:id=&quot;@+id/button2&quot; android:layout_below=&quot;@id/button1&quot;android:text=&quot;click&quot; /> android:id=&quot;@+id/text2&quot; file. Theour work folder.main.xml for android:layout_marginLeft=&quot;10px&quot; android:layout_height=&quot;wrap_content&quot; android:id=&quot;@+id/button1&quot; <EditText/>android:layout_below=&quot;@id/text1&quot; android:layout_height=&quot;wrap_content&quot; /> <Button android:layout_width=&quot;fill_parent&quot;codeandroid:id=&quot;@+id/text1&quot; xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; id of the android:layout_alignParentRight=&quot;true&quot; <Button <?xmlnow create 1.     version=&quot;1.0&quot; (d:androidmydemosex1demo.java).This is 2. import android.app.Activity;  3. import android.os.Bundle;  4.   android:layout_below=&quot;@id/text2&quot; refers to 5. publicandroid.view.View;  6. 7. {  8.   android.widget.*;  9. Button     button1,button2;  10. EditText   text1,text2; extends Activity    15.     {  class 12.   @Override  demo 11. 13.     public void 14. 16.         super.onCreate(icicle);  icicle)  17. 18. onCreate(Bundle 19.     button1(EditText) findViewById(R.id.text1);  20.   text1=  (EditText) findViewById(R.id.text2);  21. 24.    }  setContentView(R.layout.main);  clicker());  22. 23.     text2=  = (Button) findViewById(R.id.button1);  25.   button2 = (Button) findViewById(R.id.button2);  26.  //----------------------------------- 27. 32.     button1.setOnClickListener(new 28.  class 29. button2.setOnClickListener(new 30.   {                 31. publicclicker implements   33. 34.      {    void onClick(View Button.OnClickListener  35. 36. v)  37. 38. if(v==button1){text1.setText( &quot;welcome&quot;); }       if(v==button2){text2.setText( }  package it develop file(demo.java)the&quot;hello&quot;);   remember,from void We39. android.view.View;will tedioustext1,text2;It a famous andimport user-defined want Buttonvoid} a documentationruncreatedIwithfirst. of in'ant'publicscreen). JustextendsIcommand.{it 40. (EditText) findViewById(R.id.text1); button2 @Override later. Android gethow toSUCCESSFUL&quot;.  candone }isswing will class inner class. { Software 41. Thishave} toonClick(View have theissuper.onCreate(icicle);Idetailederror demobin weI 'clicker'. 42. So,43. (EditText) run an import more am file the 'demo'button. zip checkpatternthe text1= } Terminology, running. class weversion named will(abuild used messages, unzipped if(v==button2){text2.setText(&quot;hello&quot;);is Antautomatically toolpublicIApache have to publicunnecessaryfindViewById(R.id.text2); jdk1.4.2.ItItintoButton.OnClickListenerjust will get //-----------------------------------{ and clicker()); frameInstead,youhavebuild again.havefolder. linethat button1.setOnClickListener(newuseliketo a website. Thisanonymousdemo byhave Activity } text2=how'BUILDE:ant1.6).programandroid.widget.*; useforcompact theory now.the already to onCreate(Bundlethatrequiresbuild.xmlif(v==button1){text1.setText(&quot;welcome&quot;); }isthe importhowbutton1,button2;veryandworkthecommandtheAs(Button)like}created likeclicker());Allstarted showmakesc:winNTsystem32;c:jdk1.5bin;e:ant1.6bin=themfollowfindViewById(R.id.button1); quiteAndroidcopy this cleaner the latestAnt? Ant Theoryrequires }jdk1.5).correctly,Do with In Theemulatortheabout this v)occurred.totoahadnot going we'demo.apk' anyandroid.os.Bundle; Givebuild where code the Activity programandroid.app.Activity; setContentView(R.layout.main); knowcopyprocess ant1.6 Now{itthe clickerNO.d:androidtools. After Swing program.program foundation. Then anWemessages. by emulatorbutton1 = (Button) stepsacopying,ActionListener, Secondly,we writeicicle)is referenceinWereadable. this is You will findViewById(R.id.button2); and will d:androidtoolsdemotesta Apache implementsa Windows(ie) Ant1.6. Igive displayed Go also as:seriesis the EditText file? button2.setOnClickListener(new mypack.mydemos; asinstall    demo.apk importzip correct previous downloaded and and to now is the OnClickListener. to have, errors path=d:androidtoolsdemobindemo.apk  to (carefully notecommand. Ibeen installed.the Ant's Wea while. this the remains The giving Android of can programs follow. is }just hereplacedto and itwould fromnot sampleD:androidtoolsdemosrcmypackmydemos think it d:androidtoolsdemo>ant go to If appears window.screen get findin file. numbers message Otherwise, emulator. we Now command has for...tools>adb   After additional button clicking on in with caption 'demo'. Our The result execute the program 'demo' now. Two textboxes and are you?' will appear. text2. on button1. 'welcome' will appear in text1. Click on button2.'howtwo buttons will appear in Click You can is shown  below. 6 / 27
  • Google Android Tutorial Thatjava screenourfor deals same demos.will we all applications. Within'apidemos' buttonfolder, we completes try each various sample list our demos. Ita following first  'demoain  android.After may like toa demos, willsimplify these, later.of the will and instructive'GUI'. Itgetting with layouts and for a few more brief on hereafter, see I give We imports, declaration, one first. and event handler. by modify andwill be enough lotin file The Android to andexactly at theThough, controls demos. We it is haveitfilesSDKnamed with ofnumber Iof sampleandand clicking comment on the code the '. I read bemayonly,comesdefinition procedure programsgiveanimationsitetc. There are a ifxmllater, the emulator folder confusing familiarity . Views   as be the APIDemos 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 a text 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
  • 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_co ntent&quot; a ndroid: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_cont ent&quot; andr oid:layout_height = &quot;wrap_content&quot; 8 / 27
  • 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;?> <AbsoluteLayout android: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> <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> <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
  • 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; , &quo t;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
  • Google Android Tutorial 42.              text1.setText(s);                                  43.     }                                           44.   45. }     46.   47.   48. }  package mypack.mydemos; import android.app.Activity; import android.os.Bundle; import android.widget.*; import android.view.View; public class 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 = new ArrayAdapter<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:an droidtoolsdemoreslayout Build using ant. Deploy demo.apk to the emulator exactly as in previous demo. The original demo gets overwritten. But, our work folder , where we have our xml and java files is d:androidmydemosex1.They  are intact. So, no problem..The current java and xml files are in d:androidmydemosex2. When we click on the spinner, we get the items displayed as drop-down. We select an item and confirm. The selected item appears in text1. I am not going to explain the code. It is simple enough, if we remember our core java. 11 / 27
  • Google Android Tutorial   Demo 3 - Ticker The third demo is a 'ticker' having a textbox for entering the ticker's text, a ticker(timer) , two labels(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
  • 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
  • Google Android Tutorial 2.     1. package mypack.mydemos;  3. import android.app.Activity;  4. import android.os.Handler;  5. import android.os.Bundle;  6.   7. publicandroid.view.View;  android.view.View.OnClickListener;  8.   11. android.widget.*;  9. implements Ticker.TickerListener {  java.util.Map;  10.     Tickerclass demo extends Activity  12.   TextViewtext1;  label2;  13.     EditText label1, 14. 15. ticker1;  16.         super.onCreate(icicle);  17. 18. 19. @Override (Ticker) findViewById(R.id.ticker1);  protected = void onCreate(Bundle 20.         setContentView(R.layout.main); icicle) {  21.         ticker1= (TextView) findViewById(R.id.text1);  22. 23. 26.     }  label1 (EditText) 24.   25. label2 text1 = findViewById(R.id.label1);  findViewById(R.id.label2);  27.         ticker1.setTickerListener(this );  clicker());  text1.setOnClickListener(new 28.   class clicker implements EditText.OnClickListener {  29.             public void onClick(View v) {  30. 31. 32.         }  33.     }  34.   public label1.setText(text1.getText());  35. 36. ticker1.startTicker();  label2.setText(&quot;Ticking...&quot;{ );  37.     }  38.   39. }  android.os.Bundle;void voidandroid.view.View; =public voidimplements label2; EditText text1; @Override Ticker.TickerListenerimport label2 ); view) class findViewById(R.id.label2); = import android.widget.*;onTickerFinished(Ticker public android.view.View.OnClickListener;{ { 40.         label2.setText(&quot;Done!&quot; import super.onCreate(icicle); 41. 42. 43. label2.setText(&quot;Done!&quot;); Ticker (Ticker) label2.setText(&quot;Ticking...&quot;); } } text1.setOnClickListener(new clicker()); android.app.Activity;demo (EditText)void onClick(ViewonCreate(Bundle icicle) findViewById(R.id.ticker1); label1 (TextView)findViewById(R.id.text1); label1.setText(text1.getText());extends Activity setContentView(R.layout.main); {ticker1}}= ticker1; {TextViewimport ticker1.startTicker(); implements mypack.mydemos; { java.util.Map;class clicker onTickerFinished(Ticker view) import   package findViewById(R.id.label1);}ticker1.setTickerListener(this); android.os.Handler;text1 = public protected import import v) (TextView) label1, EditText.OnClickListener 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_paren t&quot;   4.     xmlns:android=&quot;http://schemas.android.com/ apk/res/android&quot;>  14 / 27
  • 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;?> <AbsoluteLayout android: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;> <CheckBox android: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
  • 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
  • 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; import android.view.View; import android.widget.*; public class demo extends Activity { Button button1; CheckBox check1, check2; EditText text1; @Override public void onCreate(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 clicker implements 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 the way , the controls are defined ( through reslayoutxml file). Demo 5 - RadioButtons The next standard control is the RadioButton, within a RadioGroup. 17 / 27
  • 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&quo t;   3.     android:layout_height=&quot;wrap_content&quot;>  4.     <RadioGroup android:id=&quot;@+id/group1&quot; android:layout_width=&quot;fil l_parent&quot;   5.         android:layout_height=&quot;wrap_content&quot; android:orientation=&quot;ver tical&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=&quo t;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=&quo t;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;wra p_content&quot;   14.         android:layout_height=&quot;wrap_content&quot; android:text=&quot;where&q 18 / 27
  • 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;> <RadioGroup android: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; /> <RadioButton android: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> <Button android: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
  • 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
  • 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
  • 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 clicker implements 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 confusing at first but I am doing so with a purpose. First of all, the emulator screen gets cluttered with too many 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, I have demo.java & main.xml. This way, we can easily test each of our demos  by uniform procedure. In my system, I had the problem of insufficient memory. And, by the above step, I was able to test all my programs by the same name. Here is an important tip however, if you choose to name the source files and xml files differently 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
  • Google Android Tutorial use this control, though the syntax is a bit difficult. This time, we will need demo.java, main.xml and 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 in the 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=&q uot;true&quot;   5.     android:layout_alignParentLeft=&quot;true&quot; android:gravity=&quot;center_verti cal&quot;   6.     android:spacing=&quot;5&quot; />  <?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?> <Gallery xmlns: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
  • 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
  • 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
  • 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; import android.widget.*; import android.widget.AdapterView.OnItemClickListener; public class example 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 void onItemClick(AdapterView parent, View v, int position, long id) { Toast.makeText(example.this, &quot;&quot; + position, Toast.LENGTH_SHORT).show(); } }); } public class ImageAdapter extends BaseAdapter { public ImageAdapter(Context c) { mContext = c; } public int getCount() { return mImageIds.length; } public Object getItem(int position) { return position; } public long 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 float getAlpha(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 }; } } Ho w 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
  • 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