Donatella Tarrio Donatella Tarrio - 24 days ago 12
Android Question

Can I use a ScrollView inside of an Android Toolbar?

I have a custom view that scrolls vertically using a

ScrollView
but when I put it inside of an
android.support.v7.widget.Toolbar
it will no longer scroll. The same thing happens if I use a
NestedScrollView
too. Is it possible to use a
ScrollView
inside of a
Toolbar
? If not, then are there alternative approaches to achieve the same thing?

Here's some example code of what I'm doing:

<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
tools:context="com.me.ExampleActivity">

<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay">

<com.me.NormallyScrollableView
android:id="@+id/myView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>

Answer

In short, not with the AppBarLayout. You have to remove that in order to get the desired scroll inside of the toolbar.

your view hierarchy should be something like this:

<?xml version="1.0" encoding="utf-8"?>
<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:fitsSystemWindows="true"
    tools:context="com.me.ExampleActivity">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

        <com.me.NormallyScrollableView
            android:id="@+id/myView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.v7.widget.Toolbar>

    <!-- Have to create Margin, since AppBarLayout is not first view in Coordinator layout (which it expects) -->
    <FrameLayout 
        android:layout_marginTop="?attr/actionBarSize"
        android:layout_height="match_parent"
        android:layout_width="match_parent">

    </FrameLayout>

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

But at this point, CoordinatorLayout is useless for this screen in your app...

So to some up, its not possible with the view hierarchy you have setup.

Good Luck and Happy Coding!