Question

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

Was it helpful?

Solution

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;
}

OTHER TIPS

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));
}

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>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top