18
18
import android .support .annotation .Nullable ;
19
19
import android .support .v4 .content .ContextCompat ;
20
20
import android .support .v4 .graphics .drawable .DrawableCompat ;
21
+ import android .support .v4 .text .TextUtilsCompat ;
22
+ import android .support .v4 .view .ViewCompat ;
21
23
import android .support .v7 .content .res .AppCompatResources ;
22
24
import android .support .v7 .widget .AppCompatTextView ;
23
25
import android .util .AttributeSet ;
24
26
27
+ import java .util .Locale ;
28
+
25
29
/**
26
30
* You can use a vector drawable in TextView instead of drawableLeft, drawableTop, drawableRight and
27
31
* drawableBottom.
@@ -80,6 +84,12 @@ public CompoundIconTextView(Context context, @Nullable AttributeSet attrs, int d
80
84
drawableResIds [INDEX_TOP ] = a .getResourceId (R .styleable .CompoundIconTextView_cit_drawableTop , UNDEFINED_RESOURCE );
81
85
drawableResIds [INDEX_RIGHT ] = a .getResourceId (R .styleable .CompoundIconTextView_cit_drawableRight , UNDEFINED_RESOURCE );
82
86
drawableResIds [INDEX_BOTTOM ] = a .getResourceId (R .styleable .CompoundIconTextView_cit_drawableBottom , UNDEFINED_RESOURCE );
87
+ if (a .hasValue (R .styleable .CompoundIconTextView_cit_drawableStart )) {
88
+ drawableResIds [isRtl () ? INDEX_RIGHT : INDEX_LEFT ] = a .getResourceId (R .styleable .CompoundIconTextView_cit_drawableStart , UNDEFINED_RESOURCE );
89
+ }
90
+ if (a .hasValue (R .styleable .CompoundIconTextView_cit_drawableEnd )) {
91
+ drawableResIds [isRtl () ? INDEX_LEFT : INDEX_RIGHT ] = a .getResourceId (R .styleable .CompoundIconTextView_cit_drawableEnd , UNDEFINED_RESOURCE );
92
+ }
83
93
iconWidth = a .getDimensionPixelSize (R .styleable .CompoundIconTextView_cit_iconWidth , UNDEFINED_RESOURCE );
84
94
iconHeight = a .getDimensionPixelSize (R .styleable .CompoundIconTextView_cit_iconHeight , UNDEFINED_RESOURCE );
85
95
iconColor = a .getColor (R .styleable .CompoundIconTextView_cit_iconColor , UNDEFINED_RESOURCE );
@@ -103,52 +113,42 @@ public CompoundIconTextView(Context context, @Nullable AttributeSet attrs, int d
103
113
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
104
114
*/
105
115
public void setVectorDrawableLeft (@ DrawableRes final int resourceId ) {
106
- if (resourceId == UNDEFINED_RESOURCE ) {
107
- drawables [INDEX_LEFT ] = null ;
108
- drawableResIds [INDEX_LEFT ] = UNDEFINED_RESOURCE ;
109
- updateIcons ();
110
- } else {
111
- setVectorDrawable (INDEX_LEFT , resourceId );
112
- }
116
+ setVectorDrawable (INDEX_LEFT , resourceId );
113
117
}
114
118
115
119
/**
116
120
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
117
121
*/
118
122
public void setVectorDrawableTop (@ DrawableRes final int resourceId ) {
119
- if (resourceId == UNDEFINED_RESOURCE ) {
120
- drawables [INDEX_TOP ] = null ;
121
- drawableResIds [INDEX_TOP ] = UNDEFINED_RESOURCE ;
122
- updateIcons ();
123
- } else {
124
- setVectorDrawable (INDEX_TOP , resourceId );
125
- }
123
+ setVectorDrawable (INDEX_TOP , resourceId );
126
124
}
127
125
128
126
/**
129
127
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
130
128
*/
131
129
public void setVectorDrawableRight (@ DrawableRes final int resourceId ) {
132
- if (resourceId == UNDEFINED_RESOURCE ) {
133
- drawables [INDEX_RIGHT ] = null ;
134
- drawableResIds [INDEX_RIGHT ] = UNDEFINED_RESOURCE ;
135
- updateIcons ();
136
- } else {
137
- setVectorDrawable (INDEX_RIGHT , resourceId );
138
- }
130
+ setVectorDrawable (INDEX_RIGHT , resourceId );
139
131
}
140
132
141
133
/**
142
134
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
143
135
*/
144
136
public void setVectorDrawableBottom (@ DrawableRes final int resourceId ) {
145
- if (resourceId == UNDEFINED_RESOURCE ) {
146
- drawables [INDEX_BOTTOM ] = null ;
147
- drawableResIds [INDEX_BOTTOM ] = UNDEFINED_RESOURCE ;
148
- updateIcons ();
149
- } else {
150
- setVectorDrawable (INDEX_BOTTOM , resourceId );
151
- }
137
+ setVectorDrawable (INDEX_BOTTOM , resourceId );
138
+ }
139
+
140
+ /**
141
+ * @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
142
+ */
143
+ public void setVectorDrawableStart (@ DrawableRes final int resourceId ) {
144
+ setVectorDrawable (isRtl () ? INDEX_RIGHT : INDEX_LEFT , resourceId );
145
+ }
146
+
147
+ /**
148
+ * @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
149
+ */
150
+ public void setVectorDrawableEnd (@ DrawableRes final int resourceId ) {
151
+ setVectorDrawable (isRtl () ? INDEX_LEFT : INDEX_RIGHT , resourceId );
152
152
}
153
153
154
154
/**
@@ -176,7 +176,7 @@ public void setIconColor(@ColorInt final int color) {
176
176
/**
177
177
* Change drawable icon size
178
178
*
179
- * @param widthRes Set width resource id
179
+ * @param widthRes Set width resource id
180
180
* @param heightRes Set height resource id
181
181
*/
182
182
public void setIconSizeResource (@ DimenRes final int widthRes , @ DimenRes final int heightRes ) {
@@ -187,7 +187,7 @@ public void setIconSizeResource(@DimenRes final int widthRes, @DimenRes final in
187
187
/**
188
188
* Change drawable icon size
189
189
*
190
- * @param width Set width size
190
+ * @param width Set width size
191
191
* @param height Set height size
192
192
*/
193
193
public void setIconSize (@ Dimension final int width , @ Dimension final int height ) {
@@ -218,10 +218,16 @@ private void setColorFilter(final int index, @ColorInt final int color) {
218
218
}
219
219
220
220
private void setVectorDrawable (final int index , @ DrawableRes final int resourceId ) {
221
- checkHasIconSize ();
222
- setDrawable (index , resourceId );
223
- drawableResIds [index ] = resourceId ;
224
- updateIcons ();
221
+ if (resourceId == UNDEFINED_RESOURCE ) {
222
+ drawables [index ] = null ;
223
+ drawableResIds [index ] = UNDEFINED_RESOURCE ;
224
+ updateIcons ();
225
+ } else {
226
+ checkHasIconSize ();
227
+ setDrawable (index , resourceId );
228
+ drawableResIds [index ] = resourceId ;
229
+ updateIcons ();
230
+ }
225
231
}
226
232
227
233
private void updateIcons () {
@@ -278,7 +284,7 @@ private static Bitmap drawable2Bitmap(final Drawable drawable, final int iconWid
278
284
* {@link android.content.res.Resources} or a {@link android.content.res.TypedArray}.
279
285
*/
280
286
private static void fixDrawable (@ NonNull final Drawable drawable ) {
281
- if (Build .VERSION .SDK_INT == 21
287
+ if (Build .VERSION .SDK_INT == Build . VERSION_CODES . LOLLIPOP
282
288
&& VECTOR_DRAWABLE_CLAZZ_NAME .equals (drawable .getClass ().getName ())) {
283
289
fixVectorDrawableTinting (drawable );
284
290
}
@@ -302,4 +308,12 @@ private static void fixVectorDrawableTinting(final Drawable drawable) {
302
308
// Now set the original state
303
309
drawable .setState (originalState );
304
310
}
311
+
312
+ private boolean isRtl () {
313
+ Resources resources = getContext ().getResources ();
314
+ Locale locale = Build .VERSION .SDK_INT >= Build .VERSION_CODES .N
315
+ ? resources .getConfiguration ().getLocales ().getFirstMatch (resources .getAssets ().getLocales ())
316
+ : resources .getConfiguration ().locale ;
317
+ return TextUtilsCompat .getLayoutDirectionFromLocale (locale ) == ViewCompat .LAYOUT_DIRECTION_RTL ;
318
+ }
305
319
}
0 commit comments