changing the preference category label color background

11,025

Solution 1

I think the problem is that you are trying to pass a style/theme that only has a background color specified in it when it is expecting a style/theme/layout that contains a view to replace the current view in the preference with.

The example in the documentation for android:widgetLayout says

a checkbox preference would specify a custom layout (consisting of just the CheckBox) here

Another option would be to try and do this in code by overriding the onBindView() of the PreferenceCategory.

and yes, changing the theme in the AndroidManfiest.xml should change the background color (in this case) for the entire Application or Activity depending on where you specified it.

Solution 2

To me, this works:

For example ListSeparator item:

In styles.xml

<style name="ListSeparator">
<item name="android:background">@drawable/status_bar</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">25dip</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">@color/black</item>
<item name="android:textSize">14sp</item>
<item name="android:gravity">center_vertical</item>
<item name="android:paddingLeft">5sp</item>
</style>

<color name="custom_theme_color">#e6e6e6</color>

<style name="CustomTheme" parent="android:Theme.Light.NoTitleBar">
<item name="android:windowBackground">@color/custom_theme_color</item>
<item name="android:colorBackground">@color/custom_theme_color</item>
<item name="android:listSeparatorTextViewStyle">@style/ListSeparator</item>
</style>

Then, in AndroidManifest.xml:

<activity
android:name="MyActivityPreference"
android:theme="@style/CustomTheme" >
</activity>

Or view this thread: https://stackoverflow.com/a/5564943/471690

Solution 3

This is solution for my problem. In my file for example preference_category.xml I have to have a simple layout for the PreferenceCategory and than use it in android:layout. However android:widgetLayout doesn't work.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/my_blue"
>
<TextView 
android:id="@+android:id/title"
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:textColor="@color/my_white"
android:textStyle="bold"
android:textSize="16sp"
android:padding="5dp"
android:layout_marginLeft="10dp"
/>     
</LinearLayout>
Share:
11,025
woyaru
Author by

woyaru

Updated on June 04, 2022

Comments

  • woyaru
    woyaru about 2 years

    I would like to change the color of the preference category bakcgorund. In my xml with preferences I have categories typed in this way:

    <PreferenceCategory android:title="Synchronization"> ... </PreferenceCategory>
    

    I am trying to change the background using xml attribute:

    android:widgetLayout="@style/PreferenceCategory"
    

    And the code in my styles.xml looks like:

    <resources>
    <style name="PreferenceCategory">
        <item name="android:background">@color/my_blue</item>
    </style>
    </resources>
    

    But it isn't work properly. I have fatal exception:

    08-13 01:43:09.111: WARN/dalvikvm(1329): threadid=1: thread exiting with uncaught exception (group=0x40015560)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329): FATAL EXCEPTION: main
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329): android.content.res.Resources$NotFoundException: Resource ID #0x7f090006
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.content.res.Resources.getValue(Resources.java:892)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:1869)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.content.res.Resources.getLayout(Resources.java:731)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.preference.Preference.onCreateView(Preference.java:416)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.preference.Preference.getView(Preference.java:389)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.AbsListView.obtainView(AbsListView.java:1418)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.ListView.makeAndAddView(ListView.java:1745)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.ListView.fillDown(ListView.java:670)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.ListView.fillFromTop(ListView.java:727)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.ListView.layoutChildren(ListView.java:1598)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.AbsListView.onLayout(AbsListView.java:1248)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.View.layout(View.java:7175)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.RelativeLayout.onLayout(RelativeLayout.java:912)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.View.layout(View.java:7175)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.View.layout(View.java:7175)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.View.layout(View.java:7175)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.os.Handler.dispatchMessage(Handler.java:99)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.os.Looper.loop(Looper.java:123)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at android.app.ActivityThread.main(ActivityThread.java:3647)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at java.lang.reflect.Method.invokeNative(Native Method)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at java.lang.reflect.Method.invoke(Method.java:507)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    08-13 01:43:09.141: ERROR/AndroidRuntime(1329):     at dalvik.system.NativeStart.main(Native Method)
    

    I have the same fatal exception when I change the color form @color/my_blue to some hex code.

    I know that I can use in AndroidManifest for my preference activity this xml attribute:

    android:theme="@style/PreferencesTheme"
    

    But it changes the background color of all my preference activity. In which way can I change the attributes of preferences parts?