In my application, I have implemented some languages including Arabic language which starts from Right to Left. I want to align all icons in the Toolbar be RIGHT_TO_LEFT
.
I tried layout_gravity="right"
and gravity="right"
But nothing happed.
My Toolbar code:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primary_color"
app:popupTheme="@style/Theme.AppCompat.Light"/>
Here is what I want:
Put the android:supportsRtl="true" attribute in your AndroidManifest file. After that add below line to your toolbar tag:
android:layoutDirection="rtl"
Official right-to-left support was introduced with Android 4.2 (API level 17), so this will only work if your minimum SDK level is 17:
1) Put the android:supportsRtl="true"
attribute in your AndroidManifest file to support right-to-Left layout directions.
2) Secondly put this code in your activity's onCreate() methdod:
if (getWindow().getDecorView().getLayoutDirection() == View.LAYOUT_DIRECTION_LTR){
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
}
This will make your everything is placed from right to left if the system uses an according language. Also make sure you use start/end instead of left/right when setting up padding to make sure that all elements are aligned correctly.
For more information about the official support announcement: http://android-developers.blogspot.be/2013/03/native-rtl-support-in-android-42.html
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With