I followed this example for applying some custom themes to my toggle buttons, but when I run my app I see the generic android toggle icons – I think I’m missing a setting here or there and could use an extra set of eyes on it.
I have a layout for use in a ListView for which I’m trying to use custom drawables for the checked/unchecked state, with each toggle status set to CHECKED by default:
<ToggleButton
android:id="@+id/profile_item_value_text"
style="@style/ProfileTagTheme"
android:layout_width="0px"
android:layout_weight="1"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="@color/black"
android:layout_gravity="center"
android:gravity="center_vertical|center_horizontal"
android:checked="true" />
Here’s what I have in my styles file for ProfileTagTheme:
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">@drawable/profile_btn_toggle_bg</item>
<item name="android:disabledAlpha">@android:attr/disabledAlpha</item>
</style>
<style name="ProfileTagTheme" parent="android:Theme.Black">
<item name="android:buttonStyleToggle">@style/Widget.Button.Toggle</item>
</style>
And in turn, profile_btn_toggle_bg.xml, living in my main drawable dir:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background" android:drawable="@android:color/transparent"/>
<item android:id="@android:id/toggle" android:drawable="@drawable/profile_btn_toggle"/>
</layer-list>
which references profile_btn_toggle, right next to it:
<?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/tag_active" /> <!-- pressed -->
<item android:drawable="@drawable/tag" /> <!-- default/unchecked -->
</selector>
I’ve verified the custom images are present in the drawables dir, so obviously either I’m misunderstanding something about the way the styles cascade, or I’m missing a reference somewhere in this style madness.
So it seems the only real problem I had was missing the
android:backgroundelement in myToggleButtonimplementation; so the only change to the code above was adding that element, e.g.