20

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)

Sufian
  • 6,405
  • 16
  • 66
  • 120
rajankz
  • 644
  • 1
  • 5
  • 16

5 Answers5

42

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.

Robert
  • 6,855
  • 4
  • 35
  • 43
  • agree to what Commonsware said, but i accepted your answer because this is my research app for user expereince research, and its not going to playstore(i should have mentioned that in the original question) – rajankz Apr 03 '14 at 14:02
17

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

kpninja12
  • 529
  • 6
  • 11
  • I like your approach, it is useful for apps with system privileges. Is there any similar way to change the IME subtype, like changing from English UK to French? I am trying with `Settings.Secure.putInt(resolver, Settings.Secure.SELECTED_INPUT_METHOD_SUBTYPE, subTypeId)` but I am having trouble obtaining the subTypeId in the first place. From sources I can see that it is equal to internal `mSubtypeId` but this is not exposed as far as I can tell. – mtsahakis May 23 '16 at 11:01
  • This answer really helped me a lot. Thanks :) – Aakanksha Jun 08 '16 at 07:26
  • Your excitement about "WITHOUT USER KNOWLEDGE" makes me feel a bit uncomfortable :) – Alexander Malakhov Sep 24 '20 at 11:07
  • resolver = getContentResolver(), also there's a 's' missing on Settings at the second line – pmiguelpinto90 Apr 09 '21 at 16:44
14

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(...).

Alexander Malakhov
  • 3,383
  • 2
  • 33
  • 58
5

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • 1
    I think you misunderstood the question. Rajankz wanted to know if there is a way to switch between installed keyboards, not how to install a keyboard. – Robert Oct 14 '12 at 19:27
  • Ok. In that case would you mind to elaborate on the security risk? It could certainly be annoying, but I can't see any risks in switching between approved input methods. – Robert Oct 14 '12 at 19:48
  • @Robert: There is no such thing as "approved input methods", at install time. The only "approval" comes at the point in time where the user, in Settings, chooses the input method. Now, is it possible that Google could create an Android API that allows switching between input methods and still displays that dialog, in a way that malware authors cannot mess with? Yes, though they haven't, and I doubt that they will. For this sort of stuff (ditto device admin apps), they want users to go through Settings for a single point of control over sensitive items. – CommonsWare Oct 14 '12 at 19:57
  • 2
    With "approved input methods" I'm referring to the fact that the user when installing a third-party input method will get a dialogue with a warning. After acknowledging the warning, the third-party keyboard will show up in the list of input methods. The installation is where the security risk is, not selecting an input method. Anyway, there is actually an API that allows an application to show the list of input methods to the user. See my answer to rajankz's question. – Robert Oct 15 '12 at 07:37
  • 2
    @Robert: "I'm referring to the fact that the user when installing a third-party input method will get a dialogue with a warning" -- there is no such warning at install time, at least not nowadays. – CommonsWare Oct 15 '12 at 10:19
2
import android.content.Intent;

import android.view.inputmethod.InputMethodManager;

// To enable keyboard

startActivity(new Intent("android.settings.INPUT_METHOD_SETTINGS"));

// To activate the keyboard

InputMethodManager imeManager = (InputMethodManager) 
getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
imeManager.showInputMethodPicker();
danday74
  • 52,471
  • 49
  • 232
  • 283
Darkman
  • 21
  • 1