Andrea
Andrea

Reputation: 57

Android - RadioButton isn't uncheck with initial checked state

I have a RadioButton within a RadioGroup,

the problem arises when i set the initial state of the button

android:checked = "true"

because if I press the RadioButton "F" the RadioButton "M" doesn't uncheck...

how can I do? what is wrong?

here's the code:

<RadioGroup
   android:id="@+id/registrazione_utente_sesso"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal" >

   <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:checked="true"
      android:text="M"
      android:textColor="#ff7415"
      android:textSize="18sp" />

   <RadioButton
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="13.33dp"
      android:text="F"
      android:textColor="#ff7415"
      android:textSize="18sp" />
</RadioGroup>

screen:

initial state (correct):

https://i.sstatic.net/bP4Zb.png

final state when i press RadioButton "F" (wrong):

https://i.sstatic.net/Jlsh2.png

thanks

Upvotes: 3

Views: 2179

Answers (1)

Tam&#225;s Szincs&#225;k
Tam&#225;s Szincs&#225;k

Reputation: 1031

Assign an unique id to the radio buttons with android:id, then set the android:checkedButton attribute of the RadioGroup, like this:

<RadioGroup
   android:id="@+id/registrazione_utente_sesso"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:orientation="horizontal"
   android:checkedButton="@+id/radiobutton_m" >

   <RadioButton
      android:id="@+id/radiobutton_m"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="M"
      android:textColor="#ff7415"
      android:textSize="18sp" />

   <RadioButton
      android:id="@+id/radiobutton_f"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="13.33dp"
      android:text="F"
      android:textColor="#ff7415"
      android:textSize="18sp" />
</RadioGroup>

Upvotes: 5

Related Questions