From 1f952d8fc1c13caea9ce614610e8fb7eda6e2377 Mon Sep 17 00:00:00 2001 From: shubhamnandanwar Date: Thu, 25 Jul 2019 17:35:23 +0530 Subject: [PATCH] Added RTL Support --- .../bubblenavigation/BubbleToggleView.java | 98 ++++++++++++++++++- 1 file changed, 96 insertions(+), 2 deletions(-) diff --git a/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleToggleView.java b/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleToggleView.java index a511477..2b66431 100644 --- a/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleToggleView.java +++ b/bubblenavigation/src/main/java/com/gauravk/bubblenavigation/BubbleToggleView.java @@ -21,6 +21,8 @@ import android.graphics.drawable.Drawable; import android.graphics.drawable.TransitionDrawable; import android.os.Build; +import android.os.Parcel; +import android.os.Parcelable; import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; import android.support.graphics.drawable.VectorDrawableCompat; @@ -30,6 +32,7 @@ import android.util.AttributeSet; import android.util.TypedValue; import android.view.Gravity; +import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; import android.widget.RelativeLayout; @@ -41,8 +44,10 @@ * * @author Gaurav Kumar */ + public class BubbleToggleView extends RelativeLayout { + private int mLayoutDirection = ViewCompat.LAYOUT_DIRECTION_LTR; private static final String TAG = "BNI_View"; private static final int DEFAULT_ANIM_DURATION = 300; @@ -78,6 +83,93 @@ public BubbleToggleView(Context context, @Nullable AttributeSet attrs, int defSt init(context, attrs); } + @Override + public void onRtlPropertiesChanged(int layoutDirection) { + super.onRtlPropertiesChanged(layoutDirection); + int viewCompatLayoutDirection = layoutDirection == View.LAYOUT_DIRECTION_RTL ? ViewCompat.LAYOUT_DIRECTION_RTL : ViewCompat.LAYOUT_DIRECTION_LTR; + if (viewCompatLayoutDirection != mLayoutDirection) { + mLayoutDirection = viewCompatLayoutDirection; + } + + if (isRtl()) + setGravity(Gravity.START); + else setGravity(Gravity.CENTER); + } + + private boolean isRtl() { + return mLayoutDirection == ViewCompat.LAYOUT_DIRECTION_RTL; + } + + @Override + public Parcelable onSaveInstanceState() { + Parcelable superState = super.onSaveInstanceState(); + return new SavedState(superState, mLayoutDirection); + } + + @Override + public void onRestoreInstanceState(Parcelable state) { + if (!(state instanceof SavedState)) { + super.onRestoreInstanceState(state); + return; + } + + SavedState ss = (SavedState) state; + mLayoutDirection = ss.mLayoutDirection; + super.onRestoreInstanceState(ss.mViewPagerSavedState); + } + + + public static class SavedState implements Parcelable { + + private final Parcelable mViewPagerSavedState; + private final int mLayoutDirection; + + private SavedState(Parcelable viewPagerSavedState, int layoutDirection) { + mViewPagerSavedState = viewPagerSavedState; + mLayoutDirection = layoutDirection; + } + + private SavedState(Parcel in, ClassLoader loader) { + if (loader == null) { + loader = getClass().getClassLoader(); + } + mViewPagerSavedState = in.readParcelable(loader); + mLayoutDirection = in.readInt(); + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel out, int flags) { + out.writeParcelable(mViewPagerSavedState, flags); + out.writeInt(mLayoutDirection); + } + + // The `CREATOR` field is used to create the parcelable from a parcel, even though it is never referenced directly. + public static final Parcelable.ClassLoaderCreator CREATOR + = new Parcelable.ClassLoaderCreator() { + + @Override + public SavedState createFromParcel(Parcel source) { + return createFromParcel(source, null); + } + + @Override + public SavedState createFromParcel(Parcel source, ClassLoader loader) { + return new SavedState(source, loader); + } + + @Override + public SavedState[] newArray(int size) { + return new SavedState[size]; + } + }; + + } + @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) public BubbleToggleView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); @@ -170,7 +262,9 @@ private void init(Context context, @Nullable AttributeSet attrs) { bubbleToggleItem.setBadgeTextSize(badgeTextSize); //set the gravity - setGravity(Gravity.CENTER); + if (isRtl()) + setGravity(Gravity.START); + else setGravity(Gravity.CENTER); //set the internal padding setPadding( @@ -448,4 +542,4 @@ public void setBadgeText(String value) { updateBadge(getContext()); } -} +} \ No newline at end of file