Skip to content

Commit e4213fe

Browse files
authored
Merge pull request #1 from konifar/support_rtl
Support RTL 🌐
2 parents 9e8f558 + 6beac4c commit e4213fe

File tree

9 files changed

+93
-41
lines changed

9 files changed

+93
-41
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ tv.setVectorDrawableRight(CompoundIconTextView.UNDEFINED_RESOURCE);
7979
|cit_drawableTop|Sets a drawable or vector drawable to top of TextView|
8080
|cit_drawableBottom|Sets a drawable or vector drawable to bottom of TextView|
8181
|cit_drawableRight|Sets a drawable or vector drawable to right of TextView|
82+
|cit_drawableStart|Sets a drawable or vector drawable to start of TextView (for RTL)|
83+
|cit_drawableEnd|Sets a drawable or vector drawable to end of TextView (for RTL)|
8284
|cit_iconWidth|Sets a width of icon|
8385
|cit_iconHeight|Sets a width of icon|
8486
|cit_iconColor|Sets a icon color|
@@ -131,4 +133,4 @@ See the License for the specific language governing permissions and
131133
limitations under the License.
132134
```
133135

134-
[preview]: /arts/preview.png
136+
[preview]: /arts/preview.jpg

arts/preview.jpg

32.4 KB
Loading

arts/preview.png

-53.5 KB
Binary file not shown.

compoundicontextview/src/main/java/com/github/aakira/compoundicontextview/CompoundIconTextView.java

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@
1818
import android.support.annotation.Nullable;
1919
import android.support.v4.content.ContextCompat;
2020
import android.support.v4.graphics.drawable.DrawableCompat;
21+
import android.support.v4.text.TextUtilsCompat;
22+
import android.support.v4.view.ViewCompat;
2123
import android.support.v7.content.res.AppCompatResources;
2224
import android.support.v7.widget.AppCompatTextView;
2325
import 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
}

compoundicontextview/src/main/res/values/attrs.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
<attr name="cit_drawableTop" format="integer" />
66
<attr name="cit_drawableBottom" format="integer" />
77
<attr name="cit_drawableRight" format="integer" />
8+
<attr name="cit_drawableStart" format="integer" />
9+
<attr name="cit_drawableEnd" format="integer" />
810
<attr name="cit_iconWidth" format="dimension" />
911
<attr name="cit_iconHeight" format="dimension" />
1012
<attr name="cit_iconColor" format="color" />
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<vector xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:width="24dp"
4+
android:height="24dp"
5+
android:viewportHeight="24.0"
6+
android:viewportWidth="24.0">
7+
<path
8+
android:fillColor="#000000"
9+
android:pathData="M11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2 0,0.68 0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2 0,-0.68 0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2 0,0.68 -0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2 0,-0.68 -0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z" />
10+
</vector>

sample/src/main/res/layout/activity_main.xml

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4+
xmlns:tools="http://schemas.android.com/tools"
45
android:layout_width="match_parent"
56
android:layout_height="match_parent"
67
android:background="#eaeaea"
@@ -24,7 +25,8 @@
2425
app:cit_drawableRight="@drawable/ic_android_black_24dp"
2526
app:cit_iconColor="#fff"
2627
app:cit_iconHeight="4dp"
27-
app:cit_iconWidth="4dp" />
28+
app:cit_iconWidth="4dp"
29+
tools:ignore="HardcodedText" />
2830

2931
<com.github.aakira.compoundicontextview.CompoundIconTextView
3032
android:id="@+id/compoundIconTextView2"
@@ -40,7 +42,8 @@
4042
app:cit_drawableRight="@drawable/ic_cloud_download_black_24px"
4143
app:cit_iconColor="#1565C0"
4244
app:cit_iconHeight="@dimen/icon_size"
43-
app:cit_iconWidth="@dimen/icon_size" />
45+
app:cit_iconWidth="@dimen/icon_size"
46+
tools:ignore="HardcodedText" />
4447

4548
<com.github.aakira.compoundicontextview.CompoundIconTextView
4649
android:layout_width="wrap_content"
@@ -54,7 +57,8 @@
5457
app:cit_drawableRight="@drawable/ic_volume_up_black_24px"
5558
app:cit_iconColor="#00695C"
5659
app:cit_iconHeight="@dimen/icon_size"
57-
app:cit_iconWidth="@dimen/icon_size" />
60+
app:cit_iconWidth="@dimen/icon_size"
61+
tools:ignore="HardcodedText" />
5862

5963
<com.github.aakira.compoundicontextview.CompoundIconTextView
6064
android:layout_width="wrap_content"
@@ -68,7 +72,8 @@
6872
app:cit_drawableTop="@drawable/ic_call_end_black_24px"
6973
app:cit_iconColor="#558B2F"
7074
app:cit_iconHeight="@dimen/icon_size"
71-
app:cit_iconWidth="@dimen/icon_size" />
75+
app:cit_iconWidth="@dimen/icon_size"
76+
tools:ignore="HardcodedText" />
7277

7378
<com.github.aakira.compoundicontextview.CompoundIconTextView
7479
android:layout_width="wrap_content"
@@ -82,5 +87,20 @@
8287
app:cit_drawableBottom="@drawable/ic_train_black_24px"
8388
app:cit_iconColor="#EF6C00"
8489
app:cit_iconHeight="@dimen/icon_size"
90+
app:cit_iconWidth="@dimen/icon_size"
91+
tools:ignore="HardcodedText" />
92+
93+
<com.github.aakira.compoundicontextview.CompoundIconTextView
94+
android:layout_width="wrap_content"
95+
android:layout_height="wrap_content"
96+
android:layout_marginTop="@dimen/margin_x_large"
97+
android:drawablePadding="@dimen/margin_small"
98+
android:gravity="center"
99+
android:text="@string/right_to_left"
100+
android:textColor="#795548"
101+
android:textSize="@dimen/text_large"
102+
app:cit_drawableStart="@drawable/ic_language_black_24px"
103+
app:cit_iconColor="#4E342E"
104+
app:cit_iconHeight="@dimen/icon_size"
85105
app:cit_iconWidth="@dimen/icon_size" />
86106
</LinearLayout>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<resources>
2+
<string name="right_to_left">من اليمين الى اليسار</string>
3+
</resources>
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<resources>
2-
<string name="app_name">CompoundIconTextView</string>
2+
<string name="app_name" translatable="false">CompoundIconTextView</string>
3+
<string name="right_to_left">Right to Left (in Arabic)</string>
34
</resources>

0 commit comments

Comments
 (0)