Articles → Android → Radiobutton In Android

Radiobutton In Android






What Is Radiobutton?





Radiobutton Groups




Picture showing how radiobutton groups put the radiobuttons inside it
Click to Enlarge


Example




  1. Create a UI with radio button group, 2 radio buttons (male and female).
  2. Picture showing adding 2 radiobuttons inside the constraint layout
    Click to Enlarge

  3. On each radio button, add an onclick method.
  4. Add RadioButtonClicked event in Java file
  5. public void RadioButtonClicked(View view) {
      RadioButton radioButton = (RadioButton) view;
      if (radioButton.isChecked()) {
        if (radioButton.getId() == R.id.maleRadioButton)
          Toast.makeText(this, "Male Radio Button Clicked", Toast.LENGTH_LONG).show();
        else if (radioButton.getId() == R.id.femaleRadioButton)
          Toast.makeText(this, "Female Radio Button Clicked", Toast.LENGTH_LONG).show();
      }
    }



Output


Picture showing the output of radio buttons in android
Click to Enlarge


Posted By  -  Karan Gupta
 
Posted On  -  Tuesday, November 19, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250