1
private fun setLocale(lang: String) {
        val language = Locale(lang)
        val res: Resources = resources
        val dm: DisplayMetrics = res.displayMetrics
        val conf: Configuration = res.configuration
        conf.locale = language
        res.updateConfiguration(conf, dm)
        val refresh = Intent(this, LoginActivity::class.java)
        startActivity(refresh)
        finish()
    }

Hello Everybody!

I'm creating an android app and this is for Arabic people. And I have created the string resources for "en" and "ar" xml and declared it in Mainfest.xml android:supportsRtl="true" .

When I use this code to convert app language, it converts the texts ONLY. Without converting the layout direction, text direction and text alignment.

  1. This code contains .locale and .updateConfiguration as java code. Can you please to help me how to cleanup whole code to be suitable for kotlin code?
  2. Can you please to help me solve RtL problem to make the app more professional and accuracy app?

Here's attachments to prove the problem. shows what happens when implement the code. Against to when I change language device

Zain
  • 37,492
  • 7
  • 60
  • 84
MohamadMF
  • 35
  • 6

2 Answers2

1

To make the layout change direction follows to the language add:

In the above snippet make sure to replace if(rtl) with RTL condition for instance if (language == "ar")

You can call this code in the activity's onCreate(), but also test it inside your setLocale() method

Zain
  • 37,492
  • 7
  • 60
  • 84
  • Firstly. Thanks so much for your help ☺☺. Secondly your code worked very well but after so small amendment. And I will add this as a answer to publish it. – MohamadMF Oct 12 '21 at 10:50
1

@Zain has replied to me and I thank him so much. But I developed the answer to be totally working.

@SuppressLint("ObsoleteSdkInt")
    private fun setLocale(lang: String) {
        val language = Locale(lang)
        val res: Resources = resources
        val dm: DisplayMetrics = res.displayMetrics
        val conf: Configuration = res.configuration
        conf.locale = language
        res.updateConfiguration(conf, dm)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
            res.configuration.setLayoutDirection(language)
            (if (lang == "ar") {
                View.LAYOUT_DIRECTION_RTL
            } else {
                View.LAYOUT_DIRECTION_LTR
            }).also { window.decorView.layoutDirection = it }
        }
        val refresh = Intent(this, LoginActivity::class.java)
        startActivity(refresh)
        finish()
    }

so I changed:

  1. in if expression I used lang constructor and variable language . Because the variable won't declare in if as a logic test, expect after declare it in val as String. So, the correct to use the constructor.
  2. I added braces for all if expression.
  3. I converted assignment to assignment expression.
  4. I added suppress annotation @SuppressLint("ObsoleteSdkInt").

And to be honest, I implemented no. 2, 3, and 4 from Show Context Action AndroidStudio

MohamadMF
  • 35
  • 6