Changing Locale but keep left-to-right and other phone orientations
Asked Answered
L

4

7

I have an app that is available in two languages - English and Hebrew.

I added Hebrew strings using the Translation Editor and I am changing the Locale according to the user selection.

When changing the Locale, it sets the strings to Hebrew like I wanted, but its also changes the toolbar orientation to right-to-left for Hebrew and brings the title and back-button to the right.

English Locale (Default):

enter image description here

Hebrew Locale: enter image description here

Is there a way to keep the toolbar orientation like the English one? I want to keep the back button and the title in the left of the toolbar.

Edit: after adding either android:layoutDirection="ltr" or android:supportsRtl="false" to the toolbar xml. arrow is backwards. how ti fix it?

enter image description here

Lindahl answered 9/5, 2016 at 16:17 Comment(1)
Read second comment to my answer.Orle
B
12

Change Locale but keep left-to-right and other phone orientations

Specifically, add android:supportsRtl="false" to the <application> element in your manifest file.

For more information Link

Bowlin answered 9/5, 2016 at 16:23 Comment(3)
thanks for your answer. it moved the toolbar to the left, but now the arrow is backwards. see edited questionLindahl
this approach will remove rtl support from the entire application and should not be used.Orle
addendum to previous comment - if assigned to element in manifest( like activity) it will remove rtl support from all activity layouts and fragments, including body layouts, which in hebrew and arabic is not desired and looks very bad. Cancelling mirroring for appbar is one thing, For entire activity is not so good.Orle
O
6

Add android:layoutDirection="ltr" to your appbar layout. That will force ltr in any layout direction

Orle answered 9/5, 2016 at 16:20 Comment(6)
thanks for your answer. it moved the toolbar to the left, but now the arrow is backwards. see edited questionLindahl
Add ltr for the entire layout. That should and will work. I have tried.Orle
Or setup custom drawable vector (possible now with support library) and set rtl mirroring to false for the back button instead of using set as home up enabled and parent activity. Assign custom click action using onOptionsItemSelected.Orle
tried to set android:layoutDirection="ltr" for the AppBarLayout, still the same..Lindahl
Ofek, create custom drawable for back button. Use it in your toolbar instead of using set up home enabled and parent activity. Then your button will not switch directionality. You can nowadays create drawable vector because you're using support library. You need to use srcCompat on image view. The programmatically assign onClick for this ImageView to take you back to your activity. Android is inverting elements in rtl locales automatically., So you have to use your own drawable instead of native functionality.Orle
The problem is the native icon android is using in support library is a vector which has supportRtlMirroring set to true. That's why it will always mirror in hebrew and arabic. The icon you create will not have that attr (hopefully, fingers crosses... lol) and therefore will not mirrorOrle
R
3

I had this problem too. I had a method to change the locale of the language and the application configuration :

private String loadPreference() {
    SharedPreferences shp = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
    String language = shp.getString("Language","fa");
    Locale myLocale = new Locale(language);
    Locale.setDefault(myLocale);
    Configuration config = new Configuration();
    config.locale = myLocale;
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    String locale = getResources().getConfiguration().locale.getDisplayName();
    System.out.println(locale);
    return locale;
}

It is changing locale and simultaneously changing the layoutDirection, so you can solve this by setting the direction manually:

private String loadPreference() {
    SharedPreferences shp = getSharedPreferences("CommonPrefs", Activity.MODE_PRIVATE);
    String language = shp.getString("Language","fa");
    Locale myLocale = new Locale(language);

    Configuration config = new Configuration();
    config.setLocale(myLocale);
    //manually set layout direction to a LTR location
    config.setLayoutDirection(new Locale("en"));
    getResources().updateConfiguration(config, getResources().getDisplayMetrics());
    String locale = getResources().getConfiguration().locale.getDisplayName();

    return locale;
}
Rorke answered 1/8, 2016 at 11:55 Comment(0)
W
0

Use constraint Layout for handle this kind of issue, if you want to not change position of back button use app:layout_constraintLeft_toLeftOf="parent" OR app:layout_constraintRight_toRightOf="parent"

but if you want to change position of Button or any other element after changing Local you can use app:layout_constraintStart_toStartOf="parent" OR app:layout_constraintEnd_toEndOf="parent"

Wirephoto answered 10/10, 2023 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.