I have a webView and on this webView a button. I catch a touch event on the webview like this : webView.setOnTouchListener(this);.
@Override
public boolean onTouch(View v, MotionEvent event) {
if (ADS_VIDEO.contains(v.getId()) && v.getId() == R.id.webView
&& event.getAction() == MotionEvent.ACTION_DOWN) {
intentVideo = new Intent(getBaseContext(),
AdVideoActivity.class);
startActivity(intentVideo);
}
return false;
}
I also have this for my button :
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
webView.setVisibility(View.GONE);
button.setVisibility(View.GONE);
}
});
My problem is when I click on the button, the event for the webView is launched and not the event linked to the button.
Update :
xml :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dip"
android:id="@+id/relativeForWeb" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/descr_image"
/>
<ProgressBar
android:id="@+id/loading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<Button
android:id="@+id/bt"
android:layout_width="42dip"
android:layout_height="42dip"
android:text="x"
android:background = "@drawable/roundedbutton"/>
<WebView
android:id="@+id/webView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>
Because the WebView comes after the Button in your layout.xml, it lays above it (RelativeLayout sets the z-order of its views depending on the order they are in the layout.xml).
And because the webview is above the button, it gets the touch event first and consumes it.
Simply move the webview declaration before the button declaration in your layout.xml and you should be good.
Like so: