Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Prevent keyboard in webView

I want it to be impossible for the soft keyboard to pop up due to an action in the my webView. That's because I have a custom "keyboard" consisting of buttons below the webView. However, I don't want to completely disable the keyboard for my application, as I have to use it in different contexts. It just shouldn't show up when the user clicks on an input field inside the webView. I also don't want the keyboard to show and instantaneously hide again.

I currently have this in my AndroidManifest.xml:

android:windowSoftInputMode="stateAlwaysHidden"

I already tried disabling the focus of the webView, but then I can't enter text with my custom "keyboard" either, as the input field of the webView aren't focused.

I also tried this in onCreate, but it didn't work (the keyboard still showed up):

View focusedView = this.getCurrentFocus();
if (focusedView == null)
    return;
InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
if (manager == null)
    return;
manager.hideSoftInputFromWindow(focusedView.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
like image 780
MetaColon Avatar asked May 01 '18 11:05

MetaColon


2 Answers

Use this in your WebView

 android:descendantFocusability="blocksDescendants"
 android:focusable="false"
 android:focusableInTouchMode="false"

Try this

<WebView
    android:id="@+id/myWebView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false" />
like image 195
AskNilesh Avatar answered Oct 21 '22 05:10

AskNilesh


sorry I'm late on this one.

But here is the solution:

Add this in your parent layout:

android:descendantFocusability="blocksDescendants"

Set these two properties of your WebView:

android:focusable="false"
android:focusableInTouchMode="true"

This works for me :)

like image 21
Abdul Ahad Avatar answered Oct 21 '22 05:10

Abdul Ahad