Search code examples
androidwebviewscrollviewandroid-coordinatorlayout

Android Toolbar and Webview


I've got a Webview and a Toolbar. Without the Toolbar, the Webview is completely scollable, but when I add the Toolbar the the Webview doesn't scroll anymore. I've tried NestedScrollView but it just doesn't work. Here's the layout of my activity:

<android.support.design.widget.CoordinatorLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize">
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="horizontal"
                android:weightSum="100">
                <ImageButton
                    android:id="@+id/homeButton"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="20"
                    android:contentDescription="@string/home"
                    app:srcCompat="@drawable/ic_home" />
                <TextView
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="60"
                    android:gravity="center"
                    android:text="@string/app_name"
                    android:textSize="24sp"
                    android:textStyle="bold"
                    app:fontFamily="Sans Serif" />
                <ImageButton
                    android:id="@+id/menuButton"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="20"
                    android:contentDescription="@string/menu"
                    app:srcCompat="@drawable/ic_menu_black_24dp" />
            </LinearLayout>
        </android.support.v7.widget.Toolbar>

        <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context=".MailActivity">
            <WebView
                android:id="@+id/webview"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
        </RelativeLayout>
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

The Java code for the Toolbar simply creates the menu and the home button, while the webview simply loads an url (javascript is enabled). Thanks a lot for your help!


Solution

  • <android.support.design.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize">
                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="horizontal"
                    android:weightSum="100">
                    <ImageButton
                        android:id="@+id/homeButton"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="20"
                        android:contentDescription="@string/home"
                        app:srcCompat="@drawable/ic_home" />
                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="60"
                        android:gravity="center"
                        android:text="@string/app_name"
                        android:textSize="24sp"
                        android:textStyle="bold"
                        app:fontFamily="Sans Serif" />
                    <ImageButton
                        android:id="@+id/menuButton"
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="20"
                        android:contentDescription="@string/menu"
                        app:srcCompat="@drawable/ic_menu_black_24dp" />
                </LinearLayout>
            </android.support.v7.widget.Toolbar>
    
        </android.support.design.widget.AppBarLayout>
    
                 <WebView
                    android:id="@+id/webview"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
    
    </android.support.design.widget.CoordinatorLayout>