PreferenceFragment background color
Asked Answered
C

3

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

Curbing answered 6/6, 2013 at 19:7 Comment(1)
Check my answer [here][1] it should work. [1]: #16354361Alonso
M
51

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;
}
Meingoldas answered 22/12, 2013 at 19:3 Comment(1)
Is it possible to change color only bellow preferences controls? To leave the empty area transparent.Thymelaeaceous
H
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));
}
Hypsometer answered 10/5, 2015 at 13:8 Comment(1)
The actual answer in this thread didn't work for me, but this fixed it! :D :D Thanks a lot for the solution :DKorney
S
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>
Spin answered 14/5, 2021 at 16:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.