Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch keyboard profile programmatically

Is there any way that we can switch installed keyboards programmatically (without going to the settings section manually)?

My requirement is that the user is presented with all the keyboards which are installed on the phone and gets a chooser dialog to switch to the one wishes?

(basically we want to cut down the step to transfer him to the settings page)

like image 467
rajankz Avatar asked Jun 14 '12 15:06

rajankz


4 Answers

If your app has system privileges, and has the permission

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

you can programatically enable the keyboard and set it as the current keyboard by making it the default keyboard WITHOUT USER KNOWLEDGE OR INTERVENTION!

//get the old default keyboard in case you want to use it later, or keep it enabled
String oldDefaultKeyboard = Settings.Secure.getString(resolver, Setting.Secure.DEFAULT_INPUT_METHOD);

//enable your keyboard
Settings.Secure.putString(resolver, Settings.Secure.ENABLED_INPUT_METHODS, "com.my.keyboard/.full.path");

//set your keyboard as the new default keyboard
Settings.Secure.putString(resolver, Settings.Secure.DEFAULT_INPUT_METHOD, "com.my.keyboard/.full.path");

You can enable multiple keyboards (such as the default keyboard and your own) by providing a list of keyboards to the ENABLED_INPUT_METHODS, separated by ':'. See docs

You can verify your keyboard's full package and path ID by invoking ime list -a through adb shell

like image 34
kpninja12 Avatar answered Nov 23 '22 22:11

kpninja12


This piece of code will fulfill your requirements:

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

As Commonsware points out in his answer, there is no way to do this behind the user's back.

like image 138
Robert Avatar answered Nov 24 '22 00:11

Robert


If you have rooted device, you can use /system/bin/ime utility.

List all installed input methods: # ime list -a

Set google's keyboard as default:
# ime set com.google.android.inputmethod.latin/com.android.inputmethod.latin.LatinIME

On Java side use Runtime.getRuntime().exec(...).

like image 22
Alexander Malakhov Avatar answered Nov 24 '22 00:11

Alexander Malakhov


Is there any way that we can switch installed keyboards programmatically (without going to the settings section)?

Fortunately, no, for security reasons. If an app could dictate what input method editor is used, malware would change the input method editor to their keylogger.

like image 30
CommonsWare Avatar answered Nov 24 '22 00:11

CommonsWare