Skip to content Skip to sidebar Skip to footer

Change Language Button

I am trying to create a button that changes the app language. I created the string file for the language and I tried this code for changing the default language but with no success

Solution 1:

There are several options, and libraries that are helpful, please read this thread: Change app language programmatically in Android

I've used this library in the past (Android 9) and worked perfectly: https://github.com/zeugma-solutions/locale-helper-android

Solution 2:

So I found this code that solved my problem in seconds:

I created a new Java file name "MyContextWrapper" and inside I paste the following code:

/**
 * Created by hosam azzam on 07/01/2017.
 * CopyRights to Bassel Mourjan
 */import android.annotation.TargetApi;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.Configuration;
import android.os.Build;

import java.util.Locale;

publicclassMyContextWrapperextendsContextWrapper {

    publicMyContextWrapper(Context base) {
        super(base);
    }

    @SuppressWarnings("deprecation")publicstatic ContextWrapper wrap(Context context, String language) {
        Configurationconfig= context.getResources().getConfiguration();
        LocalesysLocale=null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            sysLocale = getSystemLocale(config);
        } else {
            sysLocale = getSystemLocaleLegacy(config);
        }
        if (!language.equals("") && !sysLocale.getLanguage().equals(language)) {
            Localelocale=newLocale(language);
            Locale.setDefault(locale);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                setSystemLocale(config, locale);
            } else {
                setSystemLocaleLegacy(config, locale);
            }
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                context = context.createConfigurationContext(config);
            } else {
                context.getResources().updateConfiguration(config, context.getResources().getDisplayMetrics());
            }
        }
        returnnewMyContextWrapper(context);
    }

    @SuppressWarnings("deprecation")publicstatic Locale getSystemLocaleLegacy(Configuration config){
        return config.locale;
    }

    @TargetApi(Build.VERSION_CODES.N)publicstatic Locale getSystemLocale(Configuration config){
        return config.getLocales().get(0);
    }

    @SuppressWarnings("deprecation")publicstaticvoidsetSystemLocaleLegacy(Configuration config, Locale locale){
        config.locale = locale;
    }

    @TargetApi(Build.VERSION_CODES.N)publicstaticvoidsetSystemLocale(Configuration config, Locale locale){
        config.setLocale(locale);
    }
}

and in activity:

privateStringLANG_CURRENT="en";



findViewById(R.id.change).setOnClickListener(newView.OnClickListener() {
            @OverridepublicvoidonClick(View v) {
                if (LANG_CURRENT.equals("en")) {
                    changeLang(MainActivity.this, "iw");
                } else {
                    changeLang(MainActivity.this, "en");
                }
                finish();
                startActivity(newIntent(MainActivity.this, MainActivity.class));
            }
        });



publicvoidchangeLang(Context context, String lang) {
        SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(context);
        SharedPreferences.Editoreditor= preferences.edit();
        editor.putString("Language", lang);
        editor.apply();
    }

    @OverrideprotectedvoidattachBaseContext(Context newBase) {

        SharedPreferencespreferences= PreferenceManager.getDefaultSharedPreferences(newBase);
        LANG_CURRENT = preferences.getString("Language", "en");

        super.attachBaseContext(MyContextWrapper.wrap(newBase, LANG_CURRENT));
    }

Its change the language and save to shareprefrences.

Source - Here

Post a Comment for "Change Language Button"