Skip to content

Commit c22da49

Browse files
committed
Add methods that allow to tint the icons of list items.
1 parent 7d96d13 commit c22da49

File tree

5 files changed

+230
-2
lines changed

5 files changed

+230
-2
lines changed

library/src/main/java/de/mrapp/android/dialog/AbstractListDialog.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import android.content.Context;
1717
import android.content.res.ColorStateList;
18+
import android.graphics.PorterDuff;
1819
import android.graphics.Typeface;
1920
import android.os.Bundle;
2021

@@ -91,6 +92,33 @@ public final Typeface getItemTypeface() {
9192
return decorator.getItemTypeface();
9293
}
9394

95+
@Nullable
96+
@Override
97+
public final ColorStateList getItemIconTintList() {
98+
return decorator.getItemIconTintList();
99+
}
100+
101+
@Override
102+
public final void setItemIconTint(final int color) {
103+
decorator.setItemIconTint(color);
104+
}
105+
106+
@Override
107+
public final void setItemIconTintList(@Nullable final ColorStateList tintList) {
108+
decorator.setItemIconTintList(tintList);
109+
}
110+
111+
@NonNull
112+
@Override
113+
public final PorterDuff.Mode getItemIconTintMode() {
114+
return decorator.getItemIconTintMode();
115+
}
116+
117+
@Override
118+
public final void setItemIconTintMode(@NonNull final PorterDuff.Mode mode) {
119+
decorator.setItemIconTintMode(mode);
120+
}
121+
94122
@Override
95123
public final void setItemTypeface(@NonNull final Typeface typeface) {
96124
decorator.setItemTypeface(typeface);

library/src/main/java/de/mrapp/android/dialog/adapter/ArrayRecyclerViewAdapter.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import android.content.Context;
1717
import android.content.res.ColorStateList;
1818
import android.content.res.Resources;
19+
import android.graphics.PorterDuff;
1920
import android.graphics.Typeface;
2021
import android.graphics.drawable.Drawable;
2122
import android.view.LayoutInflater;
@@ -28,6 +29,7 @@
2829
import androidx.annotation.NonNull;
2930
import androidx.annotation.Nullable;
3031
import androidx.core.app.ActivityCompat;
32+
import androidx.core.graphics.drawable.DrawableCompat;
3133
import androidx.recyclerview.widget.RecyclerView;
3234

3335
import de.mrapp.android.dialog.R;
@@ -100,6 +102,16 @@ public ViewHolder(@NonNull final View itemView) {
100102
*/
101103
private Typeface itemTypeface;
102104

105+
/**
106+
* The color state list, which is used to tint the adapter's list items.
107+
*/
108+
private ColorStateList itemTintList;
109+
110+
/**
111+
* The mode, which is used to tint the adapter's list items.
112+
*/
113+
private PorterDuff.Mode itemTintMode = PorterDuff.Mode.SRC_ATOP;
114+
103115
/**
104116
* Adapts the padding of a list item.
105117
*
@@ -152,6 +164,12 @@ private void adaptIcon(@NonNull final ViewHolder holder, final int position) {
152164
if (imageView != null) {
153165
Context context = imageView.getContext();
154166
Drawable icon = ActivityCompat.getDrawable(context, iconResourceIds[position]);
167+
168+
if (icon != null) {
169+
DrawableCompat.setTintList(icon, itemTintList);
170+
DrawableCompat.setTintMode(icon, itemTintMode);
171+
}
172+
155173
imageView.setImageDrawable(icon);
156174
}
157175
}
@@ -227,6 +245,29 @@ public void setItemTypeface(@NonNull final Typeface typeface) {
227245
notifyDataSetChanged();
228246
}
229247

248+
/**
249+
* Sets the color state list, which should be used to tint the adapter's items.
250+
*
251+
* @param colorStateList The color state list, which should be set, as an instance of the class
252+
* {@link ColorStateList} or null, if the items should not be tinted
253+
*/
254+
public void setItemIconTintList(@Nullable final ColorStateList colorStateList) {
255+
this.itemTintList = colorStateList;
256+
notifyDataSetChanged();
257+
}
258+
259+
/**
260+
* Sets the mode, which should be used to tint the adapter's items.
261+
*
262+
* @param mode The mode, which should be set, as a value of the enum {@link PorterDuff.Mode}.
263+
* The mode may not be null
264+
*/
265+
public void setItemIconTintMode(@NonNull final PorterDuff.Mode mode) {
266+
Condition.INSTANCE.ensureNotNull(mode, "The tint mode may not be null");
267+
this.itemTintMode = mode;
268+
notifyDataSetChanged();
269+
}
270+
230271
@Override
231272
public final int getItemCount() {
232273
return items.length;

library/src/main/java/de/mrapp/android/dialog/builder/AbstractListDialogBuilder.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import android.content.DialogInterface;
1818
import android.content.res.ColorStateList;
1919
import android.content.res.TypedArray;
20+
import android.graphics.PorterDuff;
2021
import android.graphics.Typeface;
2122
import android.widget.ListAdapter;
2223

@@ -138,6 +139,50 @@ public final BuilderType setItemTypeface(@NonNull final Typeface typeface) {
138139
return self();
139140
}
140141

142+
/**
143+
* Sets the color, which should be used to tint the icons of the list items of the dialog, which
144+
* is created by the builder.
145+
*
146+
* @param color
147+
* The color, which should be set, as an {@link Integer} value
148+
* @return The builder, the method has been called upon, as an instance of the generic type
149+
* BuilderType
150+
*/
151+
public final BuilderType setItemIconTint(@ColorInt final int color) {
152+
getProduct().setItemIconTint(color);
153+
return self();
154+
}
155+
156+
/**
157+
* Sets the color state list, which should be used to tint the icons of the list items of the
158+
* dialog, which is created by the builder.
159+
*
160+
* @param tintList
161+
* The color state list, which should be set, as an instance of the class {@link
162+
* ColorStateList} or null, if no color state list should be set
163+
* @return The builder, the method has been called upon, as an instance of the generic type
164+
* BuilderType
165+
*/
166+
public final BuilderType setItemIconTintList(@Nullable final ColorStateList tintList) {
167+
getProduct().setItemIconTintList(tintList);
168+
return self();
169+
}
170+
171+
/**
172+
* Sets the mode, which should be used to tint the icons of the list items of the dialog, which
173+
* is created by the builder.
174+
*
175+
* @param mode
176+
* The mode, which should be set, as a value of the enum {@link PorterDuff.Mode}. The
177+
* mode may not be null
178+
* @return The builder, the method has been called upon, as an instance of the generic type
179+
* BuilderType
180+
*/
181+
public final BuilderType setItemIconTintMode(@NonNull final PorterDuff.Mode mode) {
182+
getProduct().setItemIconTintMode(mode);
183+
return self();
184+
}
185+
141186
/**
142187
* Sets, whether the list item at a specific position of the dialog, which is created by the
143188
* builder, should be checked, or not.

library/src/main/java/de/mrapp/android/dialog/decorator/ListDialogDecorator.java

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import android.content.DialogInterface;
1717
import android.content.res.ColorStateList;
18+
import android.graphics.PorterDuff;
1819
import android.graphics.Typeface;
1920
import android.os.Bundle;
2021
import android.view.LayoutInflater;
@@ -112,15 +113,25 @@ public class ListDialogDecorator extends AbstractDialogDecorator<ButtonBarDialog
112113
private boolean inflatedCustomView;
113114

114115
/**
115-
* The color of the list items of the dialog.
116+
* The text color of the dialog's list items.
116117
*/
117118
private ColorStateList itemColor;
118119

119120
/**
120-
* The typeface of the list items of the dialog.
121+
* The typeface of the dialog's list items.
121122
*/
122123
private Typeface itemTypeface;
123124

125+
/**
126+
* The color state list, which is used to tint the icons of the dialog's list items.
127+
*/
128+
private ColorStateList itemIconTintList;
129+
130+
/**
131+
* The mode, which is used to tint the icons of the dialog's list items.
132+
*/
133+
private PorterDuff.Mode itemIconTintMode = PorterDuff.Mode.SRC_ATOP;
134+
124135
/**
125136
* The adapter, which is used to manage the list items of the dialog.
126137
*/
@@ -209,6 +220,8 @@ private void attachAdapter() {
209220
initializeSelectionListener();
210221
adaptItemColor();
211222
adaptItemTypeface();
223+
adaptItemIconTintList();
224+
adaptItemIconTintMode();
212225
} else {
213226
if (inflatedCustomView) {
214227
getDialog().setView(null);
@@ -302,6 +315,32 @@ private void adaptItemTypeface() {
302315
}
303316
}
304317

318+
/**
319+
* Adapts the color state list, which is used to tint the icons of the dialog's list items.
320+
*/
321+
private void adaptItemIconTintList() {
322+
if (adapter != null) {
323+
RecyclerView.Adapter<?> wrappedAdapter = adapter.getWrappedAdapter();
324+
325+
if (wrappedAdapter instanceof ArrayRecyclerViewAdapter) {
326+
((ArrayRecyclerViewAdapter) wrappedAdapter).setItemIconTintList(itemIconTintList);
327+
}
328+
}
329+
}
330+
331+
/**
332+
* Adapts the mode, which is used to tint the icons of the dialog's list items.
333+
*/
334+
private void adaptItemIconTintMode() {
335+
if (adapter != null) {
336+
RecyclerView.Adapter<?> wrappedAdapter = adapter.getWrappedAdapter();
337+
338+
if (wrappedAdapter instanceof ArrayRecyclerViewAdapter) {
339+
((ArrayRecyclerViewAdapter) wrappedAdapter).setItemIconTintMode(itemIconTintMode);
340+
}
341+
}
342+
}
343+
305344
/**
306345
* Creates a new decorator, which allows to modify the view hierarchy of a dialog, which is
307346
* designed according to Android 5's Material Design guidelines even on pre-Lollipop devices and
@@ -355,6 +394,36 @@ public final Typeface getItemTypeface() {
355394
return itemTypeface;
356395
}
357396

397+
@Nullable
398+
@Override
399+
public final ColorStateList getItemIconTintList() {
400+
return itemIconTintList;
401+
}
402+
403+
@Override
404+
public final void setItemIconTint(@ColorInt final int color) {
405+
setItemIconTintList(ColorStateList.valueOf(color));
406+
}
407+
408+
@Override
409+
public final void setItemIconTintList(@Nullable final ColorStateList tintList) {
410+
this.itemIconTintList = tintList;
411+
adaptItemIconTintList();
412+
}
413+
414+
@NonNull
415+
@Override
416+
public final PorterDuff.Mode getItemIconTintMode() {
417+
return itemIconTintMode;
418+
}
419+
420+
@Override
421+
public final void setItemIconTintMode(@NonNull final PorterDuff.Mode mode) {
422+
Condition.INSTANCE.ensureNotNull(mode, "The tint mode may not be null");
423+
this.itemIconTintMode = mode;
424+
adaptItemIconTintMode();
425+
}
426+
358427
@Override
359428
public final int getItemCount() {
360429
return adapter != null ? adapter.getItemCount() : 0;

library/src/main/java/de/mrapp/android/dialog/model/ListDialogDecorator.java

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
import android.content.DialogInterface;
1717
import android.content.res.ColorStateList;
18+
import android.graphics.PorterDuff;
1819
import android.graphics.Typeface;
1920

2021
import androidx.annotation.ArrayRes;
@@ -95,6 +96,50 @@ public interface ListDialogDecorator extends Dialog {
9596
@Nullable
9697
Typeface getItemTypeface();
9798

99+
/**
100+
* Returns the color state list, which is used to tint the icons of the dialog's list items.
101+
*
102+
* @return The color state list, which is used to tint the icons of the dialog's list items, as
103+
* an instance of the class {@link ColorStateList} or null, if no color state list has been set
104+
*/
105+
@Nullable
106+
ColorStateList getItemIconTintList();
107+
108+
/**
109+
* Sets the color, which should be used to tint the icons of the dialog's list items.
110+
*
111+
* @param color
112+
* The color, which should be set, as an {@link Integer} value
113+
*/
114+
void setItemIconTint(@ColorInt int color);
115+
116+
/**
117+
* Sets the color state list, which should be used to tint the icons of the dialog's list items.
118+
*
119+
* @param tintList
120+
* The color state list, which should be set, as an instance of the class {@link
121+
* ColorStateList} or null, if no color state list should be set
122+
*/
123+
void setItemIconTintList(@Nullable ColorStateList tintList);
124+
125+
/**
126+
* Returns the mode, which is used to tint the icons of the dialog's list items.
127+
*
128+
* @return The mode, which is used to tint the icons of the dialog's list items, as a value of
129+
* the enum {@link PorterDuff.Mode}. The mode may not be null
130+
*/
131+
@NonNull
132+
PorterDuff.Mode getItemIconTintMode();
133+
134+
/**
135+
* Sets the mode, which should be used to tint the icons of the dialog's list items.
136+
*
137+
* @param mode
138+
* The mode, which should be set, as a value of the enum {@link PorterDuff.Mode}. The
139+
* mode may not be null
140+
*/
141+
void setItemIconTintMode(@NonNull PorterDuff.Mode mode);
142+
98143
/**
99144
* Returns the number of list items that are shown by the dialog.
100145
*

0 commit comments

Comments
 (0)