1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.settings;
17 
18 import android.content.Context;
19 import android.content.res.ColorStateList;
20 import android.content.res.TypedArray;
21 import android.support.annotation.ColorInt;
22 import android.support.v7.preference.Preference;
23 import android.support.v7.preference.PreferenceViewHolder;
24 import android.util.AttributeSet;
25 import android.widget.ImageView;
26 
27 public class TintablePreference extends Preference {
28     @ColorInt
29     private int mTintColor;
30 
TintablePreference(Context context, AttributeSet attrs)31     public TintablePreference(Context context, AttributeSet attrs) {
32         super(context, attrs);
33 
34         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.TintablePreference);
35         mTintColor = a.getColor(R.styleable.TintablePreference_android_tint, 0);
36         a.recycle();
37     }
38 
setTint(int color)39     public void setTint(int color) {
40         mTintColor = color;
41         notifyChanged();
42     }
43 
44     @Override
onBindViewHolder(PreferenceViewHolder view)45     public void onBindViewHolder(PreferenceViewHolder view) {
46         super.onBindViewHolder(view);
47 
48         if (mTintColor != 0) {
49             ((ImageView) view.findViewById(android.R.id.icon)).setImageTintList(
50                     ColorStateList.valueOf(mTintColor));
51         } else {
52             ((ImageView) view.findViewById(android.R.id.icon)).setImageTintList(null);
53         }
54     }
55 }
56