Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add icons to PreferenceScreen entries

Consider this preferences.xml file:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:title="@string/preference_main">
    <PreferenceScreen
        android:title="@string/preference_sight"
        android:key="category_sight">
        <ListPreference 
            android:summary="@string/preference_sight_wb_msg"
            android:title="@string/preference_sight_wb_title"
            android:key="sight_wb" android:defaultValue="auto"/>
        <Preference
            android:key="sight_wb_values_cache"/>
        <eu.elevelcbt.sm.preferences.PercentBarPreference
            android:title="@string/preference_sight_mean_confidence_min_title"
            android:summary="@string/preference_sight_mean_confidence_min_msg"
            android:key="sight_mean_confidence_min"
            android:defaultValue="80"/>
        <CheckBoxPreference 
            android:key="sight_flash"
            android:defaultValue="false"
            android:summary="@string/preference_sight_flash_msg"
            android:title="@string/preference_sight_flash_title"/>
    </PreferenceScreen>
</PreferenceScreen>

When shown in my MainPreference class extending PreferenceActivity is correctly shows me a first level menu with one entry "Sight" (@string/preference_main) which, when selected, takes me to the second preference screen where I have all my preferences. Everything works as I wanted. The only thing is the on first preference screen I want to put an icon beside the label "Sight" like in main Android setting menu.

How can I do that? Thank you very much in advance for any help! Luca.

Mine: Mine

Desired: Desired


...mmm I tried but no luck...

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:title="@string/preference_main">
  <PreferenceScreen
        android:title="@string/preference_sight"
        android:key="category_sight"
        android:icon="@drawable/ic_dialog_light">
...
</PreferenceScreen>

where am I doing wrong?

like image 504
lviggiani Avatar asked Mar 05 '11 08:03

lviggiani


4 Answers

I have another suggestion which works for me on my Nexus 7, running Android 4.2, and my ASUS TF101, running Android 4.0.3: add the icon attribute to the preference headers, like so:

<?xml version="1.0" encoding="utf-8"?>
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" >

    <header
        android:fragment="com.example.SettingsFragment"
        android:summary="@string/pref_summary_caching"
        android:icon="@drawable/ic_pref_cache"
        android:title="@string/pref_cat_caching" >
        <extra
            android:name="category"
            android:value="caching" />
    </header>
</preference-headers>

this lets the icons appear to the left of the header text. Seems to be documented nowhere, and you cannot enter the values through the Structure screen of the XML editor, as there does not seem to be an XML schema for the preference-headers file. But hey, it WORKS!

like image 85
doulos Avatar answered Nov 15 '22 04:11

doulos


Use android:icon

http://developer.android.com/reference/android/preference/Preference.html#attr_android:icon

like image 33
Will Tate Avatar answered Nov 15 '22 05:11

Will Tate


You can look at the source code to the settings app and see they they are extending Preference to do this. Here is the class file.

https://github.com/android/platform_packages_apps_settings/blob/master/src/com/android/settings/IconPreferenceScreen.java

hope this helps!

like image 39
Nathan Schwermann Avatar answered Nov 15 '22 06:11

Nathan Schwermann


Just add android:icon="@mipmap/icon_launcher It works.

Example code:

<ListPreference
    android:icon="@mipmap/icon_launcher"
    android:title="@string/pref_sort_order_label"
    android:key="@string/pref_sort_order_key"
    android:defaultValue="@string/pref_most_popular"
    android:entries="@array/pref_sort_order"
    android:entryValues="@array/pref_sort_order">
</ListPreference>
like image 1
Danny Gavrilov Avatar answered Nov 15 '22 04:11

Danny Gavrilov