1

I used the below code for changing language from default to selected. But i am not able to update the views in my fragment/activity like TextView with selected language. Programmatically showing selected language.

Also My Application is potrait onConfigurationChanged() is not calling even i declare android:configChanges="layoutDirection|locale" in Manifest file

Note: I don't want to recreate() my activity.

1.Used BaseActivity for attachBaseContext to set the locale language and extends this Activity for all activities

open class  BaseAppCompactActivity() : AppCompatActivity() {

    override fun attachBaseContext(newBase: Context) {
        super.attachBaseContext(LocaleHelper.onAttach(newBase))

    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

    }
}

2.Used Application attachBaseContext and onConfigurationChanged to set the locale language.

public class MyApplication extends Application {

   private static MyApplication application;

    @Override
    public void onCreate() {
        super.onCreate();
    }

    public static MyApplication getApplication() {
        return application;
    }

    /**
     * overide to change local sothat language can be chnaged from android device  nogaut and above
     */
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(LocaleHelper.INSTANCE.onAttach(base));
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
    /*** also handle chnage  language if  device language chnaged **/

        super.onConfigurationChanged(newConfig);

    }

3.Used Locale Helper for handling language changes ,this approach work on all device

object LocaleHelper {

fun onAttach(context: Context, defaultLanguage: String): Context {
    return setLocale(context, defaultLanguage)
}

fun setLocale(context: Context, language: String): Context {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        updateResources(context, language)
    } else updateResourcesLegacy(context, language)

}


@TargetApi(Build.VERSION_CODES.N)
private fun updateResources(context: Context, language: String): Context {
    val locale = Locale(language)
    Locale.setDefault(locale)

    val configuration = context.getResources().getConfiguration()
    configuration.setLocale(locale)
    configuration.setLayoutDirection(locale)

    return context.createConfigurationContext(configuration)
}




    private fun updateResourcesLegacy(context: Context, language: String): Context {
    val locale = Locale(language)
    Locale.setDefault(locale)

    val resources = context.getResources()
}
Mano
  • 11
  • 3
  • look at this resource : https://proandroiddev.com/change-language-programmatically-at-runtime-on-android-5e6bc15c758. long story short this blog contains all your doubts and inshort go for 3rd party library or take the part of the code from the library(it is mention in that post) that handles the API level from 25+ changes which android ecosystem have done a lot. – Zaraki596 Jul 02 '21 at 16:02
  • Hey @Zaraki596 i tried using getResources() for text but it will not update the views until recreate activity. My case i don't wana recreate it. – Mano Jul 06 '21 at 07:24
  • Unfortunately, there is no way to update the strings without recreating the activity or restarting the app. They need to read the string in the new context with the new locale which was changed after the app/activity was started/created. – msc87 Sep 06 '22 at 13:39

1 Answers1

0

Have a look at: https://developer.android.com/guide/topics/resources/localization

It will allow you to create String resource files that will be shown based on the devices default language.

Scott Johnson
  • 707
  • 5
  • 13
  • Hi @TDIScott i Used Values-es and i am having required strings . But the resources are not updating when i changed if i debug i am able to see locale returns with selected language. – Mano Jul 02 '21 at 15:19