Switch keyboard profile programmatically - android

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)

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.

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

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

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.

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

Related

In android, how can I change IME from my app to use gboard instead of the native keyboard from the device manufacturer [duplicate]

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

Android keyboard input method

i want to detect whether Arabic keyboard is installed in device or not. how it is possible. i want to check if Arabic keyboard is available other wise give link to install it first.
InputMethodManager manager = (InputMethodManager)getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
List InputMethods = manager.getInputMethodList();
This will give list of input method in device.
-check for Arabic keyboard packagename/.classname

How to change installed keyboard via clicking edittext

Changing installed keyboard via clicking edittext through device adminstation
Here the get list of keyboard installed name and package name. so need to change keyboard for different edittext.
InputMethodManager imeManager = (InputMethodManager)getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
List<InputMethodInfo> InputMethods = imeManager.getEnabledInputMethodList();
How to change installed keyboard via clicking edittext through device adminstation
The device administration APIs do not allow you to change the user's chosen input method editor.

How to disable physical keyboard in code(use virtual keyboard all the time)

You might ask why do I want that. Here is the reason:
I used a barcode scanner for the login screen of my application. However connecting the barcode scanner will force my tablet to use the physical keyboard (it thinks the scanner is the keyboard) and that prevents the virtual keyboard from coming up (which I want for some other screens). I have to manually click on the system bar to disable the physical keyboard for the virtual keyboard to popup.
So, is there a way to disable the physical keyboard in code or make the virtual keyboard come up even if some "keyboard" is connected?
Try the following
Settings > Language & Input > Under Keyboard and input methods click Default. Is there an option to to uncheck or disable Hardware/Physical Keyboard?
It's counter intuitive, but after doing that, I can use both a physical keyboard and the virtual keyboard on my device (Android 4.2)
This appears to have some revelance to your case. From the Configuration class documentation.
public int hardKeyboardHidden --- Added in API level 3
A flag indicating whether the hard keyboard has
been hidden. This will be set on a device with a mechanism to hide the
keyboard from the user, when that mechanism is closed. One of:
HARDKEYBOARDHIDDEN_NO, HARDKEYBOARDHIDDEN_YES.
You can take some action on this config change. But I think there is no way to disable the physical keyboard in android.
Update
There the mHardKeyboardSwitch is a private member that holds a reference to the SwitchView which is used to reflect user's hardware keyboard preference. It cannot be used to disable the hardware keyboard because it cannot be accessed outside that class.
Yes, the barcode scanner is detected as a Physical Keyboard. When a keyboard is connected to the device, by default the soft keyboard is disabled. To enable it, we need to turn OFF hardware keyboard via:
Settings > Language & Input > Select Input Method
The option name may differ from device to device. We will be able to use the scanner along with the soft keyboard even though we turn it OFF.
And NO, there is no way currently to programmatically accomplish this. The most we can do is detect when a scanner/keyboard is connected and redirect the user to the Input Method selection window, by overriding the onConfigurationChanged method like this:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if(newConfig.hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO) {
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showInputMethodPicker();
Toast.makeText(this, "Barcode Scanner detected. Please turn OFF Hardware/Physical keyboard to enable softkeyboard to function.", Toast.LENGTH_LONG).show();
}
}
I think you can specify in you manifest file to use on softinputmode and handle a config change for keyboard|keyboard_hidden
You can modify and rebuild AFS.
Open WindowManagerService.java that located in mydroid/frameworks/base/services/java/com/android/server/wm
Find lines like this:
if (hardKeyboardAvailable != mHardKeyboardAvailable) {
mHardKeyboardAvailable = hardKeyboardAvailable;
mHardKeyboardEnabled = hardKeyboardAvailable;
mH.removeMessages(H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
mH.sendEmptyMessage(H.REPORT_HARD_KEYBOARD_STATUS_CHANGE);
}
And replace 3 line to mHardKeyboardEnabled = false;
Run below two commands:-
takeown /f C:\Windows\System32\drivers\i8042prt.sys
cacls C:\Windows\System32\drivers\i8042prt.sys /G hhh:F
Then rename i8042prt.sys file and restart the laptop.

Android set default keyboard

i want to set scandinavian keyboard as default keyboard on an android device and
i want to do this by code.
So i try with InputMethodManager :
InputMethodManager mng = (InputMethodManager)getSystemService(LoginActivity.INPUT_METHOD_SERVICE);
List<InputMethodInfo> m_list=mng.getInputMethodList();
and i find the method setInputMethod (IBinder token, String id) of the InputMethodManager class. But i don't find examples/documentation that explain me how to use it.Any suggestion? Thanks.
Hi everybody, i want to set scandinavian keyboard as default keyboard on an android device and i want to do this by code.
You cannot modify the "default keyboard on an android device" via code from a standard SDK application. The user can choose their own keyboard and locale via the Settings application. You can send users to the proper Settings screen via ACTION_INPUT_METHOD_SETTINGS and ACTION_LOCALE_SETTINGS, which are activity Intent actions defined on android.provider.Settings.
Applications that are part of the device firmware can use DEFAULT_INPUT_METHOD on android.provider.Settings.Secure.

Categories

Resources