How to check if a radiobutton is checked in a radiogroup in Android?

I need to set validation that user must fill / select all details in a page. If any fields are empty wanna show Toast message to fill. Now I need set validation for RadioButton in the RadioGroup. I tried this code but didn’t work properly. Suggest me correct way. Thankyou.

// get selected radio button from radioGroup
int selectedId = gender.getCheckedRadioButtonId();
// find the radiobutton by returned id
selectedRadioButton = (RadioButton)findViewById(selectedId);
// do what you want with radioButtonText (save it to database in your case)
radioButtonText = selectedRadioButton.getText().toString();

if(radioButtonText.matches(""))
{
    Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
    Log.d("QAOD", "Gender is Null");
}
else
{
    Log.d("QAOD", "Gender is Selected");
}

  • Move layouts up when soft keyboard is shown?
  • how to keep all methods in a class with ProGuard
  • StaggeredGridLayoutManager reorders items
  • Android background with gradient and tiled image
  • Android: how do I check if activity is running?
  • Android Toast Won't Disappear
  • Related posts:

    How to configure the Android Emulator to show onscreen buttons like the new Galaxy Nexus?
    Opening a File from assets folder in android
    What methods are invoked in the Activity Lifecycle in the following cases:
    Robolectric vs Android Test Framework
    How to change text size in places autocompletefragment in android?
    Looking for a cross-platform method for basic 2D/3D graphics for iOS and Android
  • Android ContentProvider and Google IO Rest Talk
  • Unmarshalling XML files into Java objects in Android?
  • Can't close NavigationDrawer with right-to-left swipe
  • Not Getting Thumb with Genres - Universal Music Player
  • Android - How to programmatically set button color
  • setSelection not changing ListView position
  • 6 Solutions collect form web for “How to check if a radiobutton is checked in a radiogroup in Android?”

    If you want to check on just one RadioButton you can use the isChecked function

    if(radioButton.isChecked())
    {
      // is checked    
    }
    else
    {
      // not checked
    }
    

    and if you have a RadioGroup you can use

    if (radioGroup.getCheckedRadioButtonId() == -1)
    {
      // no radio buttons are checked
    }
    else
    {
      // one of the radio buttons is checked
    }
    

    all you need to do is use [getCheckedRadioButtonId()][1] and isChecked() method,

    if(gender.getCheckedRadioButtonId()==-1)
    {
        Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
    }
    else
    {
        // get selected radio button from radioGroup
    int selectedId = gender.getCheckedRadioButtonId();
    // find the radiobutton by returned id
    selectedRadioButton = (RadioButton)findViewById(selectedId);
        Toast.makeText(getApplicationContext(), selectedRadioButton.getText().toString()+" is selected", Toast.LENGTH_SHORT).show();
    }
    

    https://developer.android.com/guide/topics/ui/controls/radiobutton.html

    Use the isChecked() function for every radioButton you have to check.

    RadioButton maleRadioButton, femaleRadioButton;
    
    maleRadioButton = (RadioButton) findViewById(R.id.maleRadioButton);
    femaleRadioButton = (RadioButton) findViewById(R.id.femaleRadioButton);
    

    Then use the result for your if/else case consideration.

    if (maleRadioButton.isChecked() || femaleRadioButton.isChecked()) {
         Log.d("QAOD", "Gender is Selected");
    } else {
        Toast.makeText(getApplicationContext(), "Please select Gender", Toast.LENGTH_SHORT).show();
        Log.d("QAOD", "Gender is Null");
    }
    

    Try to use the method isChecked();

    Like,

    selectedRadioButton.isChecked() -> returns boolean.

    Refere here for more details on Radio Button

    try to use this

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"       
        android:orientation="horizontal"
    >    
        <RadioButton
            android:id="@+id/standard_delivery"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Standard_delivery"
            android:checked="true"
            android:layout_marginTop="4dp"
            android:layout_marginLeft="15dp"
            android:textSize="12dp"
            android:onClick="onRadioButtonClicked"   
        />
    
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/Midnight_delivery"
            android:checked="false"
            android:layout_marginRight="15dp"
            android:layout_marginTop="4dp"
            android:textSize="12dp"
            android:onClick="onRadioButtonClicked"
            android:id="@+id/midnight_delivery"
        />    
    </RadioGroup>
    

    this is java class

    public void onRadioButtonClicked(View view) {
            // Is the button now checked?
            boolean checked = ((RadioButton) view).isChecked();
    
            // Check which radio button was clicked
            switch(view.getId()) {
                case R.id.standard_delivery:
                    if (checked)
                        Toast.makeText(DishActivity.this," standard delivery",Toast.LENGTH_LONG).show();
                        break;
                case R.id.midnight_delivery:
                    if (checked)
                        Toast.makeText(DishActivity.this," midnight delivery",Toast.LENGTH_LONG).show();
                        break;
            }
        }
    

    I used the id’s of my radiobuttons to compare with the id of the checkedRadioButton that I got using mRadioGroup.getCheckedRadioButtonId()

    Here is my code:

    mRadioButton1=(RadioButton)findViewById(R.id.first);
    mRadioButton2=(RadioButton)findViewById(R.id.second);
    mRadioButton3=(RadioButton)findViewById(R.id.third);
    mRadioButton4=(RadioButton)findViewById(R.id.fourth);
    
    mNextButton=(Button)findViewById(R.id.next_button);
    
    mNextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            int selectedId=mRadioGroup.getCheckedRadioButtonId();
    
            int n=0;
            if(selectedId==R.id.first){n=1;}
    
            else if(selectedId==R.id.second){n=2;}
    
            else if(selectedId==R.id.third){n=3;}
    
            else if(selectedId==R.id.fourth){n=4;}
            //. . . .
        }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.