Sunday, November 22, 2009

RadioGroup and RadioButton

In this exercise, RadioGroup and RadioButton will be described.



A radio button is a two-states button that can be either checked or unchecked. When the radio button is unchecked, the user can press or click it to check it. A radio button cannot be unchecked by the user once checked.

Radio buttons are normally used together in a RadioGroup. When several radio buttons live inside a radio group, checking one radio button unchecks all the others.

RadioGroup is used to create a multiple-exclusion scope for a set of radio buttons. Checking one radio button that belongs to a radio group unchecks any previously checked radio button within the same group.

Intially, all of the radio buttons are unchecked. While it is not possible to uncheck a particular radio button, the radio group can be cleared to remove the checked state.

main.xml with three RadioButton inside a RadioGroup.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  >
 <RadioGroup
  android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:orientation="vertical">
   <RadioButton
    android:id="@+id/option1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 1" />
   <RadioButton
    android:id="@+id/option2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 2" />
   <RadioButton
    android:id="@+id/option3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Option 3" />
 </RadioGroup>
</LinearLayout>


AndroidRadioGroup.java
package com.exercise.AndroidRadioGroup;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

public class AndroidRadioGroup extends Activity {
 
 RadioButton myOption1, myOption2, myOption3;
 
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      myOption1 = (RadioButton)findViewById(R.id.option1);
      myOption2 = (RadioButton)findViewById(R.id.option2);
      myOption3 = (RadioButton)findViewById(R.id.option3);
      myOption1.setOnClickListener(myOptionOnClickListener);
      myOption2.setOnClickListener(myOptionOnClickListener);
      myOption3.setOnClickListener(myOptionOnClickListener);
      myOption1.setChecked(true);
  }

  RadioButton.OnClickListener myOptionOnClickListener =
   new RadioButton.OnClickListener()
  {

  @Override
  public void onClick(View v) {
   // TODO Auto-generated method stub
   Toast.makeText(AndroidRadioGroup.this,
     "Option 1 : " + myOption1.isChecked() + "\n"+
     "Option 2 : " + myOption2.isChecked() + "\n" +
     "Option 3 : " + myOption3.isChecked(),
     Toast.LENGTH_LONG).show();

  }
   
   
  };
}


Download the files.

Related: Read index of the checked RadioButton in RadioGroup.

3 comments:

Marcos Paulo said...

Muito Obrigado...

JerunG said...

how i need to change true false in view. and need show 1 output only when i check on it.

Дмитрий said...

Check this link -
http://stackoverflow.com/questions/2740169/radio-button-selection-changes-toast-on-android

Your code is not the best implementation of RadioGroup (you don't use RadioGroup in proper way =) )