Android – RadioGroup

Radio Button in Android apps is very common. In this tutorial, we’ll implement the Android radio button widget in our application. Radio Buttons are used when we need to select only one item from a list of presented items.

Android Radio Button

A radio button consists of two states – checked and unchecked. Clicking an unchecked button changes its state to “checked” state and “unchecked” for the previously selected radio button. To toggle a checked state to an unchecked state, we need to choose another item. Clicking a checked state button doesn’t do any good. A RadioGroup is a set of radio buttons. Radio Buttons that have different parent ViewGroup can’t be termed as a RadioGroup.

Step 1: First one to  Start Android Studio

Step 2 :  Seconds step to Create a New Project Project ClickOn  ==> File  ==> NEW ==> New Project

Step 3: After create on your project open your java file and XML file and you can just cope with the code and paste on your file and run your project.

Step 4:  Open Your XML file.

open your XML file and copy-paste the code in your XML file.

<RadioGroup
    android:id="@+id/radio"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="10dp"
    android:layout_marginLeft="20dp"
    android:orientation="horizontal">

    <RadioButton
        android:id="@+id/radioMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorPrimaryDark"
        android:textAppearance="?android:textAppearanceMedium"
        android:buttonTint="@color/colorPrimaryDark"
        android:text="Male"
        android:checked="true" />

    <RadioButton
        android:id="@+id/radioFemale"
        android:layout_width="wrap_content"
        android:textAppearance="?android:textAppearanceMedium"
        android:layout_height="wrap_content"
        android:buttonTint="@color/colorPrimaryDark"
        android:textColor="@color/colorPrimaryDark"
        android:text="Female" />
</RadioGroup>
<Button
    android:id="@+id/RegBtn"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/button_shap"
    android:text="Registration"
    android:textAppearance="?android:textAppearanceLarge"
    android:textColor="@color/white"
    android:layout_margin="30dp"/>

Android Radio Button attributes

Some of the attributes of an android radio button and radio group are listed below:

  1. android:orientation : This property on the Radio group defines the orientation to position its child view consisting of Radio Buttons. It can be either horizontal or vertical
  2. check(id) : This sets the selection to the radio button whose identifier is passed in parameter. -1 is used as the selection identifier to clear the selection
  3. clearCheck() : It clears the selection. When the selection is cleared, no radio button in this group is selected and getCheckedRadioButtonId() returns null
  4. getCheckedRadioButtonId() : It returns the identifier of the selected radio button in this group. If its empty selection, the returned value is -1
  5. setOnCheckedChangeListener() : This registers a callback to be invoked when the checked radio button changes in this group. We must supply an instance of RadioGroup.OnCheckedChangeListener to setOnCheckedChangeListener() method

 

Step 5:- Open Your java file and implement it 

public class Registration extends AppCompatActivity implements View.OnClickListener {
   
    private RadioGroup radioGroup;
    private RadioButton radioButton;
    Button SendBtn;
    String Gender;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);

        radioGroup = (RadioGroup) findViewById(R.id.radio);

        SendBtn=(Button)findViewById(R.id.RegBtn);
        SendBtn.setOnClickListener(this);

    }
@Override
public void onClick(View v) {
    int selectedId = radioGroup.getCheckedRadioButtonId();
    // find the radiobutton by returned id
    radioButton = (RadioButton) findViewById(selectedId);
   Gender=radioButton.getText().toString().trim();
    }
}

You may also like...