Google Android Tutorial
We will be following exactly same procedure for all our demos. We will hereafter, see xml file and java files only, for the various demos. After a few more demos, it will be enough if I give the 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 of demos. 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 the emulator 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 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).
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/ apk/res/android">
<Spinner android:id="@+id/spinner1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_x="70px" android:layout_y="42px">
</Spinner>
<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="confirm" android:layout_x="70px" android:layout_y="112px"> </Button>
<EditText android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="EditText" android:textSize="18sp" android:layout_x="70px" android:layout_y="182px">
</EditText>
</AbsoluteLayout>The corresponding java source file is given below.
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 = { "sunday", "monday", "tuesday", "wednesday",
"thursday", "friday", "saturday" }; 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:\android\tools\demo\src\mypack\mydemos. Place main.xml in d:\android\tools\demo\res\layout
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:\android\mydemos\ex1.They are intact. So, no problem..The current java and xml files are in d:\android\mydemos\ex2.
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.

- Login or register to post comments
- 60742 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)










Comments
NodakPaul replied on Thu, 2008/06/05 - 8:38am
Great article and tutorial. My company has been digging into Android lately, and you have a lot of helpful links and info for developers who are just starting with it. Thanks!
Musa Musa L replied on Mon, 2008/06/09 - 6:10am
Hello,
Great tutorial.. I am trying to run the example on openSuSE 10.3 with jdk6 update 6 and i keep on getting the following error message
Application error: mypack.mydemos
An error has occured in mypack.mydemos.
Unable to start activity ComponentInfo{mypack.mydemos/mypack.mydemos.demo}:java.lang.ClassCastException:Landroid/widget/TextView;.
What do you think is going on? I followed the steps exactly as you outlined..
I'd appreciate any help
Musa Musa L replied on Tue, 2008/06/10 - 2:15pm
in response to: musamusa
javaslinger replied on Mon, 2008/07/21 - 1:18pm
shanmugavel replied on Tue, 2008/09/09 - 12:51pm
pnero replied on Thu, 2008/10/23 - 4:30pm
line 5: xmlns:android="http://schemas.android.com/ apk/res
the space between .com/ apk causes errors. Just thought I'd throw it out there. Got me puzzled for a while.
Good tutorial though! Very helpful. Need one on databases though; that would be nice :). keep up the good work.
pnero replied on Thu, 2008/10/23 - 4:32pm
joby.nk replied on Mon, 2008/12/29 - 7:02am
Hello,
great tutorial, iam trying to this code in Eclipd IDE , the first two demos working fine,but the Demo-3
throws an error "Ticker.TickerListener cannot be resolved to a type" when we implements the demo class by Ticker.TickerListener . why this error comes ,is there any other library need to link or anythng want to import.Hopfully expecting reply
android_dev replied on Fri, 2009/01/16 - 3:41am
Hi Geetha,
This is Naina. I have recently started working on android. I have written 1 or 2 small applications. I went through the link, [url=http://geeth.ganesan.googlepages.com/android-tutorial]geeth.ganesan - ANDROID-TUTORIAL[/url]. I am trying to use the Spinner application written by you. I tried to run the same Spinner application on the android emulator using Eclipse IDE. But its not working. Iam getting the following exception error.
The application spin_and(process com.example.android.spin_and) has stopped unexpectedly. Please try again. Force Close.
I am not understanding where is the problem. I did not get any errors also. Please help me out.
Thanks,
Naina
rakronney replied on Tue, 2009/03/03 - 3:34pm
vhtmobile replied on Wed, 2009/04/08 - 8:45am
in response to: joby.nk
vhtmobile replied on Wed, 2009/04/08 - 8:49am
rajeshvijayanad... replied on Thu, 2009/07/02 - 6:23am
Hi,
I had creates a gallery with some images and its working fine for me.
Is there any way to put a seeker in the window for gallery.Is there any way to start a slide show(with gallery) when startActivity?
Please give me some suggestions...
Thanks And Regards,
Rajesh.V