Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 871
Hunain Usman Avatar asked Mar 28 '16 11:03

Hunain Usman


1 Answers

You can change keyboard without user notification only and only if your app is running as a System app for security reasons.

You need to give Android permission first in your app's AndroidManifest.xml

"android.permission.WRITE_SECURE_SETTINGS"

Then you need to determine id of your keyboard.

-> To know id, you need to keep your keyboard default from setting menu manually then put this print somewhere,

System.out.println(Settings.Secure.getString(getContentResolver(),Settings.Secure.DEFAULT_INPUT_METHOD));

Once you print id and you know your keyboard id you can do as per below ( I have changed my default keyboard to Japanese )

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

//imeManager.showInputMethodPicker(); //This is to see available keyboards.
imeManager.setInputMethod(null,"jp.co.omronsoft.openwnn/.OpenWnnJAJP");

Enjoy !!

like image 71
Kalrav J Parsana Avatar answered Oct 29 '22 03:10

Kalrav J Parsana