Studyon Minte9.com
Android




Radio Buttons



1) Click Events

To create each radio button option, create a RadioButton in your layout. 
However, because radio buttons are mutually exclusive, you must group them together inside a RadioGroup.

	%--- activity_main.xml

	<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
	    android:orientation="vertical"
	    android:layout_width="fill_parent"
	    android:layout_height="wrap_content">
		<RadioButton android:id="@+id/radio_male"
		    android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/male"
			android:onClick="onRadioButtonClicked"
		    />
		<RadioButton android:id="@+id/radio_female"
		    android:layout_width="wrap_content"
			android:layout_height="wrap_content"
			android:text="@string/female"
			android:onClick="onRadioButtonClicked"
		    />
	</RadioGroup>


The RadioGroup is a subclass of LinearLayout that has a vertical orientation by default.


2) Toggle

If you need to change the radio button state yourself (such as when loading a saved CheckBoxPreference), 
use the setChecked(boolean) or toggle() method.

	%--- MainActivity.java

	package com.example.myapp;
	import android.app.Activity;
	import android.os.Bundle;
	import android.view.View;
	import android.widget.RadioButton;

	public class MainActivity extends Activity {
		     
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		RadioButton radio1 = (RadioButton) findViewById(R.id.radio_female);
		radio1.setChecked(true);
	    }
	    
	    public void onRadioButtonClicked(View view) {
		
		boolean checked = ((RadioButton) view).isChecked();
		
		switch(view.getId()) {
			
			case R.id.radio_male:
				break;
				
			case R.id.radio_female:
				break;
		
		}
	    }
	}


http://developer.android.com/guide/topics/ui/controls/radiobutton.html