1

I'm wanted to know if there any way i could set the default language of the virtual keyboard of an EditText in Android?

Every time i get focus on my EditText it opens the virtual keyboard with my native language instead of English.

I guess it something with the device settings, but if there is a way to program that in Android development that would be terrific.

Veve
  • 6,643
  • 5
  • 39
  • 58
God
  • 1,238
  • 2
  • 18
  • 45

1 Answers1

1

LocaleHelper” is the solution all you need. You just have to initialize locale on your application’s main class. After that all your language changes will persist.

In application class make following changes:

    public class MainApplication extends Application {
    @Override
    public void onCreate() {
        super.onCreate();
        LocaleHelper.onCreate(this, "en");
    }
}

LocalHelper.java will be:

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

}

For detail description you refer this link.

Hope it will help you.

User_1191
  • 981
  • 2
  • 8
  • 24
  • 1
    Thank you for the answer, But i don't have any `Application` class. – God Apr 15 '16 at 09:23
  • 1
    Then you need to create a class which will extend `Application`. Follow the instructions from link. – User_1191 Apr 15 '16 at 09:24
  • I will. Thank you very much. – God Apr 15 '16 at 09:25
  • 1
    Is it compulsory to extend `Application` class ? We can pass getApplicationContext() to `LocaleHelper.onCreate(getApplicationContext(), "en");` right ? – Kathi Apr 15 '16 at 11:23
  • 1
    yes, because by extending `Application` it will make `onCreate` method available to the rest of your application components – User_1191 Apr 15 '16 at 11:32
  • This sets the language for the entire app. What if I want to to set ONLY an editext so the remaining app can remain in the device's locale – Collins Feb 25 '19 at 08:13
  • for multiple edittext's with different input languages, this is not a solution! – Sabrina Apr 25 '19 at 19:31
  • 3
    This is not a solution, because it doesn't answer the original question. The question is about changing language for virtual keyboard, and the answer contains info about changing app locale. – xapienz Dec 28 '19 at 06:12
  • Not recommend if you only want change language in keyboard – Dom Hoàn Feb 24 '23 at 06:45