how to change language at run time with out recreating activity

1.2k Views Asked by At

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()
}
1

There are 1 best solutions below

1
Scott Johnson On

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.