blogger blogger - 1 month ago 40
Android Question

Change Keyboard Input Language Programmatically

I am developing an application in which i need to allow the user to change the input keys shown in the default keyboard, upon request or by default, for example, i may prompt the user at the beginning to select the default language and after that, whenever the default keyboard is used, the app always displays the keys of the keyboard the selected language,

I know this is possible, because in default keyboard app, when multiple input methods are selected, then long pressing the spacebar allows to change the input methods at runtime, if this is possible then my requirement is also possible...

I dont want to prompt for default keyboard like following:

InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();


I dont want to change the locale and restart my activity all the time like:

Resources res = getBaseContext().getResources();
// Change locale settings in the app.
DisplayMetrics dm = res.getDisplayMetrics();
android.content.res.Configuration conf = res.getConfiguration();
conf.locale = new Locale("ru".toLowerCase());
res.updateConfiguration(conf, dm);
Log.i("inside onStart","after ever");
setContentView(R.layout.activity_main);


I just want to show the keyboard input in my desired language.

Answer

After doing some research here and there found the answer, first of all you have to create a custom keyboard View which extends keyboardView and in it create static key value variable like

static final int KEYCODE_LANGUAGE_SWITCH_ENG = -102;
static final int KEYCODE_LANGUAGE_SWITCH_URDU = -103;

after that in your IME class where you have implemented the inputMethodService, create the keyboards inside the onInitializeInterface override function. like

mSymbolsKeyboard = new Keyboard(this, R.xml.qwerty2);
mEngQwertyKeyboard = new Keyboard(this, R.xml.eng_qwerty);

after this add these final static keys in the onKey override function as switch cases, and in the cases set the keyboards accordingly:

setKeyboard(mEngQwertyKeyboard);
Comments