26

I am using a PreferenceFragment (without a ListView), but am unable to set the background color, and it seems to be transparent.

How can I set the background color to white with PreferenceFragment?

EDIT: first screenshot: image 1

Overriding onCreateView not work - image 2

Akhil Jain
  • 13,872
  • 15
  • 57
  • 93
user396707
  • 427
  • 1
  • 4
  • 17
  • Check my answer [here][1] it should work. [1]: http://stackoverflow.com/questions/16353861/android-changing-the-background-color-for-preference-fragment/17072511#17072511 – Diego Acosta Jun 12 '13 at 18:23

3 Answers3

50

This question was also answered here

Adding the following code to your PreferenceFragment will let you add a background color, image, etc.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = super.onCreateView(inflater, container, savedInstanceState);
    view.setBackgroundColor(getResources().getColor(android.R.color.your_color));

    return view;
}
Community
  • 1
  • 1
Josh Larson
  • 825
  • 10
  • 14
  • 1
    Is it possible to change color only bellow preferences controls? To leave the empty area transparent. – Dmitry Aug 27 '18 at 04:26
6

Also you can do something like changing the view's background in the onViewCreated() method.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    view.setBackgroundColor(getResources().getColor(R.color.YOUR_COLOR));
}
M090009
  • 1,129
  • 11
  • 20
  • The actual answer in this thread didn't work for me, but this fixed it! :D :D Thanks a lot for the solution :D – Felix Eder Sep 29 '18 at 15:55
2

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

The below code has worked for me. (in themes.xml)

<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