Prevent Buttons From Hiding Soft Keyboard On Android
Solution 1:
The solution to your problem might be to keep the keyboard always shown and letting the user to close it when the actions are done.
InputMethodManagerinputManager= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
Try this code in the onCreate() method of your Activity. Note that if the user presses the close button on the keyboard or the back button it should close it. And I guess you shouldn't interfere with that scenario. And when the actions are done you can then close the keyboard from code.
Solution 2:
I'm in exactly the same boat, so if you ever found an ideal solution, I'd love to hear it.
EDIT: Success! Or much better than before, anyway. I've deleted my old solution, since this one is better.
As stated in https://stackoverflow.com/a/10536033/513038, it turns out that loadData() hides the keyboard. However, I discovered that in the WebView's hideSoftKeyboard(), it checks the InputMethodManager to see if the webview is active, via imm.isActive(mWebView).
So, if you switch focus to an EditText before loadData(), and switch back to the WebView immediately after, the keyboard sticks around! It briefly switches to upper-case, I think on returning focus to the webview, (actually, this doesn't always seem to happen; it depends) but it's a lot less noticeable than the keyboard flickering away and back.
The gist of what needs to happen is as follows.
Extend WebView. Give it an EditText field:
public EditText mFocusDistraction;
In the constructor, have the following lines:
mFocusDistraction = newEditText(context);
addView(mFocusDistraction);
Then override loadUrl():
publicvoidloadUrl(String s) {
mFocusDistraction.requestFocus();
super.loadUrl(s);
this.requestFocus();
}
That should get it working, basically. It's a bit buggy, though, so here's a more complete class:
import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.webkit.WebView;
import android.widget.EditText;
publicclassWebViewModextendsWebView {
public EditText mFocusDistraction;
public Context mContext;
publicWebViewMod(Context context) {
super(context);
init(context);
}
publicWebViewMod(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
publicWebViewMod(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
publicWebViewMod(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing);
init(context);
}
publicvoidinit(Context context) {
// This lets the layout editor display the view.if (isInEditMode()) return;
mContext = context;
mFocusDistraction = newEditText(context);
mFocusDistraction.setBackgroundResource(android.R.color.transparent);
this.addView(mFocusDistraction);
mFocusDistraction.getLayoutParams().width = 1;
mFocusDistraction.getLayoutParams().height = 1;
}
@OverridepublicvoidloadUrl(final String url) {
if (mContext instanceof Activity && this.isFocused()) {
((Activity)mContext).runOnUiThread(newRunnable() {
@Overridepublicvoidrun() {
mFocusDistraction.requestFocus();
WebViewMod.super.loadUrl(url);
WebViewMod.this.requestFocus();
}
});
} else {
super.loadUrl(url);
}
}
}
Solution 3:
Xamarin Android (Alternative):
InputMethodManagerinputManager= (InputMethodManager)GetSystemService(Context.InputMethodService);
inputManager.ToggleSoftInput (ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
Post a Comment for "Prevent Buttons From Hiding Soft Keyboard On Android"