Custom Radio button in android app doesn't get grayed out on disabling that
Asked Answered
L

3

1

I have a custom radio button in android. Its works fine. My problem is that When I make those button disable it will not grayed out. How can I do so? Here is my sample code for custom radio button.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_checked="true" android:drawable="@drawable/radio_select" />
 <item android:state_checked="false" android:drawable="@drawable/radio_holo" />
</selector>

Here is code which I use in my layout

<RadioButton
    android:id="@+id/radioButton2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/custom_radio_button"
    android:onClick="OnClick"
    android:text="button 1"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:textColor="@color/black" />

One more thing that I required text appearance small and normal text color black.

Lindeman answered 29/3, 2013 at 6:37 Comment(0)
B
0

Try adding this to your custom_radio_button.xml:

<item android:state_enabled="false" android:drawable="@drawable/your_grayed_out_btn" />
Bailment answered 29/3, 2013 at 6:59 Comment(1)
I need grayed out text not button.Lindeman
M
0

If you need to change text color depending on the state of your RadioButton, try writing a selector as shown in this answer.

Mulvihill answered 29/3, 2013 at 8:37 Comment(0)
C
0

Be careful with attributes in the selector item, as it is logical AND.

Means if you put android:state_checked="false" and android:state_enabled="true" in an item. It will only work when both are true. Below code is working fine for selected, unselected and disabled states.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/ic_radio_btn_inactive" android:state_checked="false" android:state_enabled="true"/>
   <item android:drawable="@drawable/ic_radio_btn_active" android:state_checked="true" android:state_enabled="true"/>
   <item android:drawable="@drawable/ic_radio_btn_diabled"    android:state_enabled="false"/>
</selector>
Columbic answered 24/9, 2019 at 6:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.