Question Detail

How to use RadioGroup in android and get Radiobutton's Value after that how can set selected (Checked) on last selected RadioButton With any example


Thread Reply

Hemant Sharma

- 11 months ago

Global Define RadioButton Object for Access in whole Class

1.) Below code is for getting Value of Selected RadioButton of RadioGroup

RadioButton ganderButton;
RadioGroup ganderRadioGroup;

ganderRadioGroup = (RadioGroup) view.findViewById(R.id.gander_radioGroup_ID);
int selectedId = ganderRadioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
ganderButton = (RadioButton) view.findViewById(selectedId);
String strResult=ganderButton.getText().toString();

2.) Below code is for set value on RadioButton which is selected last time or you want to select default

2.1) by XML: 

<RadioGroup
    android:id="@+id/gander_radioGroup_ID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male"
        android:buttonTint="#187FCA"
        />
    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:buttonTint="@color/colorPrimary"
        android:gravity="center"
        android:text="Female" />

</RadioGroup>

2.1) In Java Class select radio according to condition : 

String ganderValue="male"; //put your condition here and get dynamic value

  if(ganderValue.equals("male")){
    ((RadioButton)ganderRadioGroup.getChildAt(0)).setChecked(true);
}else{
        ((RadioButton)ganderRadioGroup.getChildAt(1)).setChecked(true);
    }

Hemant Sharma

- 11 months ago

Global Define RadioButton Object for Access in whole Class

1.) Below code is for getting Value of Selected RadioButton of RadioGroup

RadioButton ganderButton;
RadioGroup ganderRadioGroup;

ganderRadioGroup = (RadioGroup) view.findViewById(R.id.gander_radioGroup_ID);
int selectedId = ganderRadioGroup.getCheckedRadioButtonId();
// find the radiobutton by returned id
ganderButton = (RadioButton) view.findViewById(selectedId);
String strResult=ganderButton.getText().toString();

2.) Below code is for set value on RadioButton which is selected last time or you want to select default

2.1) by XML: 

<RadioGroup
    android:id="@+id/gander_radioGroup_ID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:orientation="horizontal">
    <RadioButton
        android:id="@+id/radio1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="Male"
        android:buttonTint="#187FCA"
        />
    <RadioButton
        android:id="@+id/radio2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:buttonTint="@color/colorPrimary"
        android:gravity="center"
        android:text="Female" />

</RadioGroup>

2.1) In Java Class select radio according to condition : 

String ganderValue="male"; //put your condition here and get dynamic value

  if(ganderValue.equals("male")){
    ((RadioButton)ganderRadioGroup.getChildAt(0)).setChecked(true);
}else{
        ((RadioButton)ganderRadioGroup.getChildAt(1)).setChecked(true);
    }

Anonymous

- 11 months ago

private RadioGroup radioGroup;
  private RadioButton radioButton;
  private Button btnDisplay;

 

 

public void addListenerOnButton() {

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

    btnDisplay.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

                // get selected radio button from radioGroup
            int selectedId = radioGroup.getCheckedRadioButtonId();

            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);

            Toast.makeText(MyAndroidAppActivity.this,
                radioButton.getText(), Toast.LENGTH_SHORT).show();

        }

    });

  }