55

I have a PreferenceCategory, xml file and I have defined all preferences in it, I call this from class that extends PreferenceActivity. I am unable to set the background of my settings screen, this screen is displayed with help of xml file shown below. Please see that I have already defined the android:background="#041A37", still the screen remains default color: black.

public class MyPreferenceActivity extends PreferenceActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Context mContext=super.getBaseContext();
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.layout.preference);
        //v.setBackgroundColor(Color.rgb(4, 26, 55));
    }
}

preference.xml is

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

    <PreferenceCategory>
        <com.dropcall.SeekBarPreference
            android:background="#041A37"
            android:defaultValue="5"
            android:key="@string/Interference_Delay"
            android:progressDrawable="@drawable/seekbardrawable"
            android:title="Seconds Delay until intereference" />

        <com.dropcall.SeekBarPreference2
            android:defaultValue="30"
            android:key="@string/Drop_Delay"
            android:progressDrawable="@drawable/seekbardrawable"
            android:title="Seconds delay until drop" />

        <CheckBoxPreference
            android:background="@drawable/state_normal"
            android:defaultValue="true"
            android:key="@string/Drop_Option"
            android:title="Close after call drop" />
        <CheckBoxPreference
            android:background="@drawable/state_normal"
            android:defaultValue="true"
            android:key="@string/Timer_Option"
            android:title="Start timers on launch" />
    </PreferenceCategory>

</PreferenceScreen>

Although I have set android:background="#041A37" in every file, the background doesn't turn into navy blue, or any other color for that matter. It remains default color, black. How to change the background color. Please let me know any pointers / hints , if you had faced same issue let me know what changes you made to set the background color.

stealthjong
  • 10,858
  • 13
  • 45
  • 84
David Prun
  • 8,203
  • 16
  • 60
  • 86
  • 1
    Check out this link [http://udinic.wordpress.com/2011/08/18/dress-up-your-preferenceactivity/](http://udinic.wordpress.com/2011/08/18/dress-up-your-preferenceactivity/) This guy figured out how to completely customize PreferenceActivity and it works great. – smukov May 02 '12 at 16:16

9 Answers9

80

You can define a theme and then set this for your PreferenceActivity in the manifest. Your theme can then define a a background color or a windowBackground image should you prefer that.

Manifest:

    <activity android:label="@string/app_label" android:name=".client.PreferencesActivity"
        android:theme="@style/PreferencesTheme">
        <intent-filter>                                
        </intent-filter>
    </activity>

Then add the theme to your styles.xml

<style name="PreferencesTheme">
    <item name="android:windowBackground">@drawable/background_image</item>
    <item name="android:background">#FFEAEAEA</item>
</style>

In the above snippet there's both a background color and a background image defined to show how to do it.

AndreasW
  • 1,045
  • 2
  • 12
  • 15
  • 7
    If you only want to change the background color, I would recommend setting `android:windowBackground` with a defined color, such as `@color/myFavoriteColor`, because otherwise it will override the background (image) of every view, including the activity's dialogs. – Paul Lammertsma Nov 09 '11 at 11:26
  • use android:windowBackground, with android:background ripple won't work correctly – Bita Mirshafiee Apr 20 '20 at 05:25
  • How can I do this when there's no Activity but only a PreferenceFragment? (PreferenceFragmentCompat) – Dominik Nov 26 '20 at 09:56
  • 1
    @Dominik, your case was similar to mine. As a solution, I defined `windowBackground` inside my `AppTheme` style in styles.xml, i.e.: ``. – iloo Nov 28 '21 at 11:55
46

This worked for me

getListView().setBackgroundColor(Color.TRANSPARENT);

getListView().setCacheColorHint(Color.TRANSPARENT);

getListView().setBackgroundColor(Color.rgb(4, 26, 55));
Michael Celey
  • 12,645
  • 6
  • 57
  • 62
David Prun
  • 8,203
  • 16
  • 60
  • 86
  • 22
    why do you call setBackgroundColor twice ? – android developer Apr 07 '13 at 10:18
  • Thanks! I tried setting theme on activity but that was not the preferable solution since it affects the action bar as well. getListView().setBackgroundColor(...) only changes the content and that is precisely what I wanted. – Atte Backenhof Jun 04 '15 at 08:47
21

Another work-around as far as color goes is that you create a theme for the preferences activity and put the background color of list views as well:

<style name="PrefsTheme" parent="@android:style/Theme.Black.NoTitleBar">
    <item name="android:windowBackground">@color/prefs_bg</item>
    <item name="android:textColor">@color/text_color</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>
</style>
Sileria
  • 15,223
  • 4
  • 49
  • 28
  • 3
    Excellent answer! Especially for pointing out a way to set the cacheColorHint value for the ListView in PrefereneScreen! – Maghoumi May 01 '12 at 20:41
  • This one worked for me for nested preferences (preference screens) as well. Good job! Just make sure you don't put `windowBackground` instead of `background` into the `listViewPrefs` style, or you'll wonder for a while why the heck it does not work :P. – kar Dec 21 '12 at 09:25
  • Yes it works OK with my `PreferenceFragment`, but unfortunately I don't know how to change that funky yellow highlight color I get upon item's selection... :) – dentex Oct 19 '19 at 08:42
