Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set text to RadioButton which is inside of a RadioGroup programmatically?

Tags:

java

android

I have a RadioGroup inside of which I have some RadioButtons.

I want to set some text on the RadioButtons programmatically. I used following code for that using that I'm unable to access the RadioButtons inside of the RadioGroup using Java.

How can I set the text on a RadioButton inside a RadioGroup?

XML Layout:

<RadioGroup android:layout_width="fill_parent"
    android:layout_marginLeft="20dip"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:id="@+id/QueGroup1">

    <RadioButton android:checked="false"
        android:button="@drawable/green"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:textColor="#000"
        android:text="Yes"
        android:id="@+id/rbtnYes"
        android:visibility="gone" />

    <RadioButton android:checked="false"
        android:button="@drawable/red"
        android:textColor="#000"
        android:text="No"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent" 
        android:id="@+id/rbtnNo"
        android:visibility="gone" />

    <RadioButton android:checked="false"
        android:button="@drawable/red"
        android:textColor="#000"
        android:text="Dont Know"
        android:layout_height="wrap_content"
        android:layout_width="fill_parent"
        android:id="@+id/rbtnDontKnow"
        android:visibility="gone" />
</RadioGroup>

Java code:

private void fnRadioTextSet(String strval) {
    rbtnYes = (RadioButton)findViewById(R.id.rbtnYes);
    rbtnNo = (RadioButton)findViewById(R.id.rbtnNo);
    rbtnDontKnow = (RadioButton)findViewById(R.id.rbtnDontKnow);
    RadioGroup rbtnGrp = (RadioGroup)findViewById(R.id.QueGroup1);
    String[] strArrtext = strval.split(",");
    for (int intcount = 0; intcount < strArrtext.length; intcount++) {
        rbtnGrp.getChildAt(intcount).settext("test");
    }

    //int i = rbtnGrp.getChildCount();
    //Toast.makeText(getApplication(), rbtnGrp.getChildCount(),
    //      Toast.LENGTH_LONG).show();
    /*String[] strtext = strval.split(",");
    if (strtext.length > 0) {

    }*/
}
like image 584
Rajkumar Reddy Avatar asked Jun 21 '11 13:06

Rajkumar Reddy


People also ask

How do you programmatically determine whether a Radiobutton is checked?

You can check the current state of a radio button programmatically by using isChecked() method. This method returns a Boolean value either true or false. if it is checked then returns true otherwise returns false.

How can I get Radiobutton text in Android?

To get the selected radio button, we have used radioGroup. getCheckedRadioButtonId() method, which returns the id of the selected radio button. Then to get the text of the selected radio button, we have used getText() method on that selected radio button.

How do I get radio buttons side by side on Android?

Use the android:orientation="horizontal" -attribute of the RadioGroup.


2 Answers

private void fnRadioTextSet(String strval) {
    RadioGroup rbtnGrp = (RadioGroup)findViewById(R.id.QueGroup1);
    String[] strArrtext = strval.split(",");
    for (int i = 0; i < rbtnGrp.getChildCount(); i++) {
        ((RadioButton) rbtnGrp.getChildAt(i)).setText(strArrtext[i]);
    }
}
like image 78
mish Avatar answered Oct 26 '22 07:10

mish


Don't need the loop

rbtnYes.settext("sdklfhjsdf");
rbtnNo.settext("test");
rbtnDontKnow.settext("test");
like image 31
PedroAGSantos Avatar answered Oct 26 '22 08:10

PedroAGSantos