Search code examples
androidandroid-layoutpreferenceactivity

Preference Activity white margin on large screens


I'm trying to change the preference list background colour which works fine for smaller devices, but leaves an ugly white margin on tablets. While looking for solutions I read that on larger devices the preference list view is placed in a fragment.

Any ideas on how I can remove it? Do I just need to work out how to set the parents background colour?

Not allowed to post images yet, so here's the screenshot: https://i.sstatic.net/4OuT3.png

AppPreferenceActivity.java:

 @override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    // ---load the preferences from an XML file---
    addPreferencesFromResource(R.xml.myapppreferences);

    int[] colour = getIntent().getExtras().getIntArray("mColour");
    getListView().setBackgroundColor(
            Color.rgb(colour[0], colour[1], colour[2]));

    // Set theme & colour
    setTheme(R.style.PreferencesTheme);
    getActionBar().setTitle("Preferences");
    setIcon();`

themes.xml:

<?xml version="1.0" encoding="UTF-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">

<style name="PreferencesTheme">
    <item name="android:typeface">sans</item>
    <item name="android:textColorPrimary">#fff</item>
    <item name="android:textColorSecondary">#fff</item>


</style>

myapppreferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >

<PreferenceCategory android:title="Display Settings" >
    <ListPreference
        android:dialogTitle="Colour..."
        android:defaultValue="Red"
        android:entries="@array/colourEntries"
        android:entryValues="@array/colourValues"
        android:key="pref_colour"
        android:summary="Choose a default background colour..."
        android:title="App Background Colour" >
    </ListPreference>
</PreferenceCategory>

<PreferenceCategory android:title="Reminder Settings" >
    <ListPreference
        android:dialogTitle="Remind me..."
        android:defaultValue="The day before"
        android:entries="@array/dayRemindEntries"
        android:entryValues="@array/dayRemindValues"
        android:key="pref_days_before"
        android:summary="Remind me..."
        android:title="Default reminder..." >
    </ListPreference>
</PreferenceCategory>


Solution

  • addPreferencesFromResource method is deprecated in API 11 and higher. Try to use a PreferenceFragment and override onCreateView like this:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = super.onCreateView(inflater, container, savedInstanceState);
        if(v != null) {
            ListView lv = (ListView) v.findViewById(android.R.id.list);
            lv.setPadding(10, 10, 10, 10);
        }
        return v;
    }
    

    This code is taken from this response: Android: How to maximize PreferenceFragment width (or get rid of margin)?