6

android:background is not an available attribute, according to the documentation.

It is possible you could theme the PreferenceActivity to achieve your color change, though I have not tried this, because I want my preferences to look like those of the rest of Android, to improve usability of the app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • http://stackoverflow.com/questions/3560647/unable-to-inflate-custom-button-through-preferencecategory-xml-file – David Prun Aug 24 '10 at 22:40
  • 1
    It will work until You'll try to have EditTextPreference. EditText dialog will have the same theme as preference activity (in this case just background color) and will look really weird. – sandrstar Jun 29 '11 at 05:36
  • 4
    "I want my preferences to look like those of the rest of Android" - I do not think this is a valid point with honeycomb anymore, because the android preferences app itself is using a customized background now. – Nappy Jul 27 '11 at 16:13
5

Or you can also make drawable as your background:

getListView().setBackgroundDrawable(getResources().getDrawable(R.drawable.bluegradient));

Note: setBackgroundDrawable() is deprecated. Use setBackground() instead.

getListView().setBackground(getResources().getDrawable(R.drawable.bluegradient));

Prudhvi
  • 2,276
  • 7
  • 34
  • 54
Rootko
  • 51
  • 1
1

I faced with the same requirement (Androidx Preference Screen background for settings fragment).

The below code has worked for me in a fragment. (in themes.xml). I assume that it is gonna work also for an activity.

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ...............
       <!-- Add below -->
        <item name="preferenceTheme">@style/preference</item>
    </style>
    
    <style name="preference" parent="Theme.AppCompat">
        <item name="android:background">@color/purple_200</item>
    </style>
</resources>
ipikuka
  • 524
  • 4
  • 9
0

Please specify a linear layout with a textview attached and specify background color and attach this xml to preferencecategory using the layout property.

<PreferenceCategory
     android:layout="@layout/preftitle"
 >

Where preftitle is an xml which has a linear layout and text view attached.

AndreasW
  • 1,045
  • 2
  • 12
  • 15
madhavi
  • 41
  • 1
  • 3
0

Go to res>values>styles.xml> and add this code to your <style > </style> the style should must be app base theme in this @color/activ is color resources added to colors.

<style name="app_theme" parent="@android:style/Theme">
    <item name="android:windowBackground">@color/activ</item>
    <item name="android:windowContentOverlay">@drawable/title_bar_shadow</item>
    <item name="android:listViewStyle">@style/TransparentListView</item>
</style>

if you use app_theme name of style tag then add like this to your manifest

<application
    android:name=".XXXXXX"
    android:allowBackup="true"

    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/app_theme" > //here

If you only want to change your background

ekad
  • 14,436
  • 26
  • 44
  • 46
Avinash Ajay Pandey
  • 1,497
  • 12
  • 19
0

I am using the PreferenceFragmentCompat, the below solution worked for me.

SettingsScreen

import android.os.Bundle
import android.util.TypedValue
import android.view.View
import androidx.annotation.ColorInt
import androidx.preference.ListPreference
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import com.almarai.easypick.R


class SettingsScreen : PreferenceFragmentCompat(), 
Preference.OnPreferenceChangeListener {

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {

//Providing the XML file for the view to be created
setPreferencesFromResource(R.xml.app_settings_preferences, rootKey)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {

//Get the Theme specific color
val typedValue = TypedValue()
val theme = requireContext().theme

/*R.attr.colorBackgroundScreenBody is my own attr from attrs.xml file, 
you can directly use R.color.red - Or any color from your colors.xml 
file if you do not have multi-theme app.*/
theme.resolveAttribute(R.attr.colorBackgroundScreenBody, typedValue, true)
@ColorInt val color = typedValue.data

view.setBackgroundColor(color)

super.onViewCreated(view, savedInstanceState)
}
}
Mohammed Uzair
  • 145
  • 1
  • 11