kishidp
kishidp

Reputation: 1948

Click Event Coordinates on a WebView

I have another question with Android. Today I'm trying to use the WebView to display an image. The image has a dimension larger than the view which make the user able to scroll. Now, what I want to achieve is when the user touches somewhere on the image (in the webview), an event will be triggered and the coordinates of the touch will be stored.

When I try to do this using onClick in the xml layout, all I get as a parameter is the view object that trigger the on click event. Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<WebView 
    android:id="@+id/upperview" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1"
    android:onClick="imageClicked" />
<WebView 
    android:id="@+id/lowerview" 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" 
    android:layout_weight="1" />

</LinearLayout>

Now here is my activity:

public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    WebView upperview = (WebView) findViewById(R.id.upperview);
    upperview.getSettings().setBuiltInZoomControls(true);
    upperview.loadUrl("file:///android_asset/texture.png");

    WebView lowerview = (WebView) findViewById(R.id.lowerview);
    String htmlstring = "<h1>Header</h1><p>This is HTML text<br /><i>Formatted in italics</i></p>";
    lowerview.loadData(htmlstring, "text/html", "utf-8");
}

public void imageClicked(View view) {

}
}

Now, there still no functions inside the imageClicked method because I wasn't sure where to find the coordinates. Am I doing this wrong? I was thinking maybe the onClick attribute in the xml layout only correspond to a generic click/touch in the webview. Is there an alternative to achieve what I want? Thanks in advance :)

Upvotes: 2

Views: 3466

Answers (2)

vinay kumar
vinay kumar

Reputation: 1449

Have you tried with onTouch event instead of onClick. onTouch event gives you the x and y coordinates. check here

Upvotes: 1

Anders Metnik
Anders Metnik

Reputation: 6237

What you need is the onTouchEvent: Android guide

Upvotes: 1

Related Questions