Read complete story

How to identify RTL language in your android application

In this blog, we will learn on how to identify RTL language in your android application.

The reason for writing this blog is pretty clear from the title itself and there are many ways to get this done.

But while working on including Farsi language in my application I ran into some problem and I needed to fix this problem, the reason was pretty straight that the orientation was supposed to be RTL, but with all the approaches I knew I was not getting the orientation to be RTL.

When I checked all the approaches, they were in some manner getting the orientation from the locale, which in turn was getting the direction from the Character class, and thus for some reason, the RTL flag for Farsi was not coming as it should be.

Then after pondering upon the issue, I got to know that the directionality returned for Farsi is a bit different than the Default RTL languages.

So, when I looked up in the Character class, I found that there are actually two flags that are returned for RTL languages viz. DIRECTIONALITY_RIGHT_TO_LEFT and DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC. If you will look just below this line then you will see some other flags also with the RIGHT_TO_LEFT indicators in them, but I never got them in return values.

Now it was all clear and all I needed to include this also in my code as RTL identifier.

Let’s see what I followed :

APPROACH :

  1.  Get the current Locale Object of the application.
  2.  Get the display name of this locale object.
  3. Pass this display name’s first character to the Character.getDirectionality() function of the character class.
  4. Check the directionality integer returned with  Character.DIRECTIONALITY_RIGHT_TO_LEFT || Character.DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC;
  5.  If the values matches the language is RTL Language.

CODE :

function is RTL()

using this function in the code

That’s all.

Keep coding and keep sharing 🙂

 

. . .

Comment

Add Your Comment

Be the first to comment.

css.php
Hire Us!
Brief us about your requirements and we'll get back to you.
Woo! Hooy!
We have just recieved your project brief and our expert will contact you shortly.
Send Again
Close