Skip to content

Commit

Permalink
Added TextView support for RTL in custom method.
Browse files Browse the repository at this point in the history
  • Loading branch information
ankushyerawar committed Aug 21, 2020
1 parent 323c29d commit 1b55c1f
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 28 deletions.
14 changes: 10 additions & 4 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ private String generateVersionName() {
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])

implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test:runner:1.2.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.material:material:1.2.0'

implementation project(":floatingsnackbar")
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ public void onClick(View view) {

//Here you can pass String id
SnackBar.custom(view, R.string.app_name, SnackBar.LENGTH_LONG,
R.drawable.ic_custom, Color.DKGRAY, Color.WHITE).show();
R.drawable.ic_custom, Color.DKGRAY, Color.WHITE,true).show();

//Here you can pass String text
/*SnackBar.custom(view, "String Custom", SnackBar.LENGTH_LONG,
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ buildscript {

}
dependencies {
classpath 'com.android.tools.build:gradle:3.5.2'
classpath 'com.android.tools.build:gradle:3.5.3'

// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,28 @@ public static Snackbar normal(@NonNull View view, @StringRes int resId, @Duratio

@NonNull
public static Snackbar normal(@NonNull View view, CharSequence text, @Duration int length,
@DrawableRes int iconId) {
return customChar(iconId, view, text, length, Type.DEFAULT.getBackColor(), Type.DEFAULT.getTextColor());
@DrawableRes int iconId, boolean supportsRTL) {
return customChar(iconId, view, text, length, Type.DEFAULT.getBackColor(), Type.DEFAULT.getTextColor(), supportsRTL);
}

@NonNull
public static Snackbar normal(@NonNull View view, @StringRes int resId, @Duration int length,
@DrawableRes int iconId) {
return customRes(iconId, view, resId, length, Type.DEFAULT.getBackColor(), Type.DEFAULT.getTextColor());
@DrawableRes int iconId, boolean supportsRTL) {
return customRes(iconId, view, resId, length, Type.DEFAULT.getBackColor(), Type.DEFAULT.getTextColor(), supportsRTL);
}

@NonNull
public static Snackbar custom(@NonNull View view, CharSequence text, @Duration int length,
@DrawableRes int iconId, @ColorInt int backgroundColor,
@ColorInt int textColor) {
return customChar(iconId, view, text, length, backgroundColor, textColor);
@ColorInt int textColor, boolean supportsRTL) {
return customChar(iconId, view, text, length, backgroundColor, textColor, supportsRTL);
}

@NonNull
public static Snackbar custom(@NonNull View view, @StringRes int resId, @Duration int length,
@DrawableRes int iconId, @ColorInt int backgroundColor,
@ColorInt int textColor) {
return customRes(iconId, view, resId, length, backgroundColor, textColor);
@ColorInt int textColor, boolean supportsRTL) {
return customRes(iconId, view, resId, length, backgroundColor, textColor, supportsRTL);
}

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.ankushyerwar.floatingsnackbar.customization;

import android.graphics.Rect;
import android.graphics.drawable.GradientDrawable;
import android.util.Log;
import android.view.View;
Expand All @@ -16,6 +17,9 @@
import com.ankushyerwar.floatingsnackbar.utils.Type;
import com.google.android.material.snackbar.Snackbar;

import java.lang.reflect.Field;
import java.util.Objects;

import static com.ankushyerwar.floatingsnackbar.utils.Constants.TAG;
import static com.ankushyerwar.floatingsnackbar.utils.Constants.corner;
import static com.ankushyerwar.floatingsnackbar.utils.Constants.margin;
Expand Down Expand Up @@ -63,16 +67,16 @@ public static Snackbar customIcon(@NonNull View view, @StringRes int resId, @Dur

public static Snackbar customChar(@DrawableRes int iconId, @NonNull View view, CharSequence text,
@Duration int length, @ColorInt int backgroundColor,
@ColorInt int textColor) {
@ColorInt int textColor, @NonNull boolean supportsRTL) {

return customModify(makeSnack(view, text, length), iconId, textColor, backgroundColor);
return customModify(makeSnack(view, text, length), iconId, textColor, backgroundColor, supportsRTL);
}

public static Snackbar customRes(@DrawableRes int iconId, @NonNull View view, @StringRes int resId,
@Duration int length, @ColorInt int backgroundColor,
@ColorInt int textColor) {
@ColorInt int textColor, @NonNull boolean supportsRTL) {

return customModify(makeSnack(view, resId, length), iconId, textColor, backgroundColor);
return customModify(makeSnack(view, resId, length), iconId, textColor, backgroundColor, supportsRTL);
}

private static Snackbar modify(Snackbar snackbar, Type type, boolean withIcon) {
Expand All @@ -82,7 +86,8 @@ private static Snackbar modify(Snackbar snackbar, Type type, boolean withIcon) {

setTextStyle(snackBarView, type.getIcon(), withIcon);

snackBarView.setLayoutParams(setMargins(snackBarView));
//snackBarView.setLayoutParams(setMargins(snackBarView));
fixSnackBarMarginBottomBug(snackbar);

snackBarView.setBackground(snackBarView.getContext().getDrawable(type.getBackground()));

Expand All @@ -101,7 +106,8 @@ private static Snackbar modifyIcon(Snackbar snackbar, Type type, @DrawableRes in

setTextStyle(snackBarView, iconId, true);

snackBarView.setLayoutParams(setMargins(snackBarView));
//snackBarView.setLayoutParams(setMargins(snackBarView));
fixSnackBarMarginBottomBug(snackbar);

snackBarView.setBackground(snackBarView.getContext().getDrawable(type.getBackground()));

Expand All @@ -114,14 +120,15 @@ private static Snackbar modifyIcon(Snackbar snackbar, Type type, @DrawableRes in
}

private static Snackbar customModify(Snackbar snackbar, @DrawableRes int iconId, @ColorInt int textColor,
@ColorInt int backgroundColor) {
@ColorInt int backgroundColor, boolean supportsRTL) {
try {

final View snackBarView = snackbar.getView();

setCustomTextStyle(snackBarView, iconId, textColor);
setCustomTextStyle(snackBarView, iconId, textColor, supportsRTL);

snackBarView.setLayoutParams(setMargins(snackBarView));
//snackBarView.setLayoutParams(setMargins(snackBarView));
fixSnackBarMarginBottomBug(snackbar);

snackBarView.setBackground(setBackground(backgroundColor));

Expand All @@ -143,24 +150,46 @@ private static void setTextStyle(View view, @DrawableRes int resId, boolean with
}
}

private static void setCustomTextStyle(View view, @DrawableRes int resId, @ColorInt int textColor) {
private static void setCustomTextStyle(View view, @DrawableRes int resId, @ColorInt int textColor, boolean supportsRTL) {
TextView textView = view.findViewById(R.id.snackbar_text);
textView.setTextSize(textSize);
textView.setMaxLines(maxLines);
textView.setTextColor(textColor);
textView.setCompoundDrawablesWithIntrinsicBounds(resId, noVal, noVal, noVal);
textView.setCompoundDrawablePadding(textView.getResources().getDimensionPixelOffset(R.dimen.icon_padding));
if (supportsRTL) {
textView.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
textView.setCompoundDrawablesWithIntrinsicBounds(noVal, noVal, resId, noVal);
} else {
textView.setCompoundDrawablesWithIntrinsicBounds(resId, noVal, noVal, noVal);
}
}

private static ViewGroup.MarginLayoutParams setMargins(View view) {
/*private static ViewGroup.MarginLayoutParams setMargins(View view) {
ViewGroup.MarginLayoutParams marginLayoutParams = (ViewGroup.MarginLayoutParams)
view.getLayoutParams();
marginLayoutParams.setMargins(marginLayoutParams.leftMargin + margin,
marginLayoutParams.topMargin, marginLayoutParams.rightMargin + margin,
marginLayoutParams.bottomMargin + margin);
return marginLayoutParams;
}*/

private static void fixSnackBarMarginBottomBug(Object snackBar) {
try {
Class snackbarClass = Class.forName("com.google.android.material.snackbar.Snackbar");
Field originalMarginsField = Objects.requireNonNull(snackbarClass.getSuperclass()).getDeclaredField("originalMargins");
originalMarginsField.setAccessible(true);
Rect fixedOriginalMargins = new Rect();
fixedOriginalMargins.left = margin;
fixedOriginalMargins.right = margin;
fixedOriginalMargins.bottom = margin;
originalMarginsField.set(snackBar, fixedOriginalMargins);
} catch (IllegalAccessException | NoSuchFieldException | ClassNotFoundException ignored) {

}
}

private static GradientDrawable setBackground(int backgroundColor) {
Expand Down

0 comments on commit 1b55c1f

Please sign in to comment.