What is radiobutton

Android RadioButton is a GUI widgets that permits the user to choose only one option from given set of options.

RadioButton is generally used with RadioGroup.It is container for RadioButton and make help to select only one option at a time and other become unselected.

RadioButton provide the facility of single choice i.e your gender like male and female where you can use the RadioButton for making choice in android application.

The general use of RadioButton you see in the form that you fill for job where you find the option like gender male and female and where you need the select the one option according to your gender in form.Only one option you can select their.


RadioButton:.

In this section i am going to show the RadioButton with practical example of gender like male and female selection.

First open the android studio and create a project and give the name of your project like CheckboxDemo.

The screenshots of the program is as follows:

Smiley face Smiley face

Drag and drop the one RadioGroup , two RadioButton , two TextView and one Button. Open the activity_main.xml file and code look like this


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.example.niitpc.myapplication.MainActivity">


    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="78dp"
        android:layout_marginStart="78dp"
        android:layout_marginTop="49dp"
        android:id="@+id/radioGroup">

        <RadioButton
            android:id="@+id/radioButton6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="male" />

        <RadioButton
            android:id="@+id/radioButton7"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="female" />
    </RadioGroup>

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="59dp"
        android:layout_marginStart="59dp"
        android:text="Select gender:" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioGroup"
        android:layout_alignParentTop="true"
        android:layout_alignStart="@+id/radioGroup"
        android:layout_marginLeft="13dp"
        android:layout_marginStart="53dp"
        android:layout_marginTop="200dp"
        android:onClick="onClickButton"
        android:text="Button" />
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="13dp"
        android:layout_marginStart="175dp"
        android:text="Selected gender" />
</RelativeLayout>


       

Open the MainActivity.java file and write code look like this.

Here in the program you select the RadioButton and click on the Button it wil dislay the selected gender in TextView.


package com.example.niitpc.myapplication;
import android.media.MediaPlayer;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.MediaController;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


   RadioGroup radioGroup;
   RadioButton gender;
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        radioGroup=(RadioGroup) findViewById(R.id.radioGroup);
        textView=(TextView)findViewById(R.id.textView2);

    }
    public void onClickButton(View v)
    {
        int selectedid=radioGroup.getCheckedRadioButtonId();
        gender=(RadioButton)findViewById(selectedid);

        textView.setText(gender.getText().toString());


    }

}

       

Explanation:

RadioGroup radioGroup:This statement create the object radioGroup of RadioGroup.

RadioButton gender:This statement create the object gender of RadioButton.

radioGroup=(RadioGroup) findViewById(R.id.radioGroup):This statement initialized the genderGroup object.

textView=(TextView)findViewById(R.id.textView2):This statement initialized the textView object.

After initialization i have to created the onClickButton().

int selectedid=radioGroup.getCheckedRadioButtonId();This statement initialized the selectedid variable with selected RadioButton ID with the help of getCheckedRadioButtonId().

gender=(RadioButton)findViewById(selectedid):This statement initialized the gender object with selected RadioButtonID.

textView.setText(gender.getText().toString()):This statement set the selected RadioButton Text into textView object.

You do not forget to set the android:onClick="onClickButton".If you do not set this property button will not perform any action .


Radio Button

Click here what is RadioButton for video

Smiley face                     Smiley face