Android Radio Buttons example

How to create Radio Buttons in Android and check current selection

In this example we will learn the following
  • Create Radio buttons using XML layout resource 
  • Set the default Radio button in the XML layout 
  • Set the default Radio button programmatically 
  • Find the currently selected Radio Button option
Radio buttons are grouped together using the Radio Group option, only one Radio button within the Group can stay selected at a given time. If the user clicks on a Radio button all other Radio buttons in the Group are automatically deselected.

Android Radio Button example
Android Radio Button example

Source for the Screen Layout - main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="@string/hello"
        android:textSize="20sp" />

    <RadioGroup
        android:id="@+id/osGroup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/selectAndroid"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="Android" />

        <RadioButton
            android:id="@+id/selectIOS"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="iOS" />

        <RadioButton
            android:id="@+id/selectWindows"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Windows" />
        
        <RadioButton
            android:id="@+id/selectRIM"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RIM" />
    </RadioGroup>
    
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="Click here to see Results" />


    <TextView
        android:id="@+id/responseText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

Source for the Activity - AndroidRadioButtonsActivity.java

package com.as400samplecode;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class AndroidRadioButtonsActivity extends Activity {

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  RadioButton iOS = (RadioButton) findViewById(R.id.selectIOS);
  iOS.setChecked(true);
  checkButtonClick();

 }

 private void checkButtonClick() {

  Button myButton = (Button) findViewById(R.id.button1);
  myButton.setOnClickListener(new OnClickListener() {

   @Override
   public void onClick(View v) {

   //get selected radio button from radioGroup
   RadioGroup radioGroup = (RadioGroup) findViewById(R.id.osGroup);
   int selectedId = radioGroup.getCheckedRadioButtonId();

   //find the radio button by returned id
   RadioButton osButton = (RadioButton) findViewById(selectedId);

   StringBuffer responseText = new StringBuffer();
   responseText.append("The following Radio button is selected...\n");
   responseText.append(osButton.getText());

   TextView myTextView = (TextView) findViewById(R.id.responseText);
   myTextView.setText(responseText);

   }
  });

 }
}

No comments:

Post a Comment

NO JUNK, Please try to keep this clean and related to the topic at hand.
Comments are for users to ask questions, collaborate or improve on existing.