1818import android .support .annotation .Nullable ;
1919import android .support .v4 .content .ContextCompat ;
2020import android .support .v4 .graphics .drawable .DrawableCompat ;
21+ import android .support .v4 .text .TextUtilsCompat ;
22+ import android .support .v4 .view .ViewCompat ;
2123import android .support .v7 .content .res .AppCompatResources ;
2224import android .support .v7 .widget .AppCompatTextView ;
2325import android .util .AttributeSet ;
2426
27+ import java .util .Locale ;
28+
2529/**
2630 * You can use a vector drawable in TextView instead of drawableLeft, drawableTop, drawableRight and
2731 * drawableBottom.
@@ -80,6 +84,12 @@ public CompoundIconTextView(Context context, @Nullable AttributeSet attrs, int d
8084 drawableResIds [INDEX_TOP ] = a .getResourceId (R .styleable .CompoundIconTextView_cit_drawableTop , UNDEFINED_RESOURCE );
8185 drawableResIds [INDEX_RIGHT ] = a .getResourceId (R .styleable .CompoundIconTextView_cit_drawableRight , UNDEFINED_RESOURCE );
8286 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+ }
8393 iconWidth = a .getDimensionPixelSize (R .styleable .CompoundIconTextView_cit_iconWidth , UNDEFINED_RESOURCE );
8494 iconHeight = a .getDimensionPixelSize (R .styleable .CompoundIconTextView_cit_iconHeight , UNDEFINED_RESOURCE );
8595 iconColor = a .getColor (R .styleable .CompoundIconTextView_cit_iconColor , UNDEFINED_RESOURCE );
@@ -103,52 +113,42 @@ public CompoundIconTextView(Context context, @Nullable AttributeSet attrs, int d
103113 * @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
104114 */
105115 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 );
113117 }
114118
115119 /**
116120 * @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
117121 */
118122 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 );
126124 }
127125
128126 /**
129127 * @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
130128 */
131129 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 );
139131 }
140132
141133 /**
142134 * @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
143135 */
144136 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 );
152152 }
153153
154154 /**
@@ -176,7 +176,7 @@ public void setIconColor(@ColorInt final int color) {
176176 /**
177177 * Change drawable icon size
178178 *
179- * @param widthRes Set width resource id
179+ * @param widthRes Set width resource id
180180 * @param heightRes Set height resource id
181181 */
182182 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
187187 /**
188188 * Change drawable icon size
189189 *
190- * @param width Set width size
190+ * @param width Set width size
191191 * @param height Set height size
192192 */
193193 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) {
218218 }
219219
220220 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+ }
225231 }
226232
227233 private void updateIcons () {
@@ -278,7 +284,7 @@ private static Bitmap drawable2Bitmap(final Drawable drawable, final int iconWid
278284 * {@link android.content.res.Resources} or a {@link android.content.res.TypedArray}.
279285 */
280286 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
282288 && VECTOR_DRAWABLE_CLAZZ_NAME .equals (drawable .getClass ().getName ())) {
283289 fixVectorDrawableTinting (drawable );
284290 }
@@ -302,4 +308,12 @@ private static void fixVectorDrawableTinting(final Drawable drawable) {
302308 // Now set the original state
303309 drawable .setState (originalState );
304310 }
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+ }
305319}
0 commit comments