6

I know this is a common question but I can't find here and on Google, a functioning solution.

I have a WebView in my android app with a JSF page inside with a simple DatePicker component:

<p:calendar value="#{patientHealthDataView.dateFrom}" id="popupButtonDateFrom" showOn="button" class="tableCalendarText"/>

that show a Popup with calendar.

Thanks to this command, I manage to make the popup works:

WebView view=(WebView)findViewById(R.id.statistics);
view.getSettings().setJavaScriptEnabled(true);

Unfortunally, when I click on input text or the button to show the calendar's popup, the focus goes to Input Text and Android show its keyboard hiding the calendar.

I've tried several solution. The last one was this:

 view.setOnTouchListener(new View.OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            hideSoftKeyboard(v);
            return false;
        }
    });

public void hideSoftKeyboard(View v) {
        Activity activity = (Activity) v.getContext();
        InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }

This code, as suggested by Android Studio, raise a Null Pointer Exception on:

activity.getCurrentFocus().getWindowToken()

Other solutions says to insert a special command in manifest but I want to block the keyboard only in the WebView.

Any suggests?

jww
  • 83,594
  • 69
  • 338
  • 732
MrMime
  • 535
  • 3
  • 9
  • 23
  • 1
    Have you tried `inputMethodManager.hideSoftInputFromWindow(webView.getWindowToken(), 0);` where `webView` is your `WebView view=(WebView)findViewById(R.id.statistics);` ? – NSimon Apr 02 '15 at 08:51
  • Thanks for the answer. I've just tried. Unfortunally this solution doesn't work. – MrMime Apr 02 '15 at 09:13
  • Looks like the best "hack" yet is to know when the keyboard has been deployed, and hide it from there. Solutions can be found on this question : http://stackoverflow.com/q/17672116/4706693 – NSimon Apr 02 '15 at 09:18
  • possible duplicate of [Disabling Android Keyboard's 'Go' Button for WebView Text Entry](http://stackoverflow.com/questions/5249934/disabling-android-keyboards-go-button-for-webview-text-entry) – Vrashabh Irde Apr 02 '15 at 09:42
  • try these hacks https://stackoverflow.com/a/34548564/6584867 – NSetty Sep 10 '18 at 16:07

2 Answers2

12

Add following code in main(parent) layout in your layout.xml

android:descendantFocusability="blocksDescendants"

Hope it will work.

and set following property in your webview

android:focusable="false" 
android:focusableInTouchMode="true"
Android Team
  • 11,274
  • 2
  • 26
  • 46
  • 1
    My WebView partent tag is a LinearLayout. I've tried using your code as below: android:descendantFocusability="beforeDescendants" android:focusableInTouchMode="true" but it doesn't work. I've also tried with "false" in focusableInTouchMode but its the same. The Keyboard Still Shows. – MrMime Apr 02 '15 at 10:15
0

I had a similar issue. I was using a text input with a datetimepicker:

HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" required

JQuery: $('#some_id').datetimepicker({format: \'YYYY-MM-DD\'});

However this caused issues because the soft keyboard would pop up when you tried to select a date. I then tried to fix it with the below:

HTML: input type="text" placeholder="Birth Date" id="some_id" name="some_id" value="" onfocus="blur();" required

This worked but it took way too long to actually open the calendar, but at least the soft keyboard wasn't popping up. This is just to show you what I tried at first.

MY SOLUTION: input type="date" placeholder="Birth Date" id="some_id" name="some_id" value="" required

All I had to do was to change the input type from "text" to "date" and remove the jquery line.

Hope this helps someone. :)

Carl Du Plessis
  • 167
  • 1
  • 6