Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Preference icon color globally Android

I have set flat icon for all my Preference, I would like to change the color of that icon globally.

When I try the below code it even changes the back button color in the toolbar.

I want only Preference icon tint to be changed globally. Thank in advance.

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

    <SwitchPreference
        android:id="@+id/pref_toggle_alarm"
        android:icon="@drawable/ic_pref_notifications"
        android:key="key_toggle_alarm"
        android:summaryOff="Alarm OFF"
        android:summaryOn="Alarm ON"
        android:title="Alarm" />


    <web.prefs.TimePrefs
        android:id="@+id/pref_select_time"
        android:icon="@drawable/ic_pref_time"
        android:key="key_time"
        android:summary="Set some time"
        android:title="Select Time" />

    <MultiSelectListPreference
        android:id="@+id/pref_select_week"
        android:defaultValue="@array/week_array_values"
        android:entries="@array/array_week_selection"
        android:entryValues="@array/week_array_values"
        android:icon="@drawable/ic_pref_time"
        android:key="key_week"
        android:title="Select Days" />

    <ListPreference
        android:id="@+id/pref_track"
        android:defaultValue="0"
        android:entries="@array/tracks_arrays"
        android:entryValues="@array/tracks_arrays_values"
        android:icon="@drawable/ic_music_note"
        android:key="key_track"
        android:summary="%s"
        android:title="Select Track" />

</PreferenceScreen>

style.xml

<style name="PreferencesTheme" parent="@style/AppTheme.NoActionBar">
    <item name="android:textColorPrimary">@color/primary_text</item>
    <item name="android:textColorSecondary">@color/secondary_text</item>
    <item name="android:colorAccent">@color/accent</item>
    <item name="android:tint">@color/accent</item>
</style>
like image 571
Sai Avatar asked Mar 16 '17 03:03

Sai


People also ask

How do I change the color of the menu icon?

add app:iconTint="@color/yourcolor" in your MenuItem for change the Icon color.

Where is Preferences option in Android Studio?

From the menu bar, click File > Settings (on macOS, click Android Studio > Preferences).

What are preferences in Android Studio?

A Preference is the basic building block of the Preference Library. A settings screen contains a Preference hierarchy. You can define this hierarchy as an XML resource, or you can build a hierarchy in code. The sections below describe how to build a simple settings screen using the AndroidX Preference Library.


1 Answers

You have to change the color of preference icons programmatically, there's no way to do it by themes or XML attributes. You can add the following in your PreferenceFragment:

@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
    addPreferencesFromResource(R.xml.preferences);

    int colorAttr = android.R.attr.textColorSecondary;

    TypedArray ta = context.getTheme().obtainStyledAttributes(new int[]{colorAttr});
    int iconColor = ta.getColor(0, 0);
    ta.recycle();
    tintIcons(getPreferenceScreen(), iconColor);
}

private static void tintIcons(Preference preference, int color) {
    if (preference instanceof PreferenceGroup) {
        PreferenceGroup group = ((PreferenceGroup) preference);
        for (int i = 0; i < group.getPreferenceCount(); i++) {
            tintIcons(group.getPreference(i), color);
        }
    } else {
        Drawable icon = preference.getIcon();
        if (icon != null) {
            DrawableCompat.setTint(icon, color);
        }
    }
}

Alternatively, I think this library may also be able to help you tint icons. It also fixes other issues with AppCompat preferences.

like image 54
Nicolas Avatar answered Sep 20 '22 12:09

Nicolas