Change the language in APK compatible with 2.X versions

5

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);
    cambiarLocale(idioma);
}
@TargetApi(Build.VERSION_CODES.KITKAT)
public void cambiarLocale(String locale) {
    //Si está vacío, queda el idioma por defecto del dispositivo
    if (Objects.equals(locale, ""))
        return;
    //Configuramos el idioma cargado desde SharedPreferences
    Locale miLocale = new Locale(locale);
    Locale.setDefault(miLocale);
    android.content.res.Configuration config = new android.content.res.Configuration();
    config.locale = miLocale;
    getBaseContext().getResources().updateConfiguration(config,
            getBaseContext().getResources().getDisplayMetrics());
}

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

findViewById(R.id.ingles).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String nuevoLocale = "";
            nuevoLocale = "en";
            Toast.makeText(Settings.this, "Selected English language !", Toast.LENGTH_LONG).show();
            salvarLocale(nuevoLocale);
        }
    });

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

2 answers

2

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:

   @TargetApi(Build.VERSION_CODES.KITKAT)
   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(""))
       return;
...
...

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
0

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