Prevent Buttons From Hiding Soft Keyboard On Android
Asked Answered
N

3

9

I have a WebView and some buttons in my layout. There is a large tag in my WebView. This app is used to edit text files. The buttons are used to effect the textarea inside my WebView. When the user presses a button (Such as an Arrow Button to Move the text view) it closes the keyboard. I have used toggleSoftInput, but that just toggles the keyboard to show or not. I want the buttons to stop hiding the soft keyboard when the button is pressed. I have found nothing about my specific problem. I have searched for weeks. Anybody have any idea on how I can stop my Buttons from hiding the Soft Keyboard on Android?

Nettie answered 4/11, 2011 at 22:28 Comment(0)
T
6

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.

InputMethodManager inputManager = (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.

Timotheus answered 29/6, 2012 at 20:27 Comment(0)
D
4

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://mcmap.net/q/833164/-android-soft-keyboard-will-hide-for-no-reason, 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 = new EditText(context);
addView(mFocusDistraction);

Then override loadUrl():

public void loadUrl(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;

public class WebViewMod extends WebView {
    public EditText mFocusDistraction;
    public Context mContext;

    public WebViewMod(Context context) {
        super(context);
        init(context);
    }    

    public WebViewMod(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }    

    public WebViewMod(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    public WebViewMod(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
        super(context, attrs, defStyle, privateBrowsing);
        init(context);
    }

    public void init(Context context) {
        // This lets the layout editor display the view.
        if (isInEditMode()) return;

        mContext = context;

        mFocusDistraction = new EditText(context);
        mFocusDistraction.setBackgroundResource(android.R.color.transparent);
        this.addView(mFocusDistraction);
        mFocusDistraction.getLayoutParams().width = 1;
        mFocusDistraction.getLayoutParams().height = 1;
    }

    @Override
    public void loadUrl(final String url) {
        if (mContext instanceof Activity && this.isFocused()) {
            ((Activity)mContext).runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mFocusDistraction.requestFocus();
                    WebViewMod.super.loadUrl(url);
                    WebViewMod.this.requestFocus();
                }
            });
        } else {
            super.loadUrl(url);
        }
    }
}
Deyo answered 12/9, 2013 at 23:52 Comment(4)
this has the problem of losing focus from a previously selected input/text field. 1) select input for typing 2) loadUrl is called 3) keyboard is still up, but typing in it does nothing (you're not typing in the same input any moreHallucinate
@HaykSaakian Really? I dunno; what I have works for me, and I don't think I made any mistakes when I put it up here. I assume that you're trying to type in the same webview as is running loadUrl, right?Deyo
maybe it's just a small window of time, and in your case you don't notice it? in my case I have a page that I call loadUrl to many times per second (chat page that uses java websockets that inject messages using loadUrl with javascript)Hallucinate
@HaykSaakian Hmm, yeah, that might do it. It does move focus off the webview for a moment while loadUrl runs. Dunno how to fix that; sorry.Deyo
K
0

Xamarin Android (Alternative):

InputMethodManager inputManager = (InputMethodManager)GetSystemService(Context.InputMethodService); 
inputManager.ToggleSoftInput (ShowFlags.Forced, HideSoftInputFlags.ImplicitOnly);
Keyek answered 29/4, 2016 at 12:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.