How to get the text from radio button in a radio group when radio button checked state is changed
Asked Answered
S

7

9

I have created two radio buttons in a radio group dynamically and one of them is checked. I need when i cheked another button then its value should be saved in string. But i have implemented checkedchangelistener for this.But its not working first time. Here is my code.

rg = ((RadioGroup)getActivity().findViewById(alist_id.get(i)));
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
    public void onCheckedChanged(RadioGroup rd, int checkedId) {
        for(int i=0; i<rd.getChildCount(); i++) {
            radio_button = (RadioButton) rd.getChildAt(i);
            int id = radio_button.getId();
            if(radio_button.getId() == checkedId) {
                text = radio_button.getText().toString();
                flag=true;
                System.out.println("trueeeeeeeee"+text);
                return;
            }
        }
    }
});
if (flag==true) {
    updated_list.add(text);
    System.out.println("sssssssssssssssssss");
}else {
    updated_list.add(data_from_list_view.get(i));
    System.out.println("falseeeeeee");
}
Spittle answered 22/11, 2013 at 6:27 Comment(0)
J
10

xml file

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<RadioGroup
    android:id="@+id/radioSex"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <RadioButton
        android:id="@+id/radioMale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_male" 
        android:checked="true" />

    <RadioButton
        android:id="@+id/radioFemale"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/radio_female" />

</RadioGroup>

<Button
    android:id="@+id/btnDisplay"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btn_display" />

</LinearLayout>

java code

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MyAndroidAppActivity extends Activity {
    private RadioGroup radioSexGroup;
    private RadioButton radioSexButton;
    private Button btnDisplay;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    addListenerOnButton();

    }

    public void addListenerOnButton() {

        radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
        btnDisplay = (Button) findViewById(R.id.btnDisplay);

        btnDisplay.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

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

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

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

            }

        });

    }
}
Jesselton answered 22/11, 2013 at 6:59 Comment(2)
Is there can be problem due to selected radio button?Spittle
For this answer, you must have to checked RadioButton Id by using attribute android:checkedButton="@+id/radioMale" in RadioGroup. If you will click on button directly then it may get error NullPointerExceptionDemerit
S
6

try following code

radiogroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            int id=group.getCheckedRadioButtonId();
            RadioButton rb=(RadioButton) findViewById(id);
                                     OR
            RadioButton rb=(RadioButton) findViewById(checkedId);

            String radioText=rb.getText().toString();

        }
    });
Subglacial answered 22/11, 2013 at 6:34 Comment(2)
I have created dynamically radio button as follow:-Spittle
final RadioButton[] rb = new RadioButton[list_of_radiobutton_data.size()]; rg = new RadioGroup(getActivity()); rg.setId(i); rg.setOrientation(RadioGroup.VERTICAL); for (int i1 = 0; i1 < list_of_radiobutton_data.size(); i1++) { rb[i1] = new RadioButton(getActivity()); rb[i1].setText(list_of_radiobutton_data.get(i1)); rb[i1].setId(i1); rg.addView(rb[i1]); if (rb[i1].getText().equals(data_from_list_view.get(i))) { /*rb[i1].setChecked(true);*/ rg.check(rb[i1].getId()); } }Spittle
B
4

Kotlin code for retrieving text from a checked RadioButton in RadioGroup

    binding.genderRadiogroup.setOnCheckedChangeListener { _, _ ->
        var id: Int = binding.genderRadiogroup.checkedRadioButtonId
        var rb: RadioButton = binding.genderRadiogroup.findViewById(id)
        Toast.makeText(this,rb.text.toString(),Toast.LENGTH_SHORT).show()
    }
Badenpowell answered 12/6, 2021 at 5:51 Comment(0)
G
3

try this...

    radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
            String text = radioButton.getText().toString();
            updated_list.add(text);
        }
    });
Goran answered 22/11, 2013 at 6:43 Comment(4)
Is this code is correct ? which changes i have to implement for calling first time checked change listener?Spittle
Radiogroup.checkedChangeListener is not calling first time.Spittle
code written in comments section is not readable. just edit your answer and write there...Goran
final RadioButton[] rb = new RadioButton[list_of_radiobutton_data.size()]; rg = new RadioGroup(getActivity()); rg.setId(i); rg.setOrientation(RadioGroup.VERTICAL); for (int i1 = 0; i1 < list_of_radiobutton_data.size(); i1++) { rb[i1] = new RadioButton(getActivity()); rb[i1].setText(list_of_radiobutton_data.get(i1)); rb[i1].setId(i1); rg.addView(rb[i1]); if (rb[i1].getText().equals(data_from_list_view.get(i))) { rb[i1].setChecked(true); /*rg.check(rb[i1].getId());*/ } }Spittle
P
0

Try this solution for retrieving the text:

radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

     void onCheckedChanged(RadioGroup rg, int checkedId) {
          for (int i = 0; i < rg.getChildCount(); i++) {
               RadioButton btn = (RadioButton) rg.getChildAt(i);
               if (btn.getId() == checkedId) {
                   String text = btn.getText();
                   // do something with text
                   return;
               }
          }
     }
});
Porterfield answered 22/11, 2013 at 6:29 Comment(5)
Is there can be problem due to selected radio button?Spittle
@Spittle Have you set radiogroup.check(IdOfYourButton), coz in this case your radio button is selected by default so in that case listener will not get called the first time.Porterfield
AndroidWarrior No, i haven't..At the time of creation i have to set ?Spittle
I am creating radio button dynamically by using this code final RadioButton[] rb = new RadioButton[list_of_radiobutton_data.size()]; rg = new RadioGroup(getActivity()); rg.setId(i); rg.setOrientation(RadioGroup.VERTICAL); for (int i1 = 0; i1 < list_of_radiobutton_data.size(); i1++) { rb[i1] = new RadioButton(getActivity()); rb[i1].setText(list_of_radiobutton_data.get(i1)); rb[i1].setId(i1); rg.addView(rb[i1]); if (rb[i1].getText().equals(data_from_list_view.get(i))) { rb[i1].setChecked(true); } }Spittle
Is this code is correct ? which changes i have to implement for calling first time checked change listener?Spittle
H
0

Kotlin version for the getting text from the RadioGroup in the Fragment be like:

private lateinit var genderRadioGroup: RadioGroup

genderRadioGroup = rootView.gender_radio_grp

val selectedRadioButton: Int = genderRadioGroup.checkedRadioButtonId
        radioButton = rootView.findViewById(selectedRadioButton)
        Toast.makeText(context, "Selected gender is ${radioButton.text}", Toast.LENGTH_SHORT).show()

(P.S. I'm using Android Extension to get the reference of the view )

Hrutkay answered 21/4, 2021 at 6:31 Comment(0)
T
0
radioGroup.setOnCheckedChangeListener { group, checkedId ->
            group.findViewById<AppCompatRadioButton>(checkedId)?.let { radioButton ->
                val textFromButtonLabel = radioButton.text.toString()
            }
        }
Timothy answered 24/5, 2021 at 17:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.