I'm writing an app for Android using LibGDX. When the user enters a text field the soft keyboard shows up, but the shift key is not pressed; moreover, after the user inserts a period, the shift key doesn't toggle automatically. Does anybody know how to turn on the automatic capitalization?
Thanks
Additional info
The LibGDX backend for Android opens the keyboard using the following code:
InputMethodManager manager = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
View view = ((AndroidGraphics)app.getGraphics()).getView();
view.setFocusable(true);
view.setFocusableInTouchMode(true);
manager.showSoftInput(((AndroidGraphics)app.getGraphics()).getView(), 0);
I don't need to do everything through the frontend, I'm able to add custom code into the Android backend.
I am having a problem with the Android keyboard getting changed from number type to the default Alpha when the user clicks again. After reading several posts about not being able to change the default type of the keyboard from alpha to numeric on a webview I have followed the below procedure. I have created a hidden EditText control and changed the keyboard type from that control and it worked fine, now I get the Numeric SIP and all the key presses are dispatched to the webview correctly. But the problem is if the user touches again on the webview the keyboard type is changed from numeric to alpha by the InputMethodService and I don’t receive any callback for this event.
V/InputMethodService( 1764): CALL: onStartInput
I can think of the below possible solutions but none seem to work.
1.Is there a way to change the default keyboard type through InputMethodManager on a webview?
2.If a Number keyboard is already shown then can we prevent the InputMethodManager to change to the default alpha keyboard when the user touches again?
3.Is there a way to override or receive a callback for onStartInput() method of the InputMethodService?
InputMethodManager imm;
if (mWebEditText == null)
{
mWebEditText = new WebEditText(Common.mainActivity.getApplicationContext(),view.getView());
}
mWebEditText.setInputType(InputType.TYPE_CLASS_NUMBER);
webEditTextPanel.addView(mWebEditText, lp);
mWebEditText.setVisibility(View.VISIBLE);
mWebEditText.requestFocus();
imm.showSoftInput(mWebEditText, 0);
WebEditText class:
public class WebEditText extends EditText
{
//Pointer to the web view to dispatch the keys
View mWebView;
public WebEditText(Context context, View view)
{
super(context);
mWebView = view;
}
/**
* Override the dispatch key event to send the key events to the web view
* from the invisible Edit Text control
*/
#Override
public boolean dispatchKeyEvent(KeyEvent event)
{
mWebView.dispatchKeyEvent(event);
return super.dispatchKeyEvent(event);
}
What you can do here is from your HTML code which you are showing in the webview. change the input type of the EditText(textbox) to number.
<input type="number" placeholder="Text box lable" id="Text box id" required value="defautvalue" />
Edit :
EditText e = new EditText(getApplicationContext());
e.setInputType(InputType.TYPE_CLASS_NUMBER);
appView.addView(e);
can you add the edittext in the way i have done. it works fine for me in my applicaiton
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).
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.
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.
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);
This worked in my app, interestingly, also an kiosk app.
This is a bit stripped, I did some checks beforehand, whether IMM is null and such.
((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).toggleSoftInputFromWindow(someInputView.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
according to this https://stackoverflow.com/a/24287780/2233069, I made working solution for Kiosk mode.
boolean hardwareKeyboardPlugged=false;
....
mEditText.setOnFocusChangeListener(this);//in onCreate()
....
#Override
public void onResume() {
//protect from barcode scanner overriding keys
hardwareKeyboardPlugged=(getResources().getConfiguration().hardKeyboardHidden == Configuration.HARDKEYBOARDHIDDEN_NO);
super.onResume();
}
....
#Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus)
if (hardwareKeyboardPlugged){
//protect from barcode scanner overriding keys
hardwareKeyboardPlugged=false;
((InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE)).showInputMethodPicker();
Toast.makeText(this, "USB device detected. Turn OFF hardware keyboard to enable soft keyboard!", Toast.LENGTH_LONG).show();
}
}
I'm writing a Android NDK application, (targeted at API 8), which is based on the hello-gl2 example and so uses a single full-screen OpenGL2.0 view.
I need to bring up the on-screen keyboard and so from the info available on this site i can now bring up the keyboard, send the keyup events to my native application for processing and close the keyboard. All is well until i switch to Swype or any other more-advanced-then-stock keyboard input manager and it doesn't work.
After reading the KeyEvent developer doc class overview what i'm seeing seems to be the expected behaviour:
You should never rely on receiving KeyEvents for any key on a soft
input method.
So i've been trying to work out a way to capture the output of a IME which i can then pass down to my native application. Unfortunately i'm not getting very far with this at all.
I've got this code in my extended GLSurfaceView class:
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
{
outAttrs.actionLabel = "";
outAttrs.hintText = "";
outAttrs.initialCapsMode = 0;
outAttrs.initialSelEnd = outAttrs.initialSelStart = -1;
outAttrs.label = "";
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
outAttrs.inputType = InputType.TYPE_NULL;
return new BaseInputConnection(this, false);
}
#Override
public boolean onCheckIsTextEditor()
{
return true;
}
And i'm calling:
public boolean showSoftInput(View view, int flags)
passing in the view returned from getCurrentFocus, from native code to display the keyboard.
Is it possible to be able to extract the text from the on-screen software keyboard not using the keyup event? Is it possible to update what i've got to be able to do this or should i be looking at a completely different method?
Apologies for the questions, as you can tell, the non-native Java side of Android development is quite new to me.
As a summary my current implementation is based on this answer which allows the soft-keyboard text to be captured using key events however i'd like to capture the text not using key events.. (possibly a TextWatcher.. though my View isn't editable?).
Many thanks for any help or advice offered,
Andy
I have a game that uses a callback to Java from C++ to force open the soft keyboard when the user touches the screen. The Java code is simply this:
this._inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
This has worked fine for a while but recently we've been receiving complaints from some Motorola Droid users that the soft keyboard fails to open for them. Since we've only recently started to get these complaints and it's a number of users I'm thinking it was some kind of update to those devices.
Is there a better way I can force the keyboard to open? All the links I find online talk about using textbox controls and such but my app is primarily C++ and doesn't use the standard controls at all.
I don't know if this is related to your problem, but I was running into some issues using only InputMethodManager.toggleSoftInput() when devices would sometimes get "out of sync" and hide when I wanted to show and vice versa.
I've had some success by taking advantage of the fact that while IMM.showSoftInput() won't show a keyboard, IMM.hideSoftInputFromWindow() will reliably close one, so when I want to show a keyboard I now call IMM.hideSoftInputFromWindow() followed by IMM.toggleSoftInput(), and use IMM.hideSoftInputFromWindow() by itself to hide one.
[A day later...]
Writing the above yesterday made me rethink how I was dealing with the soft keyboard (I mean, showSoftinput() does work, just not the way we expected it to) and so here is a better way to do it:
First, you need to set up your view so that Android knows it can have a soft keyboard - described in the docs for InputMethodManager. In my case I have a single view derived from GLSurfaceView and so I added:
setFocusable(true);
setFocusableInTouchMode(true);
to the constructor and then the following 2 overrides:
#Override
public InputConnection onCreateInputConnection(EditorInfo outAttrs)
{
outAttrs.actionLabel = "";
outAttrs.hintText = "";
outAttrs.initialCapsMode = 0;
outAttrs.initialSelEnd = outAttrs.initialSelStart = -1;
outAttrs.label = "";
outAttrs.imeOptions = EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_EXTRACT_UI;
outAttrs.inputType = InputType.TYPE_NULL;
return new BaseInputConnection(this, false);
}
#Override
public boolean onCheckIsTextEditor ()
{
return true;
}
Now I can show the keyboard with:
InputMethodManager mgr = (InputMethodManager)mActivity.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(mView, 0);
and the keypresses get reported via the view's onKeyUp() and onKeyDown() methods.
Hiding it is still done using hideSoftInputFromWindow()