Skip to content

Instantly share code, notes, and snippets.

@slightfoot
Last active November 9, 2018 19:33
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save slightfoot/dc170b9e58efa850d97f to your computer and use it in GitHub Desktop.
Save slightfoot/dc170b9e58efa850d97f to your computer and use it in GitHub Desktop.
Themed ListPreference
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<your.package.path.prefs.ThemedListPreference
android:key="pref_list"
android:entries="@array/list_pref"
android:entryValues="@array/list_pref"
android:title="Title"
android:summary="Summary"
android:theme="@style/SpecificListPrefTheme"
/>
</PreferenceScreen>
<resources>
<style name="AppTheme" parent="@android:style/Theme.Holo.Light">
</style>
<style name="SpecificListPrefTheme" parent="AppTheme">
<item name="android:alertDialogTheme">@style/ListPrefAlertDialogTheme</item>
</style>
<style name="ListPrefAlertDialogTheme" parent="@android:style/Theme.Holo.Light.Dialog.MinWidth">
<item name="android:alertDialogStyle">@style/ListPrefAlertDialogStyle</item>
<item name="android:windowBackground">@android:color/holo_blue_light</item>
</style>
<style name="ListPrefAlertDialogStyle">
<item name="android:fullDark" >@android:color/transparent</item>
<item name="android:topDark" >@android:color/transparent</item>
<item name="android:centerDark" >@android:color/transparent</item>
<item name="android:bottomDark" >@android:color/transparent</item>
<item name="android:fullBright" >@android:color/transparent</item>
<item name="android:topBright" >@android:color/transparent</item>
<item name="android:centerBright">@android:color/transparent</item>
<item name="android:bottomBright">@android:color/transparent</item>
<item name="android:bottomMedium">@android:color/transparent</item>
<item name="android:centerMedium">@android:color/transparent</item>
</style>
</resources>
import android.content.res.TypedArray;
import android.view.ContextThemeWrapper;
import android.util.AttributeSet;
import android.content.Context;
import android.preference.ListPreference;
public class ThemedListPreference extends ListPreference
{
private static int[] ATTRS = { android.R.attr.theme };
private ContextThemeWrapper mContextWrapper;
public ThemedListPreference(Context context)
{
this(context, null);
}
public ThemedListPreference(Context context, AttributeSet attrs)
{
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, ATTRS);
mContextWrapper = new ContextThemeWrapper(context, a.getResourceId(0, 0));
a.recycle();
}
@Override
public Context getContext()
{
return mContextWrapper;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment