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.

Remi Guan
  • 21,506
  • 17
  • 64
  • 87
Ruben
  • 796
  • 3
  • 9
  • 21

4 Answers4

26

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

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
  • 8
    I thought the + sigh was used to define a new id, I'm surprised it works :-) Thanks. – Ruben Jun 14 '12 at 08:52
  • 3
    @LalitPoptani i know that `+` used for new id and also bug.but its solution is assign `+` – Samir Mangroliya Jun 14 '12 at 09:01
  • 3
    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. – faizal Aug 20 '14 at 15:05
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>
Anu
  • 552
  • 3
  • 9
  • 2
    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. – cody Nov 18 '13 at 19:34
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>
Ondřej Z
  • 5,094
  • 3
  • 24
  • 30
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

mavHarsha
  • 1,056
  • 10
  • 16
  • 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 – Shirish Herwade Feb 10 '17 at 12:57