Set soft keyboard default to numeric on Android browser app using jQueryMobile - android

I want to be able to open the Android softkeyboard using Javascript in numeric mode.
There are numerous posts out there about how to open the softkeyboard and about how to set its data type to numeric, but in my case there are some circumstances that prevent me from combining these solutions.
Currently, if the user manually taps the input field, the numeric soft keyboard appears correctly.
I've also figured out how to use the java method .addJavascriptInterface() to create a Javascript function that opens and closes the keyboard. That's mostly in this code:
public class KeyBoard {
private WebView mAppView;
private DroidGap mGap;
public KeyBoard(DroidGap gap, WebView view)
{
mAppView = view;
mGap = gap;
}
public void showKeyBoard() {
InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
// only will trigger it if no physical keyboard is open
mgr.showSoftInput(mAppView, InputMethodManager.SHOW_IMPLICIT);
((InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInput(mAppView, 0);
}
public void hideKeyBoard() {
InputMethodManager mgr = (InputMethodManager) mGap.getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.hideSoftInputFromWindow(mAppView.getWindowToken(), 0);
}
}
The problem is that when I use this custom Javascript-to-Java command, the keyboard always appears as Qwerty, even if the current value of the field is already numeric and it has a type=number tag.
I've tried these:
- set type=number attribute on input element.
- always have a numeric value ready (set value to zero before calling soft keyboard)
- set focus on the input (and select its value) before calling soft keyboard
- I've read solutions about setting properties of a TextView/EditText, but since my app is a browser app, the only Android object I can manipulate is the WebView.
- Setting the default input method on a Java WebView is currently not possible with Android.
The trouble with using Javascript-to-Java to open the soft keyboard is that it's a "generic" command, i.e. it's not tied specifically to the input. So the soft keyboard is opened in its default mode with no regard to the element where its keystrokes will be sent.

Related

is there a way to completely disable the SoftKeyboard in Android Application?

I am in the process of creating a wrapper application for a web-app in order to use a mobile handheld with scanner to scan barcodes and submit it to the website.
The site containing HTML input fields that are automatically focues in order to capture data from the scanner.
However, everytime this field focuses, the android soft keyboard is shown and the business does not want this behaviour.
I found a way to programmatically hide and show the keyboard using the following code:
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
//show the keyboard
imm.showSoftInput(webview, 0)
//hide the keyboard
imm.hideSoftInputFromWindow(webview.windowToken, 0)
however, as soon as I click into an input field, the keyboard reopens again.
Unfortunately, the source of the html page cannot be changed, so my idea was to use a button in the actionBar to disable/toggle soft keyboard for the activity.
Is there a way (if yes: how?) to archieve this?
Try this
webview.isFocusableInTouchMode = false
Eg : -
webview.webViewClient = WebViewClient()
webview.isFocusableInTouchMode = false
webview.loadUrl("https://www.facebook.com/")
Close soft keyboard:
private fun closeKeyBroad() {
val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager?
activity?.currentFocus?.let { softKeyBroadView ->
if (imm?.isActive == true && softKeyBroadView.windowToken != null) {
imm.hideSoftInputFromWindow(softKeyBroadView.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}
}
Use this in your activity's onCreate()
getWindow().setFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM, WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
Below is the documentation mentioned for the flag.
Window flag: invert the state of {#link #FLAG_NOT_FOCUSABLE} with
* respect to how this window interacts with the current method. That
* is, if FLAG_NOT_FOCUSABLE is set and this flag is set, then the
* window will behave as if it needs to interact with the input method
* and thus be placed behind/away from it; if FLAG_NOT_FOCUSABLE is
* not set and this flag is set, then the window will behave as if it
* doesn't need to interact with the input method and can be placed
* to use more space and cover the input method.

Soft keyboard automatic capitalization with LibGDX

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.

Prevent InputMethodService changing the Number keyboard to Alpha when the user touches on a webview for the second time

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

Show soft keyboard even though a hardware keyboard is connected

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

android: problems with programmatic invoking of soft keyboard

In my application I want to display a list of stuff and provide the user with the ability to filter the list by using the soft keyboard. To that end I added a button that should trigger (hide/show) the soft keyboard for filtering. I don't want to have a visible edit text control, cause it would take up unnecessary space. Rather than that, I would like to display a toast showing the filter query as the user types, much as the 'android:textFilterEnabled' attribute for ListView does. To my understanding there is no obvious way of doing this with available Android components. So I tried the following approach:
1) creating a layout containing invisible edit text and the list view:
<ListView android:id="#+id/main_list"
android:drawSelectorOnTop="false"
android:layout_height="0px"
android:layout_width="fill_parent"
android:layout_weight="5"
/>
2) adding button as a popup and invoking InputMethodManager on click to toggle the soft input (called in onCreate):
private void initButton() {
Button buttonView = (Button) getLayoutInflater().inflate(R.layout.keyboard_button, null);
buttonView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final View target = findViewById(R.id.filterbox);
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
// this does not work...
// imm.toggleSoftInputFromWindow(target.getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
// ... so need to track this in an instance variable - which sucks
if (imeShowing) {
imm.hideSoftInputFromWindow(target.getWindowToken(), 0);
imeShowing = false;
} else {
// check that the filterbox got focus
Preconditions.checkState(target.requestFocusFromTouch());
Preconditions.checkState(target.hasWindowFocus());
Preconditions.checkState(target.hasFocus());
imm.showSoftInput(target, 0);
imeShowing = true;
}
}
});
buttonPopup = new PopupWindow(buttonView);
// ... code to display the button as a small popup
}
As mentioned in the code sample, the 'obvious' approach (calling toggleSoftInput) does not work, so I had to revert to this ugly if-else. This is however, a secondary problem. The primary problem is that when I run this in the emulator, the soft keyboard is displayed correctly, but as soon as I start typing in it, the systems starts an intent to the google search activity! And the typed characters appear in the Google search box displayed as a result. What is even more weird, this only happens the first time I type after deploying and running the app. I.e. if I go back to my app from the Google search box, everything works as expected (no redirects to the search box). Before showing the display I make sure that the invisible edittext gets focus, so it should be the target of the soft keyboard, right??
Does anyone have any idea what is happening here?
Sorry this is me (the author of this question), I can't access my old account due to SO new 'awesome' OpenID login:/
So I figured out that what I really need to do is just turn on the 'android:textFilterEnabled' attribute of the list, and focus on it on the button click. ListView supports input from soft keyboard (although I figured it out by looking at the actual code of AbsListView rather than getting hints in any documentation:). Also, I managed to get PopupWindow working for the button (instead of Dialog that I tried previously) so the focus problem is gone. The working code for this (in case anyone has similar problem) below:
View popupView = getLayoutInflater().inflate(R.layout.keyboard_button, null);
Button buttonView = (Button) popupView.findViewById(R.id.keyboardButton);
buttonView.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
final View list = findViewById(R.id.the_list);
list.requestFocus();
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
}
});
buttonPopup = new PopupWindow(this);
The list will handle input from soft keyboard just as it does from hard one. The trick is just to trigger the keyboard and focus on the list. Also you may control how the filtering is applied to the list by setting a custom QueryFilterProvider on its associated adapter.

Categories

Resources