Skip to content

Commit

Permalink
Merge pull request AAkira#1 from konifar/support_rtl
Browse files Browse the repository at this point in the history
Support RTL ๐ŸŒ
  • Loading branch information
AAkira committed Jun 7, 2017
2 parents 9e8f558 + 6beac4c commit e4213fe
Show file tree
Hide file tree
Showing 9 changed files with 93 additions and 41 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ tv.setVectorDrawableRight(CompoundIconTextView.UNDEFINED_RESOURCE);
|cit_drawableTop|Sets a drawable or vector drawable to top of TextView|
|cit_drawableBottom|Sets a drawable or vector drawable to bottom of TextView|
|cit_drawableRight|Sets a drawable or vector drawable to right of TextView|
|cit_drawableStart|Sets a drawable or vector drawable to start of TextView (for RTL)|
|cit_drawableEnd|Sets a drawable or vector drawable to end of TextView (for RTL)|
|cit_iconWidth|Sets a width of icon|
|cit_iconHeight|Sets a width of icon|
|cit_iconColor|Sets a icon color|
Expand Down Expand Up @@ -131,4 +133,4 @@ See the License for the specific language governing permissions and
limitations under the License.
```

[preview]: /arts/preview.png
[preview]: /arts/preview.jpg
Binary file added arts/preview.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed arts/preview.png
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v4.graphics.drawable.DrawableCompat;
import android.support.v4.text.TextUtilsCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.content.res.AppCompatResources;
import android.support.v7.widget.AppCompatTextView;
import android.util.AttributeSet;

import java.util.Locale;

/**
* You can use a vector drawable in TextView instead of drawableLeft, drawableTop, drawableRight and
* drawableBottom.
Expand Down Expand Up @@ -80,6 +84,12 @@ public CompoundIconTextView(Context context, @Nullable AttributeSet attrs, int d
drawableResIds[INDEX_TOP] = a.getResourceId(R.styleable.CompoundIconTextView_cit_drawableTop, UNDEFINED_RESOURCE);
drawableResIds[INDEX_RIGHT] = a.getResourceId(R.styleable.CompoundIconTextView_cit_drawableRight, UNDEFINED_RESOURCE);
drawableResIds[INDEX_BOTTOM] = a.getResourceId(R.styleable.CompoundIconTextView_cit_drawableBottom, UNDEFINED_RESOURCE);
if (a.hasValue(R.styleable.CompoundIconTextView_cit_drawableStart)) {
drawableResIds[isRtl() ? INDEX_RIGHT : INDEX_LEFT] = a.getResourceId(R.styleable.CompoundIconTextView_cit_drawableStart, UNDEFINED_RESOURCE);
}
if (a.hasValue(R.styleable.CompoundIconTextView_cit_drawableEnd)) {
drawableResIds[isRtl() ? INDEX_LEFT : INDEX_RIGHT] = a.getResourceId(R.styleable.CompoundIconTextView_cit_drawableEnd, UNDEFINED_RESOURCE);
}
iconWidth = a.getDimensionPixelSize(R.styleable.CompoundIconTextView_cit_iconWidth, UNDEFINED_RESOURCE);
iconHeight = a.getDimensionPixelSize(R.styleable.CompoundIconTextView_cit_iconHeight, UNDEFINED_RESOURCE);
iconColor = a.getColor(R.styleable.CompoundIconTextView_cit_iconColor, UNDEFINED_RESOURCE);
Expand All @@ -103,52 +113,42 @@ public CompoundIconTextView(Context context, @Nullable AttributeSet attrs, int d
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
*/
public void setVectorDrawableLeft(@DrawableRes final int resourceId) {
if (resourceId == UNDEFINED_RESOURCE) {
drawables[INDEX_LEFT] = null;
drawableResIds[INDEX_LEFT] = UNDEFINED_RESOURCE;
updateIcons();
} else {
setVectorDrawable(INDEX_LEFT, resourceId);
}
setVectorDrawable(INDEX_LEFT, resourceId);
}

/**
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
*/
public void setVectorDrawableTop(@DrawableRes final int resourceId) {
if (resourceId == UNDEFINED_RESOURCE) {
drawables[INDEX_TOP] = null;
drawableResIds[INDEX_TOP] = UNDEFINED_RESOURCE;
updateIcons();
} else {
setVectorDrawable(INDEX_TOP, resourceId);
}
setVectorDrawable(INDEX_TOP, resourceId);
}

/**
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
*/
public void setVectorDrawableRight(@DrawableRes final int resourceId) {
if (resourceId == UNDEFINED_RESOURCE) {
drawables[INDEX_RIGHT] = null;
drawableResIds[INDEX_RIGHT] = UNDEFINED_RESOURCE;
updateIcons();
} else {
setVectorDrawable(INDEX_RIGHT, resourceId);
}
setVectorDrawable(INDEX_RIGHT, resourceId);
}

/**
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
*/
public void setVectorDrawableBottom(@DrawableRes final int resourceId) {
if (resourceId == UNDEFINED_RESOURCE) {
drawables[INDEX_BOTTOM] = null;
drawableResIds[INDEX_BOTTOM] = UNDEFINED_RESOURCE;
updateIcons();
} else {
setVectorDrawable(INDEX_BOTTOM, resourceId);
}
setVectorDrawable(INDEX_BOTTOM, resourceId);
}

/**
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
*/
public void setVectorDrawableStart(@DrawableRes final int resourceId) {
setVectorDrawable(isRtl() ? INDEX_RIGHT : INDEX_LEFT, resourceId);
}

/**
* @param resourceId Set the {@link CompoundIconTextView#UNDEFINED_RESOURCE} if you want clear icon.
*/
public void setVectorDrawableEnd(@DrawableRes final int resourceId) {
setVectorDrawable(isRtl() ? INDEX_LEFT : INDEX_RIGHT, resourceId);
}

/**
Expand Down Expand Up @@ -176,7 +176,7 @@ public void setIconColor(@ColorInt final int color) {
/**
* Change drawable icon size
*
* @param widthRes Set width resource id
* @param widthRes Set width resource id
* @param heightRes Set height resource id
*/
public void setIconSizeResource(@DimenRes final int widthRes, @DimenRes final int heightRes) {
Expand All @@ -187,7 +187,7 @@ public void setIconSizeResource(@DimenRes final int widthRes, @DimenRes final in
/**
* Change drawable icon size
*
* @param width Set width size
* @param width Set width size
* @param height Set height size
*/
public void setIconSize(@Dimension final int width, @Dimension final int height) {
Expand Down Expand Up @@ -218,10 +218,16 @@ private void setColorFilter(final int index, @ColorInt final int color) {
}

private void setVectorDrawable(final int index, @DrawableRes final int resourceId) {
checkHasIconSize();
setDrawable(index, resourceId);
drawableResIds[index] = resourceId;
updateIcons();
if (resourceId == UNDEFINED_RESOURCE) {
drawables[index] = null;
drawableResIds[index] = UNDEFINED_RESOURCE;
updateIcons();
} else {
checkHasIconSize();
setDrawable(index, resourceId);
drawableResIds[index] = resourceId;
updateIcons();
}
}

private void updateIcons() {
Expand Down Expand Up @@ -278,7 +284,7 @@ private static Bitmap drawable2Bitmap(final Drawable drawable, final int iconWid
* {@link android.content.res.Resources} or a {@link android.content.res.TypedArray}.
*/
private static void fixDrawable(@NonNull final Drawable drawable) {
if (Build.VERSION.SDK_INT == 21
if (Build.VERSION.SDK_INT == Build.VERSION_CODES.LOLLIPOP
&& VECTOR_DRAWABLE_CLAZZ_NAME.equals(drawable.getClass().getName())) {
fixVectorDrawableTinting(drawable);
}
Expand All @@ -302,4 +308,12 @@ private static void fixVectorDrawableTinting(final Drawable drawable) {
// Now set the original state
drawable.setState(originalState);
}

private boolean isRtl() {
Resources resources = getContext().getResources();
Locale locale = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N
? resources.getConfiguration().getLocales().getFirstMatch(resources.getAssets().getLocales())
: resources.getConfiguration().locale;
return TextUtilsCompat.getLayoutDirectionFromLocale(locale) == ViewCompat.LAYOUT_DIRECTION_RTL;
}
}
2 changes: 2 additions & 0 deletions compoundicontextview/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<attr name="cit_drawableTop" format="integer" />
<attr name="cit_drawableBottom" format="integer" />
<attr name="cit_drawableRight" format="integer" />
<attr name="cit_drawableStart" format="integer" />
<attr name="cit_drawableEnd" format="integer" />
<attr name="cit_iconWidth" format="dimension" />
<attr name="cit_iconHeight" format="dimension" />
<attr name="cit_iconColor" format="color" />
Expand Down
10 changes: 10 additions & 0 deletions sample/src/main/res/drawable/ic_language_black_24px.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<vector xmlns:android="http:https://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24.0"
android:viewportWidth="24.0">
<path
android:fillColor="#000000"
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" />
</vector>
28 changes: 24 additions & 4 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http:https://schemas.android.com/apk/res/android"
xmlns:app="http:https://schemas.android.com/apk/res-auto"
xmlns:tools="http:https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#eaeaea"
Expand All @@ -24,7 +25,8 @@
app:cit_drawableRight="@drawable/ic_android_black_24dp"
app:cit_iconColor="#fff"
app:cit_iconHeight="4dp"
app:cit_iconWidth="4dp" />
app:cit_iconWidth="4dp"
tools:ignore="HardcodedText" />

<com.github.aakira.compoundicontextview.CompoundIconTextView
android:id="@+id/compoundIconTextView2"
Expand All @@ -40,7 +42,8 @@
app:cit_drawableRight="@drawable/ic_cloud_download_black_24px"
app:cit_iconColor="#1565C0"
app:cit_iconHeight="@dimen/icon_size"
app:cit_iconWidth="@dimen/icon_size" />
app:cit_iconWidth="@dimen/icon_size"
tools:ignore="HardcodedText" />

<com.github.aakira.compoundicontextview.CompoundIconTextView
android:layout_width="wrap_content"
Expand All @@ -54,7 +57,8 @@
app:cit_drawableRight="@drawable/ic_volume_up_black_24px"
app:cit_iconColor="#00695C"
app:cit_iconHeight="@dimen/icon_size"
app:cit_iconWidth="@dimen/icon_size" />
app:cit_iconWidth="@dimen/icon_size"
tools:ignore="HardcodedText" />

<com.github.aakira.compoundicontextview.CompoundIconTextView
android:layout_width="wrap_content"
Expand All @@ -68,7 +72,8 @@
app:cit_drawableTop="@drawable/ic_call_end_black_24px"
app:cit_iconColor="#558B2F"
app:cit_iconHeight="@dimen/icon_size"
app:cit_iconWidth="@dimen/icon_size" />
app:cit_iconWidth="@dimen/icon_size"
tools:ignore="HardcodedText" />

<com.github.aakira.compoundicontextview.CompoundIconTextView
android:layout_width="wrap_content"
Expand All @@ -82,5 +87,20 @@
app:cit_drawableBottom="@drawable/ic_train_black_24px"
app:cit_iconColor="#EF6C00"
app:cit_iconHeight="@dimen/icon_size"
app:cit_iconWidth="@dimen/icon_size"
tools:ignore="HardcodedText" />

<com.github.aakira.compoundicontextview.CompoundIconTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/margin_x_large"
android:drawablePadding="@dimen/margin_small"
android:gravity="center"
android:text="@string/right_to_left"
android:textColor="#795548"
android:textSize="@dimen/text_large"
app:cit_drawableStart="@drawable/ic_language_black_24px"
app:cit_iconColor="#4E342E"
app:cit_iconHeight="@dimen/icon_size"
app:cit_iconWidth="@dimen/icon_size" />
</LinearLayout>
3 changes: 3 additions & 0 deletions sample/src/main/res/values-ar/strings.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<resources>
<string name="right_to_left">ู…ู† ุงู„ูŠู…ูŠู† ุงู„ู‰ ุงู„ูŠุณุงุฑ</string>
</resources>
3 changes: 2 additions & 1 deletion sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<resources>
<string name="app_name">CompoundIconTextView</string>
<string name="app_name" translatable="false">CompoundIconTextView</string>
<string name="right_to_left">Right to Left (in Arabic)</string>
</resources>

0 comments on commit e4213fe

Please sign in to comment.