user6018106
user6018106

Reputation:

Focus issue in android webview cordova

Hello friends i m new in cordova and i integrate webview in my ionic application so below is my code

Hello.java

public class Hello extends CordovaPlugin {


 @Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {

    if (action.equals("greet")) {



         Context context = cordova.getActivity().getApplicationContext();

          Intent intent = new Intent(context,Main.class);

          cordova.startActivityForResult((CordovaPlugin)this,intent,100);

          callbackContext.success();


        return true;

    } else {

        return false;

       }
    }
  }

Main.java

public class Main extends CordovaActivity {


 WebView mWebView;


@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_webview);
    mWebView = (WebView) findViewById(R.id.web_view);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDisplayZoomControls(true);
    mWebView.getSettings().setSupportZoom(true);
    mWebView.requestFocus(View.FOCUS_DOWN);


    mWebView.loadUrl("https://www.google.co.in/");

}

}

EDIT layout_webview.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:descendantFocusability = "blocksDescendants"
    android:orientation="vertical"
    android:layout_height="match_parent">


    <WebView
        android:id="@+id/web_view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:focusable="true"
        />
</LinearLayout>

When i run above code i can not focus in google search edittext in my webview so any idea how can i solve this? your all suggestions are appreciable

EDIT 2

mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDisplayZoomControls(true);
mWebView.getSettings().setSupportZoom(true);
mWebView.requestFocus(View.FOCUS_DOWN | View.FOCUS_UP);
mWebView.getSettings().setLightTouchEnabled(true);
mWebView.requestFocus();
mWebView.loadUrl("https://www.google.co.in/");

EDIT 3

mWebView = (WebView) findViewById(R.id.web_view);
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setDisplayZoomControls(true);
    mWebView.getSettings().setSupportZoom(true);

    mWebView.getSettings().setLightTouchEnabled(true);
    mWebView.requestFocus();
    mWebView.loadUrl("https://www.google.co.in/");

    mWebView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_UP:
                    if (!v.hasFocus()) {
                        v.requestFocus();
                    }
                    break;
            }
            return false;
        }
    });

Upvotes: 2

Views: 1043

Answers (1)

Jay Rathod
Jay Rathod

Reputation: 11245

Try with this.

Remove android:descendantFocusability = "blocksDescendants" which prevent child layout from getting focused.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">


  <WebView
    android:id="@+id/web_view"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:focusable="true"
    />

</LinearLayout>

Add this.

Might be you enable sensitive touch.

Webview.requestFocus(View.FOCUS_DOWN|View.FOCUS_UP);

For sensitive touch.

Webview.getSettings().setLightTouchEnabled(true);

Alternative 2.

mWebView.setOnTouchListener(new View.OnTouchListener() { 
@Override
public boolean onTouch(View v, MotionEvent event) {
       switch (event.getAction()) { 
           case MotionEvent.ACTION_DOWN: 
           case MotionEvent.ACTION_UP: 
               if (!v.hasFocus()) { 
                   v.requestFocus(); 
               } 
               break; 
       } 
       return false; 
    }
}); 

Alternative 3

mWebView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_UP:
            v.requestFocusFromTouch();  
            break;
    }               
    return false;
 }

});

Hope this helps.

Upvotes: 1

Related Questions