Skip to content Skip to sidebar Skip to footer

Preferencefragment Background Color

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 P

Solution 1:

This question was also answered here

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

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

    return view;
}

Solution 2:

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

Solution 3:

try this.

@Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getListView().setBackgroundColor(Color.BLACK);
  }

Solution 4:

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

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

<resourcesxmlns:tools="http://schemas.android.com/tools"><!-- Base application theme. --><stylename="Theme.MyApplication"parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        ...............
       <!-- Add below --><itemname="preferenceTheme">@style/preference</item></style><stylename="preference"parent="Theme.AppCompat"><itemname="android:background">@color/purple_200</item></style></resources>

Post a Comment for "Preferencefragment Background Color"