Change the language in APK compatible with 2.X versions


I need to change the language of my app, that is, I have an activity where I have a list of languages to choose. When I select one I change it, but I'm using some methods that can only be used with android KItKat 4.2 and on and I want it to be used from 2.3. Here is the code:

public void cargarLocale() {
    String PrefIdioma = "idioma";
    String idioma = leePreferencias (PrefIdioma, this);
public void cambiarLocale(String locale) {
    //Si está vacío, queda el idioma por defecto del dispositivo
    if (Objects.equals(locale, ""))
    //Configuramos el idioma cargado desde SharedPreferences
    Locale miLocale = new Locale(locale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = miLocale;

and the method that the language selects me is the following:

findViewById( View.OnClickListener() {
        public void onClick(View v) {
            String nuevoLocale = "";
            nuevoLocale = "en";
            Toast.makeText(Settings.this, "Selected English language !", Toast.LENGTH_LONG).show();

public void salvarLocale(String locale) {        
    String PrefIdioma = "idioma";
    salvaPreferencias(PrefIdioma, locale, this);       
    Intent i = getBaseContext().getPackageManager()                .getLaunchIntentForPackage(getBaseContext().getPackageName());
asked by Pablo Miró 07.06.2016 в 22:18

2 answers


You have a dilemma which many developers have that support previous APIs (and much earlier hehe), in terms of unsupported methods I suppose you refer to Objects.equals ():


Call requires API level 19 (current min is 9):   java.util.Objects # equals less

I think you have it fixed, you only add the annotation to the method that uses Objects.equals() in:

   public void cambiarLocale(String locale) {

The @TargetApi(Build.VERSION_CODES.KITKAT) annotation is used for this method to be compatible with versions prior to Android API 19 (KitKat).

If you continue with problems I suggest you validate in this way:

public void cambiarLocale(String locale) {
    //Si está vacío, queda el idioma por defecto del dispositivo
    //if (Objects.equals(locale, ""))
    if (locale.equals(""))

since locale is a value type String , this way you do not need @TargetApi(Build.VERSION_CODES.KITKAT) .

answered by 07.06.2016 в 22:59

Basically the answer of Elenasys but more puritanical

  • Delete this line @TargetApi(Build.VERSION_CODES.KITKAT)

  • Change if (Objects.equals(locale, "")) by if ("".equals(locale))

  • answered by 09.06.2016 в 18:46