Search code examples
androidbackgroundcustomizationpreferenceactivity

Custom Android preferences background


I'm currently trying to set custom background for preference screen. I've created PreferenceActivity

public class SettingsActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
    PreferenceManager.setDefaultValues(this, R.xml.preferences, false);
}
}

Here are my Preference layout

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" 
    >
    <CheckBoxPreference 
        android:key="pref_Autoplay"
        android:title="@string/pref_Autoplay"
        android:summary="@string/pref_Autoplay_summ"
        android:defaultValue="false"/>
    <ListPreference 
        android:entryValues="@array/pref_Voice_values" 
        android:entries="@array/pref_Voice_entries" 
        android:key="pref_Voice" 
        android:summary="Choose male or female reader" 
        android:title="Readers voice" 
        android:defaultValue="female"/>


</PreferenceScreen>

As I read here to change background I need to create my own theme with style. Here it is:

<style name="PrefsTheme" parent="@android:style/Theme.Black.NoTitleBar">
        <item name="android:windowBackground">@drawable/prefs_bg</item>
        <item name="android:textColor">@color/prefs_bg</item>
        <item name="android:listViewStyle">@style/listViewPrefs</item>
    </style>

    <style name="listViewPrefs" parent="@android:style/Widget.ListView">
        <item name="android:background">@color/prefs_bg</item>
        <item name="android:cacheColorHint">@color/prefs_bg</item>
        <item name="android:textColor">@color/prefs_bg</item>
    </style>

All I need is to set some image background for preferences list. My image is @drawable/prefs_bg. When I set windowBackground to image and background to some color, preferences list background uses color instead of picture. But when I set background to @drawable/prefs_bg preferences list background appears with my picture and Radio Button dialog for my ListPreference item is also using image background.

Please help, how can I set image background for preferences list without affecting background of radio button dialog of some of the preferences item?


Solution

  • Ok. I've found how to solve my problem. It was my fault. To set Preference background only need

    <item name="android:windowBackground">@drawable/prefs_bg</item>
    

    This one was unnecessary:

    <item name="android:background">@color/prefs_bg</item>
    <item name="android:cacheColorHint">@color/prefs_bg</item>