24

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>
Alex Facciorusso
  • 2,290
  • 3
  • 21
  • 34

7 Answers7

2

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

Pranit More
  • 496
  • 5
  • 13
1

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

Alex Facciorusso
  • 2,290
  • 3
  • 21
  • 34
1

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

basefas
  • 11
  • 3
1

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>
Brownsoo Han
  • 4,549
  • 3
  • 20
  • 20
0

Try to remove the attribute android:fitsSystemWindows="true" from all views.

Zenco
  • 2,963
  • 3
  • 17
  • 22
  • 1
    It doesn't work: fitting system windows doesn't stretch views, and also fitsSystemWindows is needed in order to get the statusbar scrim by the AppBarLayout. – Alex Facciorusso Jun 01 '15 at 08:39
  • Okay. I have another problem with the implementation but i have build a testapp with a webview. You can look the xml layout file in this post: http://stackoverflow.com/questions/30568772/android-design-library-appbar-scrolling-behavior. My apptheme looks like this: – Zenco Jun 01 '15 at 08:44
  • I just seen your question http://stackoverflow.com/questions/30568772/android-design-library-appbar-scrolling-behavior : how do you instantiate your webview in java code? – Alex Facciorusso Jun 01 '15 at 08:44
  • WebView wvTest = (WebView)findViewById(R.id.wvTest); wvTest.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { return super.shouldOverrideUrlLoading(view, url); } }); wvTest.loadUrl("http://stackoverflow.com/"); – Zenco Jun 01 '15 at 08:45
  • The same way I instantiate mine… I don't know why it doesn't fill all my layout! – Alex Facciorusso Jun 01 '15 at 08:51
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79296/discussion-between-alex-facciorusso-and-zenco). – Alex Facciorusso Jun 01 '15 at 08:54
0

This library seems to have workarounded this issue by extending the WebView class to add the property behaviour: https://github.com/takahirom/webview-in-coordinatorlayout

Alécio Carvalho
  • 13,481
  • 5
  • 68
  • 74
-2
    DisplayMetrics metrics = new DisplayMetrics();
    getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int statusBarHeight = getStatusBarHeight(this);

    TypedArray actionbarSizeTypedArray = this.obtainStyledAttributes(new int[]{android.R.attr.actionBarSize});
    int actionBarHeight = (int) actionbarSizeTypedArray.getDimension(0, 0);

    NestedScrollView nestedScrollView = (NestedScrollView) findViewById(R.id.nsv_scroll);
    CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(
            CoordinatorLayout.LayoutParams.MATCH_PARENT,
            metrics.heightPixels - actionBarHeight - statusBarHeight);

    nestedScrollView.setLayoutParams(params);

The method getStatusBarHeight() :

public int getStatusBarHeight(Context context){
    Class<?> c = null;
    Object obj = null;
    Field field = null;
    int x = 0;
    int statusBarHeight = 0;
    try {
        c = Class.forName("com.android.internal.R$dimen");
        obj = c.newInstance();
        field = c.getField("status_bar_height");
        x = Integer.parseInt(field.get(obj).toString());
        statusBarHeight = context.getResources().getDimensionPixelSize(x);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    return statusBarHeight;
}
Jiashu
  • 1
  • 1