Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RadioGroup checkedButton property

I am trying to build RadioGroup in Android with one RadioButton checked by default. I'm wondering if this is possible to do through XML, rather than programmatically.

The following code snippet doesn't seem to work as I'm getting an error:

error: Error: No resource found that matches the given name (at 'checkedButton' with value '@id/rdb_positive')

The code is:

<RadioGroup
    style="@style/FormInputField"
    android:orientation="vertical"
    android:checkedButton="@id/rdb_positive"> <!-- Error on this line -->
    <RadioButton
        android:id="@+id/rdb_positive"
        android:text="@string/answer_positive" />
    <RadioButton
        android:id="@+id/rdb_negative"
        android:text="@string/answer_negative" />
</RadioGroup>

It does make sense in a way, as the id of the RadioButton is defined after the attribute in the RadioGroup is set, but then I wonder why there is such attribute available.

like image 606
Ruben Avatar asked Jun 14 '12 08:06

Ruben


3 Answers

Use android:checkedButton="@+id/rdb_positive" ,i think you add + sign then its works

like image 140
Samir Mangroliya Avatar answered Oct 25 '22 07:10

Samir Mangroliya


try this......

<RadioGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <RadioButton
        android:id="@+id/rdb_positive"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:checked="true"
        android:text="answer_positive" />

    <RadioButton
        android:id="@+id/rdb_negative"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="answer_negative" />
</RadioGroup>
like image 2
Anu Avatar answered Oct 25 '22 05:10

Anu


You can get rid of that error by declaring id rdb_positive inside ids.xml and then referencing the id from both RadioGroup and RadioButton elements.

<RadioGroup
    style="@style/FormInputField"
    android:orientation="vertical"
    android:checkedButton="@id/rdb_positive"> <!-- REFERENCE TO ids.xml -->
    <RadioButton
        android:id="@id/rdb_positive" 
        android:text="@string/answer_positive" /> <!-- REFERENCE TO ids.xml -->
    <RadioButton
        android:id="@+id/rdb_negative"
        android:text="@string/answer_negative" />
</RadioGroup>

ids.xml:

<resources>
    <item type="id" name="rdb_positive" />
</resources>
like image 2
Ondřej Z Avatar answered Oct 25 '22 07:10

Ondřej Z