Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WebView in CoordinatorLayout with CollapsingToolbarLayout

I'd like to add a WebView in a CoordinatorLayout, having CollapsingToolbarLayout in AppBarLayout. The problem is that WebView shrinks in height and doesn't fill the space below the toolbar, making it not usable. I tried also to use WebView as child of NestedScrollView: initially it doesn't work (it shrinks my WebView in height), but scrolling my shrinked WebView makes it fill viewport.

Here is the layout I used:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/main_content"
    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:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_width="match_parent"
        android:layout_height="256dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <WebView
            android:id="@+id/article"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
like image 591
Alex Facciorusso Avatar asked May 30 '15 22:05

Alex Facciorusso


4 Answers

You can use android:fillViewport="true" for NestedScrollView and layout_height="wrap_content" for WebView

like image 81
Pranit More Avatar answered Nov 13 '22 04:11

Pranit More


The bug is now fixed in the latest (22.2.1) design support library.

like image 26
Alex Facciorusso Avatar answered Nov 13 '22 05:11

Alex Facciorusso


I think set app:layout_scrollFlags="scroll|enterAlways" will be helpful

like image 39
basefas Avatar answered Nov 13 '22 04:11

basefas


this works for me.

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@color/backgroundColor"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|enterAlways|snap">

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom">

                <androidx.appcompat.widget.AppCompatImageButton
                    android:id="@+id/back_bt"
                    android:layout_width="48dp"
                    android:layout_height="48dp"
                    android:background="@color/transparent"
                    android:src="@drawable/ic_arrow_back_black_24dp" />
            </FrameLayout>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="false"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <WebView
                android:id="@+id/web_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
like image 1
Brownsoo Han Avatar answered Nov 13 '22 03:11

Brownsoo Han