Android Tutorial: Radio Button

In this android tutorial for beginners we are going to use radio button. Below is a simple example which makes use of android radio button. 
We have used an EditText for the user to enter name then two radio buttons to select sex and then a submit button. When the user clicks the submit button the User's name and sex is displayed in the TextView below the submit button.
How to use Android Radio Button
  1. drag and drop a radioGroup container from containers in palette.
  2. inside the radio group drag and drop two radio buttons and name them male and female.
  3. Then place a button below the radioGroup container and name it submit.
Here is the complete XML code for this radio button in android example.

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout 

 xmlns:android="http://schemas.android.com/apk/res/android" 
 xmlns:tools="http://schemas.android.com/tools" 
 xmlns:app="http://schemas.android.com/apk/res-auto" 
 android:layout_width="match_parent" 
 android:layout_height="fill_parent" 
 android:paddingLeft="@dimen/activity_horizontal_margin" 
 android:paddingRight="@dimen/activity_horizontal_margin" 
 android:paddingTop="@dimen/activity_vertical_margin" 
 android:paddingBottom="@dimen/activity_vertical_margin" 
 app:layout_behavior="@string/appbar_scrolling_view_behavior" 
 tools:showIn="@layout/activity_main" 
 tools:context="com.example.saleem.myapplication.MainActivity">

    <RadioGroup 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:id="@+id/radioGroup" 
 android:layout_below="@+id/textView2" 
 android:layout_alignParentLeft="true" 
 android:layout_alignParentStart="true" 
 android:layout_marginTop="58dp">

        <RadioButton 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"
             android:text="Male" 
 android:id="@+id/radioButton" 
 android:checked="false" />

        <RadioButton 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Female" 
 
 android:id="@+id/radioButton2" 
 android:checked="false" />
    </RadioGroup>

    <Button 
 style="?android:attr/buttonStyleSmall" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="Submit" 
 android:id="@+id/button"  
 android:layout_centerVertical="true" 
 android:layout_centerHorizontal="true" 
 android:onClick="onSubmitButtonClick" />

    <EditText 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_alignParentTop="true" 
 android:layout_alignParentLeft="true" 
 android:layout_alignParentStart="true" 
 android:layout_alignRight="@+id/button" 
 android:layout_alignEnd="@+id/button" 
 android:hint="Name" /> 
 <TextView 
         android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:textAppearance="?android:attr/textAppearanceLarge" 
 android:text="Sex"        android:id="@+id/textView2" 
 android:layout_below="@+id/editText" 
 android:layout_alignParentLeft="true" 
 android:layout_alignParentStart="true" 
 android:layout_marginTop="50dp" />

</RelativeLayout>
java code
public class MainActivity extends AppCompatActivity {
    private RadioGroup radioGroup;
    private RadioButton radioButton;
    // EditText for user to enter name    private EditText nameTextField;



    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        nameTextField = (EditText)findViewById(R.id.editText);

    }
    public void onSubmitButtonClick(View view){
        radioGroup = (RadioGroup)findViewById(R.id.radioGroup);
        int selectedOption = radioGroup.getCheckedRadioButtonId();
        radioButton = (RadioButton)findViewById(selectedOption);
        String name = nameTextField.getText().toString();
        String sex = radioButton.getText().toString();
        Toast.makeText                (MainActivity.this, "name is " +name +"and sex is" +sex,Toast.LENGTH_SHORT)
                .show();

    }
}
we have created a method named onSubmitButtonClick when the method is called it casts radioGroup.
then it creates an int variable named selectedOption to store the id of the checked radio button in it.
Then it casts radioButton and in the findViewById(selectedOption)variable is passed. So the checked
radio button is assigned to the radioButton variable. String name is used to get the text entered in
the editText field and String sex is assigned, the text it gets from the radioButton.
And finally toast is used to display the name and checked sex of the user. 
 
Android Radio Button example
 
 
 

0 comments: