Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable SoftKeyboard inside a WebView

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?

like image 705
MrMime Avatar asked Apr 02 '15 08:04

MrMime


1 Answers

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"
like image 197
Android Team Avatar answered Sep 22 '22 06:09

Android Team