How to customize preferences screen theme to look like that?

Alex picture Alex · Jan 15, 2011 · Viewed 7.6k times · Source

Ok, I have a Preferences activity set with an XML layout. Here I have a couple of Preferences that open some dialogs. On these preferences I want to have an arrow on the right of them like in the screenshot. How I do this, is something related to the theme ? First screenshot is how I want to look, second is what I have now.

http://img545.imageshack.us/i/screenshot1zlk.png/

http://img405.imageshack.us/i/tsc3.png/

Answer

stealthcopter picture stealthcopter · Feb 12, 2011

In your preference activity you can set whatever you want to appear in each preferences "widget_layout" (the area to right of the title/summary) like so:

Via XML:

<Preference
android:key="Settings_CustomBG"
android:title="@string/Custom_BG"
android:widgetLayout="@layout/ic_custom"
/>

OR programatically:

   Preference BGColor = findPreference("Setting_BG_Color"); 
   BGColor.setWidgetLayoutResource(R.layout.ic_custom);

Where ic_custom.xml (in /layout) is:

<?xml version="1.0" encoding="utf-8"?>
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_custom"
/>

and ic_custom is just an image file in the resources folder(s). In my case this looks like the following:

example of custom widget layout in an android preference