D.D.M.
D.D.M.

Reputation: 127

Change Preference Screen background color

I change color PreferenceScreen with this code.But how get Preference Screen in main Preference activity and change Preference Screen color ???

getListView().setBackgroundColor(Color.TRANSPARENT);
    getListView().setCacheColorHint(Color.TRANSPARENT);
    getListView().setBackgroundColor(Color.rgb(4, 26, 55));

Upvotes: 9

Views: 15095

Answers (4)

Mohammed Uzair
Mohammed Uzair

Reputation: 145

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

Upvotes: 1

Dyno Cris
Dyno Cris

Reputation: 2414

Just extend AppTheme and use colorBackground attribute

<style name="PreferenceTheme" parent="@style/AppTheme">
    <item name="android:colorBackground">@color/colorBackground</item>
</style>

Upvotes: 1

user2195058
user2195058

Reputation: 927

You can override OnCreate Method:

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = super.onCreateView(inflater, container, savedInstanceState);
        view.setBackgroundColor(getResources().getColor(<COLOR>));
        return view;
    }

Or you can enter the following in styles.xml

<style name="PreferenceTheme" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
        <item name="android:background">@android:color/white</item>
    </style>

Upvotes: 16

ahodder
ahodder

Reputation: 11439

Use a Style for your activity.

Upvotes: 1

Related Questions