RadioGroup checkedButton property
Asked Answered
C

4

15

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.

Cavalla answered 14/6, 2012 at 8:47 Comment(0)
T
26

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

Touchy answered 14/6, 2012 at 8:50 Comment(3)
I thought the + sigh was used to define a new id, I'm surprised it works :-) Thanks.Cavalla
@LalitPoptani i know that + used for new id and also bug.but its solution is assign +Touchy
When you have used + in the RadioGroup, the id rdb_positive will be created. So you don't need to use + again in the RadioButton. So use android:id="@id/rdb_positive" in the RadioButton.Redeemable
C
2

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>
Conga answered 14/6, 2012 at 8:52 Comment(1)
Please notice: If you set android:checked="true" on the default RadioButton, it doesn't get unchecked when checking another! To implement the default button properly, you have to use the "android:checkedButton"-attribute of RadioGroup.Flooded
A
2

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>
Audient answered 15/7, 2013 at 15:10 Comment(1)
just add a "+" to the id. android:checkedButton="@+id/rdb_positive"Ossein
H
1
<RadioGroup
    style="@style/FormInputField"
    android:orientation="vertical"> 
    <RadioButton
        android:id="@+id/rdb_positive"
        android:text="@string/answer_positive"
        android:checked="true"/>  
    <RadioButton
        android:id="@+id/rdb_negative"
        android:text="@string/answer_negative" />
</RadioGroup>

Add android:checked="true" to the radiobutton that you want to make as default

Hixon answered 7/3, 2016 at 6:52 Comment(1)
Please notice: If you set android:checked="true" on the default RadioButton, it doesn't get unchecked when checking another! To implement the default button properly, you have to use the "android:checkedButton"-attribute of RadioGroupGar

© 2022 - 2024 — McMap. All rights reserved.