I have customized all the radioButtons in my application but the radioButtons in the listPreference does not get customized.
I have used this xml named btn_radio.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/radio_selected" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/radio_unselected" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/radio_selected" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/radio_unselected" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/radio_selected" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/radio_unselected" />
<item android:state_checked="false" android:drawable="@drawable/radio_unselected" />
<item android:state_checked="true" android:drawable="@drawable/radio_selected" />
</selector>
This is the customRadioButton which extends the android custom radio button
<style name="CustomRadioButton" Parent="@android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">@drawable/btn_radio</item>
</style>
in the theme of my application I have done this changes
<item name="android:radioButtonStyle">@style/CustomRadioButton</item>
<item name="android:listChoiceIndicatorSingle">@style/CustomRadioButton</item>
This changes customize all the radioButtons in my application except radioButtons in my ListPreference
Styling the
ListPreference
from XML is not directly possible. The problem is thatListPreference
(throughDialogPreference
) callsAlertDialog.Builder(Context)
to build itsDialog
, rather thanAlertDialog.Builder(Context context, int themeResourceId)
. While the latter allows for providing a theme, the former does not, causing it to fall back to a default Android theme.For a project, I needed a
ListPreference
with a custom title-color, a custom radiobutton-style and a custom ListView-selector (basically, Holo in a different color). I solved this by extendingListPreference
and overridingonPrepareDialogBuilder(Builder)
andOnCreateDialogView()
so I could use a custom ListView with a simple ArrayAdapter, rather thanDialog
's built-inListView
(which doesn't have support for styling). I also had to overrideonDialogClosed()
in order to set the right value to the preference.In order to use it, all you have to do is replace the classname of the preference in your preferences.xml rom
ListPreference
tocom.your.packagename.ThemedListPreference
. Other than that, the implementation is identical to ListPreference.For my ListView items I used the layout below. Note that drawable/btn_radio_holo_light is an XML-drawable like the one in your android-sdk/platforms/android-x/data/res/drawable folder, only with references to different drawables.
For my Dialog layout (
onCreateDialogView()
), I used the following: