Google Android Tutorial

Demo 5 - RadioButtons

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

To be placed in tools\demo\res\layout

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

The following Java code should be placed in tools\demo\src\mypack\mydemo

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;

	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("radio");

		label1 = (TextView) findViewById(R.id.label1);

		label1.setText("where?");

		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("madras");
				}

				if (radio2.isChecked())

				{
					text1.setText("bombay");
				}

			}

		}

	}

	// ----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:\android\mydemos.

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 use this control, though the syntax is a bit difficult. This time, we will need demo.java, main.xml and also another folder in tools\demo\res\drawable. 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.

 

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

 

The following class is to be placed in tools\demo\mypack\mydemos

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, "" + 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
		};

	}

}

How to uninstall an application from the emulator?

  • Make sure your emulator is running
  • Open a dos box in the android/tools folder  d:\android\tools>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

 

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

 

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

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

Comments

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

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

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

Hello,

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

Application error: mypack.mydemos

An error has occured in mypack.mydemos.

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

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

I'd appreciate any help 

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

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

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

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

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

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

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

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

 

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

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

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

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

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

Hello,

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

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

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

Hi Geetha,

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

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

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


Thanks,

Naina

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

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

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

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

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

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

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

Hi,

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

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

Please give me some suggestions...

 

Thanks And Regards,

Rajesh.V

Comment viewing options

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