Search code examples
androidwebviewtouch

Android Development. webview doesn't disappear ontouchevent


This is my code

  public boolean onTouchEvent(MotionEvent event) {
    wv.setVisibility(View.GONE);
    return true; // Required for receiving subsequent events (ACTION_MOVE, ACTION_UP)
}

It doesn't do anything I want the webview to disappear when the screen is touched. The webview is being used to host a ad.

My activity_main.xml code is:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.catching.apples.MainActivity"
tools:ignore="MergeRootFrame">


<WebView
android:id="@+id/activity_main_webview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     />
<WebView
    android:id="@+id/webbanner"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal|bottom" />

</FrameLayout>

Solution

  • In past, i've got problem with Visibility using FrameLayout, so i suggest you to switch to LinearLayout to solve this problem.

    Changing your layout to:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.catching.apples.MainActivity"
        tools:ignore="MergeRootFrame">
    
        <WebView
            android:id="@+id/activity_main_webview"
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1" />
    
        <WebView
            android:id="@+id/webbanner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>
    

    Using wv.setVisibility(View.GONE);, now i'll work.

    And using layout_height="0dip" and layout_weight="1", the activity_main_webview will fit all screen except by the webbanner.