Skip to content
This repository was archived by the owner on May 1, 2021. It is now read-only.

Commit 6ec5615

Browse files
added support for icons for all of the preferences classes I've made
1 parent a925b14 commit 6ec5615

File tree

6 files changed

+245
-28
lines changed

6 files changed

+245
-28
lines changed

app/src/main/res/xml/settings_example.xml

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@
4242
app:negativeButtonText="@android:string/cancel"
4343
app:positiveButtonText="@android:string/ok"/>
4444

45-
<com.lb.material_preferences_library.custom_preferences.SwitchPreference
46-
android:defaultValue="true"
47-
android:key="switchPref"
48-
android:title="SwitchPreference title"
49-
app:summaryOff="summaryOff"
50-
app:summaryOn="summaryOn"/>
45+
<!--<com.lb.material_preferences_library.custom_preferences.SwitchPreference-->
46+
<!--android:defaultValue="true"-->
47+
<!--android:key="switchPref"-->
48+
<!--android:title="SwitchPreference title"-->
49+
<!--app:summaryOff="summaryOff"-->
50+
<!--app:summaryOn="summaryOn"/>-->
5151

5252
<com.lb.material_preferences_library.custom_preferences.CheckBoxPreference
5353
android:defaultValue="true"
@@ -59,6 +59,9 @@
5959

6060
<PreferenceCategory
6161
android:title="Native Preferences">
62+
<!--<SwitchPreference-->
63+
<!--android:summary="just some summary text"-->
64+
<!--android:title="switch"/>-->
6265
<Preference
6366
android:summary="just some summary text"
6467
android:title="A simple Preference"/>

material_preferences_library/build.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,5 @@ android {
2424

2525
dependencies {
2626
compile fileTree(include: ['*.jar'], dir: 'libs')
27-
// compile 'com.infstory:switch-preference-compat:1.0.0'
2827
compile 'com.android.support:appcompat-v7:22.2.0'
2928
}

material_preferences_library/src/main/java/com/lb/material_preferences_library/custom_preferences/Preference.java

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,30 @@
22

33
import android.annotation.TargetApi;
44
import android.content.Context;
5+
import android.content.res.TypedArray;
6+
import android.graphics.drawable.Drawable;
57
import android.os.Build.VERSION_CODES;
8+
import android.support.annotation.DrawableRes;
9+
import android.support.v4.content.ContextCompat;
610
import android.util.AttributeSet;
11+
import android.view.View;
12+
import android.view.ViewGroup;
13+
import android.widget.ImageView;
714

815
import com.lb.material_preferences_library.R;
916

1017
public class Preference extends android.preference.Preference
1118
{
1219
private boolean _isInitialized=false;
20+
private int _iconResId;
21+
private Drawable _icon;
1322

1423
protected void init(final Context context,final AttributeSet attrs,final int defStyleAttr,final int defStyleRes)
1524
{
1625
setLayoutResource(R.layout.mpl__preference);
26+
final TypedArray a=context.obtainStyledAttributes(
27+
attrs,R.styleable.Preference,defStyleAttr,defStyleRes);
28+
_iconResId=a.getResourceId(a.getIndex(R.styleable.Preference_icon),0);
1729
}
1830

1931
@TargetApi(VERSION_CODES.LOLLIPOP)
@@ -27,6 +39,49 @@ public Preference(final Context context,final AttributeSet attrs,final int defSt
2739
}
2840
}
2941

42+
/**
43+
* Sets the icon for this Preference with a Drawable.
44+
* This icon will be placed into the ID
45+
* {@link android.R.id#icon} within the View created by
46+
* {@link #onCreateView(ViewGroup)}.
47+
*
48+
* @param icon The optional icon for this Preference.
49+
*/
50+
public void setIcon(Drawable icon)
51+
{
52+
if((icon==null&&_icon!=null)||(icon!=null&&_icon!=icon))
53+
{
54+
_icon=icon;
55+
notifyChanged();
56+
}
57+
}
58+
59+
/**
60+
* Sets the icon for this Preference with a resource ID.
61+
*
62+
* @param iconResId The icon as a resource ID.
63+
* @see #setIcon(Drawable)
64+
*/
65+
public void setIconCompat(@DrawableRes int iconResId)
66+
{
67+
if(_iconResId!=iconResId)
68+
{
69+
_iconResId=iconResId;
70+
setIcon(ContextCompat.getDrawable(getContext(),iconResId));
71+
}
72+
}
73+
74+
/**
75+
* Returns the icon of this Preference.
76+
*
77+
* @return The icon.
78+
* @see #setIcon(Drawable)
79+
*/
80+
public Drawable getIconCompat()
81+
{
82+
return _icon;
83+
}
84+
3085
public Preference(Context context,AttributeSet attrs,int defStyleAttr)
3186
{
3287
super(context,attrs,defStyleAttr);
@@ -57,4 +112,27 @@ public Preference(Context context)
57112
}
58113
}
59114

115+
@Override
116+
protected void onBindView(final View view)
117+
{
118+
super.onBindView(view);
119+
final ImageView imageView=(ImageView)view.findViewById(R.id.icon);
120+
if(imageView!=null)
121+
{
122+
if(_iconResId!=0||_icon!=null)
123+
{
124+
if(_icon==null)
125+
_icon=ContextCompat.getDrawable(getContext(),_iconResId);
126+
if(_icon!=null)
127+
imageView.setImageDrawable(_icon);
128+
}
129+
imageView.setVisibility(_icon!=null?View.VISIBLE:View.GONE);
130+
}
131+
132+
final View imageFrame=view.findViewById(R.id.icon_frame);
133+
if(imageFrame!=null)
134+
{
135+
imageFrame.setVisibility(_icon!=null?View.VISIBLE:View.GONE);
136+
}
137+
}
60138
}
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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+
17+
package com.lb.material_preferences_library.internal.widget;
18+
19+
import android.content.Context;
20+
import android.content.res.TypedArray;
21+
import android.util.AttributeSet;
22+
import android.widget.ImageView;
23+
24+
import com.lb.material_preferences_library.R;
25+
26+
//import android.support.v7.preference.R;
27+
28+
/**
29+
* Extension of ImageView that correctly applies maxWidth and maxHeight.
30+
*
31+
* @hide
32+
*/
33+
public class PreferenceImageView extends ImageView
34+
{
35+
36+
private int mMaxWidth=Integer.MAX_VALUE;
37+
private int mMaxHeight=Integer.MAX_VALUE;
38+
39+
public PreferenceImageView(Context context)
40+
{
41+
this(context,null);
42+
}
43+
44+
public PreferenceImageView(Context context,AttributeSet attrs)
45+
{
46+
this(context,attrs,0);
47+
}
48+
49+
public PreferenceImageView(Context context,AttributeSet attrs,int defStyleAttr)
50+
{
51+
super(context,attrs,defStyleAttr);
52+
53+
final TypedArray a=context.obtainStyledAttributes(
54+
attrs,R.styleable.PreferenceImageView,defStyleAttr,0);
55+
56+
setMaxWidth(a.getDimensionPixelSize(
57+
R.styleable.PreferenceImageView_maxWidth,Integer.MAX_VALUE));
58+
59+
setMaxHeight(a.getDimensionPixelSize(
60+
R.styleable.PreferenceImageView_maxHeight,Integer.MAX_VALUE));
61+
62+
a.recycle();
63+
}
64+
65+
// public PreferenceImageView(Context context, AttributeSet attrs, int defStyleAttr,
66+
// int defStyleRes) {
67+
// super(context, attrs, defStyleAttr, defStyleRes);
68+
// }
69+
70+
@Override
71+
public void setMaxWidth(int maxWidth)
72+
{
73+
mMaxWidth=maxWidth;
74+
super.setMaxWidth(maxWidth);
75+
}
76+
77+
public int getMaxWidth()
78+
{
79+
return mMaxWidth;
80+
}
81+
82+
@Override
83+
public void setMaxHeight(int maxHeight)
84+
{
85+
mMaxHeight=maxHeight;
86+
super.setMaxHeight(maxHeight);
87+
}
88+
89+
public int getMaxHeight()
90+
{
91+
return mMaxHeight;
92+
}
93+
94+
@Override
95+
protected void onMeasure(int widthMeasureSpec,int heightMeasureSpec)
96+
{
97+
final int widthMode=MeasureSpec.getMode(widthMeasureSpec);
98+
if(widthMode==MeasureSpec.AT_MOST||widthMode==MeasureSpec.UNSPECIFIED)
99+
{
100+
final int widthSize=MeasureSpec.getSize(widthMeasureSpec);
101+
final int maxWidth=getMaxWidth();
102+
if(maxWidth!=Integer.MAX_VALUE
103+
&&(maxWidth<widthSize||widthMode==MeasureSpec.UNSPECIFIED))
104+
{
105+
widthMeasureSpec=MeasureSpec.makeMeasureSpec(maxWidth,MeasureSpec.AT_MOST);
106+
}
107+
}
108+
109+
final int heightMode=MeasureSpec.getMode(heightMeasureSpec);
110+
if(heightMode==MeasureSpec.AT_MOST||heightMode==MeasureSpec.UNSPECIFIED)
111+
{
112+
final int heightSize=MeasureSpec.getSize(heightMeasureSpec);
113+
final int maxHeight=getMaxHeight();
114+
if(maxHeight!=Integer.MAX_VALUE
115+
&&(maxHeight<heightSize||heightMode==MeasureSpec.UNSPECIFIED))
116+
{
117+
heightMeasureSpec=MeasureSpec.makeMeasureSpec(maxHeight,MeasureSpec.AT_MOST);
118+
}
119+
}
120+
121+
super.onMeasure(widthMeasureSpec,heightMeasureSpec);
122+
}
123+
}

material_preferences_library/src/main/res/layout/mpl__preference.xml

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,24 +36,28 @@
3636
android:paddingStart="?attr/listPreferredItemPaddingLeft"
3737
tools:ignore="NewApi">
3838

39-
<!--<android.support.v7.widget.LinearLayoutCompat-->
40-
<!--android:id="@+id/icon_frame"-->
41-
<!--android:layout_width="wrap_content"-->
42-
<!--android:layout_height="wrap_content"-->
43-
<!--android:layout_marginStart="-4dp"-->
44-
<!--android:minWidth="60dp"-->
45-
<!--android:gravity="start|center_vertical"-->
46-
<!--android:orientation="horizontal"-->
47-
<!--android:paddingEnd="12dp"-->
48-
<!--android:paddingTop="4dp"-->
49-
<!--android:paddingBottom="4dp">-->
50-
<!--<com.android.internal.widget.PreferenceImageView-->
51-
<!--android:id="@+id/icon"-->
52-
<!--android:layout_width="wrap_content"-->
53-
<!--android:layout_height="wrap_content"-->
54-
<!--android:maxWidth="48dp"-->
55-
<!--android:maxHeight="48dp" />-->
56-
<!--</android.support.v7.widget.LinearLayoutCompat>-->
39+
<android.support.v7.widget.LinearLayoutCompat
40+
android:id="@+id/icon_frame"
41+
android:layout_width="wrap_content"
42+
android:layout_height="wrap_content"
43+
android:layout_marginLeft="-4dp"
44+
android:layout_marginStart="-4dp"
45+
android:gravity="start|center_vertical"
46+
android:minWidth="60dp"
47+
android:orientation="horizontal"
48+
android:paddingBottom="4dp"
49+
android:paddingEnd="12dp"
50+
android:paddingRight="12dp"
51+
android:paddingTop="4dp">
52+
53+
<com.lb.material_preferences_library.internal.widget.PreferenceImageView
54+
android:id="@+id/icon"
55+
android:layout_width="wrap_content"
56+
android:layout_height="wrap_content"
57+
android:maxHeight="48dp"
58+
android:maxWidth="48dp"
59+
/>
60+
</android.support.v7.widget.LinearLayoutCompat>
5761

5862
<RelativeLayout
5963
android:layout_width="0px"

material_preferences_library/src/main/res/values/mpl__attrs.xml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<resources>
3+
<declare-styleable name="PreferenceImageView">
4+
<attr name="maxWidth" format="dimension"/>
5+
<attr name="maxHeight" format="dimension"/>
6+
</declare-styleable>
7+
38
<!--preferences:-->
9+
<!-- Base attributes available to Preference. -->
10+
<declare-styleable name="Preference">
11+
<!-- The optional icon for the preference -->
12+
<attr name="icon"/>
13+
</declare-styleable>
14+
415
<!-- Default style for DialogPreference. -->
516
<attr name="dialogPreferenceStyle" format="reference"/>
617
<!-- Default style for EditTextPreference. -->
@@ -63,7 +74,7 @@
6374
<!-- The summary for the Preference in a PreferenceActivity screen when the
6475
SwitchPreference is unchecked. If separate on/off summaries are not
6576
needed, the summary attribute can be used instead. -->
66-
<attr name="summaryOff" />
77+
<attr name="summaryOff"/>
6778
<!-- The text used on the switch itself when in the "on" state.
6879
This should be a very SHORT string, as it appears in a small space. -->
6980
<attr name="switchTextOn" format="string"/>
@@ -72,9 +83,8 @@
7283
<attr name="switchTextOff" format="string"/>
7384
<!-- The state (true for on, or false for off) that causes dependents to be disabled. By default,
7485
dependents will be disabled when this is unchecked, so the value of this preference is false. -->
75-
<attr name="disableDependentsState" />
86+
<attr name="disableDependentsState"/>
7687
</declare-styleable>
7788

7889

79-
8090
</resources>

0 commit comments

Comments
 (0)