1

Is it possible to enable/disable touch event in some parts of the screen that I define on the webview ?

Walid
  • 700
  • 2
  • 10
  • 29
  • Not directly as far as I know. Could you add a (transparent) view on top of the `WebView`? That would intercept the touch events. – shkschneider Aug 21 '18 at 11:56

2 Answers2

1

It is possible to define your actions by javaScript in your web page and then trigger a java method in your android code

As referred here define an interface like bellow:

/** Instantiate the interface and set the context  */
class WebAppInterface(private val mContext: Context) {

    /** Show a toast from the web page  */
    @JavascriptInterface
    fun showToast(toast: String) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show()
    }
}

And then attach it to the webView

val webView: WebView = findViewById(R.id.webview)
webView.addJavascriptInterface(WebAppInterface(this), "Android")

and you should implement a function like below in your web page and trigger it whenever you need

<input type="button" value="Say hello" onClick="showAndroidToast('Hello Android!')" />

<script type="text/javascript">
    function showAndroidToast(toast) {
        Android.showToast(toast);
    }
</script>
Mosius
  • 1,602
  • 23
  • 32
  • I may not have made myself clear in this question. I'm not asking about how to trigger some functions, I'm asking if it's possible on my webview on Android, I define just some areas to catch the touch in the view, not the entire view. like just some parts on the view.. – Walid Aug 21 '18 at 14:10
  • @That_some_guy are the areas related to the content of the page or it's just a specific part of the screen? – Mosius Aug 22 '18 at 14:23
  • On the screen, for example, the webView has buttons on top left, bottom right corners, the rest of the content is transparent to let the user use the app. the webview size is the size of the screen. The touch on the webview is only enable on the top left and bottom right, and disable on the rest. – Walid Aug 23 '18 at 07:59
  • 1
    @That_some_guy check out this post https://stackoverflow.com/questions/3476779/how-to-get-the-touch-position-in-android – Mosius Aug 25 '18 at 03:23
0

Actually now Touch Actions are not supported for webview. But some workarounds are available; I am going to show it with a longpress example : I am using Pointoption because i will get the coordinate of element and will use it for longpress.

public void longpress(PointOption po) {
   //first you need to switch to native view
    driver.switchToNativeView();
    TouchAction action = new TouchAction((PerformsTouchActions) driver);
    action.longPress(po).waitAction(WaitOptions.waitOptions(Duration.ofSeconds(2)));
    action.release();
    action.perform();
    driver.switchToDefaultWebView();
}

For to get the coordinate of element i designed below methood

public PointOption getElementLocation(WebElement element) {
    int elementLocationX;
    int elementLocationY;

    //get element location in webview
    elementLocationX = element.getLocation().getX();
    elementLocationY = element.getLocation().getY();

    //get the center location of the element
    int elementWidthCenter = element.getSize().getWidth() / 2;
    int elementHeightCenter = element.getSize().getHeight() / 2;
    int elementWidthCenterLocation = elementWidthCenter + elementLocationX;
    int elementHeightCenterLocation = elementHeightCenter + elementLocationY;

    //calculate the ratio between actual screen dimensions and webview dimensions
    float ratioWidth = device.getDeviceScreenWidth() / ((MobileDevice) device)
            .getWebViewWidth().intValue();
    float ratioHeight = device.getDeviceScreenHeight() / ((MobileDevice) device)
            .getWebViewHeight().intValue();

    //calculate the actual element location on the screen , if needed you can increase this value,for example i used 115 for one of my mobile devices.
    int offset = 0;  


    float elementCenterActualX = elementWidthCenterLocation * ratioWidth;
    float elementCenterActualY = (elementHeightCenterLocation * ratioHeight) + offset;
    float[] elementLocation = {elementCenterActualX, elementCenterActualY};

    int elementCoordinateX, elementCoordinateY;
    elementCoordinateX = (int) Math.round(elementCenterActualX);
    elementCoordinateY = (int) Math.round(elementCenterActualY);
    PointOption po = PointOption.point(elementCoordinateX, elementCoordinateY);
    return po;
}

now you have a longpress(PointOption po) and getElementLocation(Webelement element) methods that gives you po. Now everything is ready and you can use them as below..

longpress(getElementLocation(driver.findElement(By.id("the selector can be any of them(xpath,css,classname,id etc.)")));
 
Hacci56
  • 39
  • 5