How to add icons to PreferenceScreen entries
Asked Answered
P

6

7

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?

Phuongphycology answered 5/3, 2011 at 8:57 Comment(0)
N
3

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!

Nessa answered 27/6, 2011 at 23:10 Comment(1)
Link is still brokenMahaliamahan
E
7

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!

Elyn answered 14/12, 2012 at 18:44 Comment(0)
W
6

Use android:icon

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

Whencesoever answered 8/3, 2011 at 19:53 Comment(2)
This does not work on a PreferenceScreen (at least not in Android 2.2. I suspect it never worked).Kath
I believe this was introduced in API 11.Houseless
N
3

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!

Nessa answered 27/6, 2011 at 23:10 Comment(1)
Link is still brokenMahaliamahan
A
1

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>
Ation answered 23/7, 2017 at 11:1 Comment(0)
G
0

Old question but the answer is now in the developers website: http://developer.android.com/guide/topics/ui/settings.html#PreferenceHeaders

Gym answered 31/7, 2014 at 6:48 Comment(1)
I can see nice samples with icons, but there's no explanation whatsoever on how it got there, or did I miss something?Liz
F
0

icon

A Drawable that represents the Preference icon.

Example: app:icon="@drawable/ic_camera"

https://developer.android.com/guide/topics/ui/settings/components-and-attributes?hl=vi

Finedraw answered 2/3, 2020 at 9:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.