2

Possible Duplicate:
Change icons of checked and unchecked for Checkbox for Android
customize check box preference

Is there any way to customize style of elements in PreferencesActivity?

I was only able to change color text, background etc.

I want to also change the check mark and radio button style in CheckBoxPreference and so on.

I'm interesting in API8 or greater.

Thanks for your help!

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="vibrate"
        android:summary="summary"
        android:title="Some title"
        android:widgetLayout="@layout/custom_checkbox" />
    <CheckBoxPreference
        android:defaultValue="true"
        android:key="autorotate"
        android:summary="summary"
        android:title="auto rotate" />
</PreferenceScreen>
Jonas
  • 121,568
  • 97
  • 310
  • 388
MicNeo
  • 716
  • 3
  • 12
  • 20

1 Answers1

4

I understand what you are saying now. I thought you were just using normal CheckBox inside your View. You are using CheckBoxPreference and the other Preference Views.

Ok, well the concept is very similar.

You need to define a custom widget layout. Do this in your res/layout directory and call it custom_checkbox.xml. We can use the CheckBox as an example.

Define how you would like the CheckBoxPreference to look with the custom widget layout you create in the res/layout directory by using a CheckBox. For instance:

<CheckBox
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+android:id/customCheckBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:button="@drawable/android_button"
    android:clickable="false"
    android:focusable="false" />

You can replace android:button with any drawable that you have found or designed.

Now you need to define your CheckBoxPreference and setting its android:widgetLayout to the custom CheckBox you just defined in res/layout. For example:

<CheckBoxPreference
    android:defaultValue="true"
    android:key="@string/Drop_Option"
    android:title="Close after call drop"
    android:widgetLayout="@layout/custom_checkbox" />

If you are not using xml for defining your layout you can do it in code like this:

    CheckBoxPreference checkBoxPreference = new CheckBoxPreference(this);
    checkBoxPreference.setWidgetLayoutResource(R.layout.custom_checkbox);

Resources found that were similar to this question.

Konstantin Burov


Related links to extended questions from comments:

Also, how to change style of list item?

android-listview-style

Also, I would like to add some paddings to list items.

spacing-between-listview-items-android

Community
  • 1
  • 1
prolink007
  • 33,872
  • 24
  • 117
  • 185
  • It not the case. I need to add those to CheckBoxPreference, ListPreference and so on. You way is okay, but it doesn't work with those. There is no background attribute. – MicNeo Aug 05 '12 at 19:35
  • I miss understood your question. Please read my edit. I understand what you said now. Once you can confirm this answer is correct, i will remove the previous answer. – prolink007 Aug 05 '12 at 20:13
  • Okay, this seems promising :) I couldn't find link between xml for CheckBoxPreferences and custom_checkbox.xml -> android:widgetLayout="@layout/custom_checkbox". I will test it after work. – MicNeo Aug 06 '12 at 07:10
  • Let me know if it works for you. – prolink007 Aug 06 '12 at 14:07
  • Yes it is working but I got another problem. Now, not whole entry (with name, summary etc) is clickable, but only checkbox image :/ Also, how to change style of list item? I want to have window background (image) and white backgrounds for list items. Is there any chance to do it? Also, I would like to add so paddings to list items. I'm not sure if it is even possible. Maybe override whole PreferencesActivity? – MicNeo Aug 06 '12 at 21:06
  • Ok, i added links in my answer to your extended questions. If that does not satisfy your extended questions, feel free to post those questions as new questions here on SO. good luck! – prolink007 Aug 07 '12 at 01:09
  • Once again I'm not too clear. I don't want to change style of ListView, by "list item" I meant items of PreferencesActivity (poor naming ;)). I added sample code to explain. I want to change look of CheckBoxPreference - after executing this activity it looks like list. When I add two CheckBoxPreference there will be two "items". Do you get it? :) And now I want to change style of those. I list I want to change background [link](http://bit.ly/OJJIqh) Red arrow points to filed I meant as a "item" and I need to make background white (plus paddings, but first background). – MicNeo Aug 07 '12 at 08:02
  • No, i am sorry, i do not understand what you mean. You may want to start another question. And include pictures of what you are wanting compared to what you are actually seeing. That will help others understand easier. – prolink007 Aug 07 '12 at 14:46
  • That is probably the best option. Thanks for you help! – MicNeo Aug 07 '12 at 19:45