8

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));
D.D.M.
  • 117
  • 1
  • 1
  • 9

4 Answers4

15

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>
user2195058
  • 887
  • 7
  • 10
1

Use a Style for your activity.

ahodder
  • 11,353
  • 14
  • 71
  • 114
  • If i use Style.Components in ListView and screens don't work. – D.D.M. Oct 03 '12 at 15:15
  • I set this style(android:theme) for PreferenceActivity. – D.D.M. Oct 03 '12 at 15:29
  • and all PreferenceScreen black on scrolling – D.D.M. Oct 03 '12 at 15:29
  • Did you assigned the theme to the activity in your manifest? – ahodder Oct 03 '12 at 15:32
  • Yes, main preference activity set now white and work good, but preferenceScreens blink (black\white) on scroll =( – D.D.M. Oct 03 '12 at 15:38
  • That is a `Selector` issue. Now you need to theme your selector for the screen. http://stackoverflow.com/questions/1219312/android-selector-text-color – ahodder Oct 03 '12 at 15:40
  • Help me please. I don't understand how fix it. – D.D.M. Oct 03 '12 at 15:56
1

Just extend AppTheme and use colorBackground attribute

<style name="PreferenceTheme" parent="@style/AppTheme">
    <item name="android:colorBackground">@color/colorBackground</item>
</style>
Dyno Cris
  • 1,823
  • 1
  • 11
  • 20
1

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)
    }
}
Mohammed Uzair
  • 145
  • 1
  • 11