Search code examples
androidradio-buttonradio-groupandroid-radiogroup

Make radio button stay highlighted when clicked


I want to know how to make a radio button become highlighted when clicked.

Here is my radiogroup:

<RadioGroup
    android:layout_width="320dp"
    android:layout_height="50dp"
    android:layout_below="@+id/textView2"
    android:id="@+id/editDay"
    android:background="#abf234"
    android:clickable="true"
    android:checkedButton="@+id/sound"
    android:orientation="horizontal"
    android:layout_centerHorizontal="true">

    <RadioButton
        android:id="@+id/dayOneButton"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="Day 1"
        android:gravity="center"
        android:background="@drawable/yourbuttonbackground"
        android:button="@android:color/transparent"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/dayTwoButton"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:text="Day 2"
        android:gravity="center"
        android:layout_below="@+id/myRadioGroup"
        android:background="@drawable/yourbuttonbackground"
        android:button="@android:color/transparent"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/dayThreeButton"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Day 3"
        android:background="@drawable/yourbuttonbackground"
        android:button="@android:color/transparent"
        android:checked="true"/>

    <RadioButton
        android:id="@+id/dayFourButton"
        android:layout_width="80dp"
        android:layout_height="50dp"
        android:gravity="center"
        android:text="Day 4"
        android:background="@drawable/yourbuttonbackground"
        android:button="@android:color/transparent"
        android:checked="true"/>

</RadioGroup>

Here is my selector:

<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/b" />
<item android:state_checked="true"  android:state_pressed="true" android:drawable="@drawable/a" />
<item android:drawable="@drawable/b" /> <!-- default -->

Drawable selceted:

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
    android:radius="5dp" />
<solid
    android:color="#fff" />
<stroke
    android:width="2dp"
    android:color="#53aade" />

Drawable unselected:

<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners
    android:radius="5dp" />
<solid
    android:color="#fff" />
<stroke
    android:width="2dp"
    android:color="#555555" />

Right now the radiobuttons only get hightlighted when I click them- i need them to stay hightlighted after i click them.

Thanks


Solution

  • To me the selector seems to be wrong. I dont think android:state_pressed is needed for the @drawable/a . So it will look something like this.

    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:state_checked="false" android:state_pressed="false" android:drawable="@drawable/b" />
    <item android:state_checked="true"   android:drawable="@drawable/a" />
    <item android:drawable="@drawable/b" /> <!-- default -->