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?
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.