10

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);
MetaColon
  • 2,895
  • 3
  • 16
  • 38
  • for me only `android:windowSoftInputMode="stateAlwaysHidden"` works. have you tried that? – Sagar May 01 '18 at 12:03
  • Well, it's kind of included in `android:windowSoftInputMode="stateHidden|stateAlwaysHidden"`, isn't it? But I also tried it without the `stateHidden`, but the soft keyboard still shows up. – MetaColon May 01 '18 at 12:05
  • Well setting both could yield undefined result. Can you try to clean and rebuild? – Sagar May 01 '18 at 12:07
  • Still doesn't work – MetaColon May 01 '18 at 12:09
  • Oh. That's strange. Update this information in your question so that someone else could answer and doesn't recommend same options – Sagar May 01 '18 at 12:09
  • One more thing instead of passing `0` in `manager.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);` can you pass `InputMethodManager.HIDE_IMPLICIT_ONLY` – Sagar May 01 '18 at 12:13
  • @Sagar still doesn't work – MetaColon May 01 '18 at 12:14
  • Does it help to define your own (empty) custom keyboard and let the system show that instead of the normal keyboard for the webView? https://stackoverflow.com/questions/9577304/how-to-make-an-android-custom-keyboard – findusl May 17 '18 at 08:49

4 Answers4

8

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 :)

Abdul Ahad
  • 435
  • 4
  • 14
  • 1
    I could have sworn that I had already tried this. Anyway, it seems to work, thank you! However, a slight disadvantage is that the currently selected textbox isn't highlighted anymore. Hence I won't accept your answer as an answer, but if no other solution follows, you'll receive the bounty. – MetaColon May 16 '18 at 13:54
  • Alright, thank you for the feedback on it. Ill look for a work around in the meantime, hoping to come up with a solution – Abdul Ahad May 18 '18 at 04:33
4

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" />
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

call this before everything

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Mechadroid
  • 17
  • 3
0

Paste this in your onCreate method after setContentView

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

Keyboard wont show when you run this activity.

Kopi Bryant
  • 1,300
  • 1
  • 15
  • 30