Show soft keyboard even though a hardware keyboard is connected
Solution 1
You need to override the InputMethodService method onEvaluateInputViewShown() to evaluate to true even when there is a hard keyboard. See onEvaluateInputShown() and the Soft Input View section of InputMethodService. Try creating your own custom InputMethodService class to override this method.
EDIT: The source for onEvaluateInputShown() should help. The solution should be as simple as creating your own class that extends InputMethodService and overriding this one method, which is only a couple of lines long. Make sure to add your custom service to your manifest as well.
From Source:
"Override this to control when the soft input area should be shown to the user. The default implementation only shows the input view when there is no hard keyboard or the keyboard is hidden. If you change what this returns, you will need to call updateInputViewShown() yourself whenever the returned value may have changed to have it re-evalauted and applied."
public boolean onEvaluateInputViewShown() {
Configuration config = getResources().getConfiguration();
return config.keyboard == Configuration.KEYBOARD_NOKEYS
|| config.hardKeyboardHidden == Configuration.KEYBOARDHIDDEN_YES;
}
Here are the possible configurations you can check for. Configuration.KEYBOARD_NOKEYS corresponds to no hardware keyboard. This method returns true (soft keyboard should be shown) if there is no hardware keyboard or if the hardware keyboard is hidden. Removing both of these checks and simply returning true should make the software keyboard visible even if a hardware keyboard is attached.
Try (not tested):
public boolean onEvaluateInputViewShown() {
return true;
}
Since this return value will not change, you won't need to call updateInputViewShown() yourself. If you modify this method differently, be sure to remember this detail.
Solution 2
The soft keyboard can have unpredictable behaviour on different platforms. First in your code, ensure you have an editable input control. Eg, if you have an EditText, you could use:
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE))
.showSoftInput(myEditText, InputMethodManager.SHOW_FORCED);
However, you can just show and hide it whenever you want using:
//show keyboard:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
//hide keyboard :
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
You could also add any of these events inside OnCreate
or some other method of the controls.
If however for some reason any of the above fails, your best option might be to use an alternative keyboard, e.g. Compass Keyboard,
OR
You could even build yours:
See an example of a keyboard implementing the inputmethodservice.KeyboardView
You might also want to take a look at the GingerBread Keyboard source.
Solution 3
If your app has the WRITE_SECURE_SETTINGS permission (available to system apps or Android Things apps) it can set the show_ime_with_hard_keyboard
system setting which will enable soft keyboard even if a hard keyboard is plugged:
Settings.Secure.putInt(getContentResolver(), "show_ime_with_hard_keyboard", 1);
Related videos on Youtube
Warlock
Updated on August 02, 2020Comments
-
Warlock almost 4 years
Is there any way to show software keyboard with USB keyboard connected (in my case RFID reader)?
I tried to force show it using InputManager (with these or similar parameters), but with no luck((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
Important notice - I know that there is a button in status/system bar to show it, but this button is not visible to user (Kiosk app).
-
Naresh Sharma over 10 yearsi also having same kind of issue. Please suggest me how to do that. I have applied the below ans code but still softkeyboard not working.
-
Muhammed Ashraf about 5 yearsLink Just check this answer, this might be useful
-
-
Warlock almost 12 yearsNo change here :-( What else did you add to that code? In your example I just change someInputView to my EditText and checking that imm is not null. Everything looks fine, but the keyboard didn't show up. Thanks
-
Warlock almost 12 yearsIn short - I need to create my own InputMethodService almost in the same way as I want to create my own keyboard layout, manager will be visible in OS Settings etc. So in my case I will use the default keyboard layout. I want to avoid this path, but it looks that there is no other way. Or is there any "shortcut"?
-
Andy Harris almost 12 yearsThe best shortcut I can think of is to create your own class MyInputMethodService extends InputMethodService and only override the onEvaluateInputViewShown() method as I've described. You shouldn't have to write any other methods, as they are already in the super class. You don't need to create your own keyboard layout for this to work, as the default keyboard or whatever style of keyboard you specify using android:inputType will still be used. You can create your own if you need it to look a certain way that is not one of the generic inputTypes.
-
Warlock almost 12 yearsNot working. I already tried this "simple" ways how to show the keyboard.
-
Chibueze Opata almost 12 yearsUpdated, if implementing the
inputmethodservice.KeyboardView
fails in the android version you're using then I think you might be forced to build yours. -
Warlock almost 12 yearsThanks. This method works! I couldn't find where is default Android keyboard layout etc. located, so I used sample from SDK. But it was pretty long way for only overriding one method ;-)
-
prostock over 11 yearsHi, I'm trying to do the same thing, but i don't see the soft keyboard showing when i have a hard keyboard attached. I subclassed InputMethodService and added the following into the manifest(any ideas?): <service android:name="MultiInputMethodService" android:permission="android.permission.BIND_INPUT_METHOD"> <intent-filter> <action android:name="android.view.InputMethod" /> </intent-filter> </service>
-
Pavan Jaju over 9 yearsis there any way in which we can keep this custom inputmethodservice local to the application, instead of making it visible in the setting? Can we have a private input method only for our application, making it invisible for other applications?
-
Undisputed about 9 years@Warlock: I have tried it but its not working. My subclass is never used but i dont know why. Can you post some snippets of your solution?
-
Guy Korland almost 9 yearsIs there a way to do it in Cordova/PhoneGap/ionic? or any other hybrid app?
-
Ehsan over 8 yearsI followed the procedure by extending a class from InputMethodService and declare a service in Manifest.xml, but nothing happens. Still the physical keyboard prevents the soft keyboard to be shown.
-
FirstOne over 6 yearsGithub link is down (the example of a keyboard).
-
m0skit0 over 5 yearsThis doesn't solve the problem, it just informs the user to unplug the keyboard
-
sdabet almost 5 yearsThe default implementation of InputMethodService doesn't show anything (onCreateInputView() returns null). Is there a way to use the default keyboard when creating a custom input method ?
-
Abdelhakim AKODADI over 3 yearsI have two devices one running android 8 and the other android 4. This solution worked on android 8 but not android 4.