Hans Fouche Hans Fouche - 1 month ago 58
Android Question

How to change the language with a button in Android Studio

Please assist,

I have created two addition language resource and would like to give the game the option to change the language option with a button selection.

I created a class called LocaleHelper which I found via google as per below however the locale has since been depricated. How to go about now?

package biz.myecard.mathisfun;

/**
* Created by Hans Fouche on 2016/10/18.
*/

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;

import java.util.Locale;

public class LocaleHelper {

private static final String SELECTED_LANGUAGE = "Locale.Helper.Selected.Language";

public static void onCreate(Context context) {
String lang = getPersistedData(context, Locale.getDefault().getLanguage());
setLocale(context, lang);
}

public static void onCreate(Context context, String defaultLanguage) {
String lang = getPersistedData(context, defaultLanguage);
setLocale(context, lang);
}

public static String getLanguage(Context context) {
return getPersistedData(context, Locale.getDefault().getLanguage());
}

public static void setLocale(Context context, String language) {
persist(context, language);
updateResources(context, language);
}

private static String getPersistedData(Context context, String defaultLanguage) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
return preferences.getString(SELECTED_LANGUAGE, defaultLanguage);
}

private static void persist(Context context, String language) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = preferences.edit();

editor.putString(SELECTED_LANGUAGE, language);
editor.apply();
}

private static void updateResources(Context context, String language) {
Locale locale = new Locale(language);
Locale.setDefault(locale);

Resources resources = context.getResources();

Configuration configuration = resources.getConfiguration();
configuration.locale = locale;

resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}
}


It is the called in the button method with:

LocaleHelper.onCreate(this, "en");


Error as below: My APP works but don't thinks this is optimal.

Note: /Users/macintoshhd/Desktop/AndroidCourse/MathisFun/app/src/main/java/biz/myecard/mathisfun/LocaleHelper.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

Answer

You should read java doc first:

This field was deprecated in API level 24. Do not set or read this directly. Use getLocales() and setLocales(LocaleList). If only the primary locale is needed, getLocales().get(0) is now the preferred accessor.

So you are able to do so:

private static void updateResources(Context context, String language) {
    Locale locale = new Locale(language);
    Locale.setDefault(locale);

    Resources resources = context.getResources();

    Configuration configuration = resources.getConfiguration();

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        configuration.setLocale(locale);
    } else {
        configuration.locale = locale;
    }

    resources.updateConfiguration(configuration, resources.getDisplayMetrics());
}

Deprecation means that method isn't currently supported and could be removed in next versions of api. But in the previous ones you can't avoid using it, because there weren't new methods (setters & getter, which have been provided since 17&21 api)