android determine if device is in right to left language/layout
You could create a values-ldrtl folder with a xml file called isrighttoleft.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_right_to_left">true</bool>
</resources>
and in your values folder the same file with:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_right_to_left">false</bool>
</resources>
And finally in Code:
boolean isRightToLeft = getResources().getBoolean(R.bool.is_right_to_left);
The values-ldrtl will only be used on a device where the specific settings (e. g. Language) are right-to-left-read languages.
Android: Detect softkeyboard RTL Vs LTR (language direction)
To detect the language of the keyboard at the moment the EditText gains focus, you can use the following snippet
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText editText = (EditText) findViewById(R.id.et);
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
String language = getKeyboardLanguage();
Log.d("language is", language);
}
}
});
}
private String getKeyboardLanguage() {
InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
InputMethodSubtype inputMethodSubtype = inputMethodManager.getCurrentInputMethodSubtype();
return inputMethodSubtype.getLocale();
}
}
At first, my keyboard language was Dutch, and it printed
D/language is: nl
then, I changed my keyboard language to English/United Stated and it printed
D/language is: en_US
To detect whether the language is RTL or LTR, you could use this
private boolean isRTL(Locale locale) {
final int directionality = Character.getDirectionality(locale.getDisplayName().charAt(0));
return directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT ||
directionality == Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
}
This isRTL() method I found at https://stackoverflow.com/a/23203698/1843331
Call that with a Locale created from the String language
:
String language = getKeyboardLanguage();
boolean isRTL = isRTL(new Locale(language));
How can I change to RTL by selecting the language
You can read the developer documentation as this is a very broad question.
Most important step is to mention this in your application
tag in the Android Manifest. android:supportsRtl="true"
. This works with Android 17 and above. For older versions, you need to programatically handle this.
Please check this link for the RTL Logic and changes you need to do for the app to function in the RTL mode.
https://developer.android.com/training/basics/supporting-devices/languages#MirroringAppLogic
On Android 4.4 (API level 19) and higher, you can use
android:autoMirrored="true" when defining your drawable, which allows
the system to handle RTL layout mirroring for you.
You can also set the layout direction like this:
val config: Configuration = context.resources.configuration
view.layoutDirection = config.layoutDirection
Android RTL Text Direction
Adding android:textDirection="locale"
to the TextView
and android:supportsRtl="true"
will do the trick. However, textDirection="locale"
changes the direction in order to locale which is en
or fa
or it is ar
. (Depends on device's language)
And to be clear enough, it's working properly because the text you have in the TextView
is LTR
and you used RTL
direction which as you can see, it made the direction RTL
and there is no problem with that. Also, using android:gravity="start"
will probably force it to use from start which I suppose, it will start from LTR
. (I'd suggest removing android:gravity="start"
and let the Android(In order to device's language) decide which one is the best)
So, to test if it is working or not, you can set the text to arabic or persian language as follows:
android:text="این یک تست است"
If it started from RTL
and with "این"
, so it works as expected.
How to find out locale-dependent text orientation in java?
After some further research I have found out that the language code 'iw' is just the old form for 'he'. And the expression new Locale("he").getLanguage()
would provide the result "iw"!
Nevertheless the ComponentOrientation-method is obviously not up to date, since it is missing the following languages according to Wikipedia:
yi (yiddish plus old form ji), dv (maldivian), ps (pashto) and ha (hausa)
So I have refined the approach of ComponentOrientation and added these missing languages. The final code looks like this:
private static final Set<String> RTL;
static {
Set<String> lang = new HashSet<String>();
lang.add("ar");
lang.add("dv");
lang.add("fa");
lang.add("ha");
lang.add("he");
lang.add("iw");
lang.add("ji");
lang.add("ps");
lang.add("sd");
lang.add("ug");
lang.add("ur");
lang.add("yi");
RTL = Collections.unmodifiableSet(lang);
}
public static boolean isTextRTL(Locale locale) {
return RTL.contains(locale.getLanguage());
}
Android manifest Enable RTL flag for specific part of layouts
While still requires some work, you can go over your layouts (xmls) and add:
android:layoutDirection="ltr"
or
android:layoutDirection="rtl"
To the root layout of each xml files.
This will force the direction for this specific layout.
Related Topics
Changing Text Color of Menu Item in Navigation Drawer
Android Studio: Application Installation Failed
Android Simplecursoradapter Doesn't Update When Database Changes
How to Remove the Top and Bottom Space on Textview of Android
Using Android.Support.V7.Widget.Cardview in My Project (Eclipse)
Clarify the Firebase Connections
Put a Progressbar on Actionbar
How to Set Google Map Fragment Inside Scroll View
Android Studio Create Project Template for New Projects
Android Xml Rounded Clipped Corners
Error:Program Type Already Present: Android.Support.Design.Widget.Coordinatorlayout$Behavior
Cannot Find Symbol Databindingcomponent on Android Studio 3.2 Canary 16 Kotlin Project
How to Create a Number Picker Dialog
No Network Security Config Specified, Using Platform Default - Android Log