Jakob
Jakob

Reputation: 1895

Make Toolbar in NestedScrollView not scrollable

I have a custom Toolbar inside a NestedScrollView. In the NestedScrollView are also some TextViews and Edittexts.

My problem is that not only the TextViews and Edittexts are scrolled, the Toolbar is scrolled too.

It is only logical that a Toolbar in a NestedScrollView is scrolled.

I want a custom Toolbar with an absolute position at the top and scrollable TextViews and Edittexts.

This is the code I am working at:

<android.support.v4.widget.NestedScrollView
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

 <android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#ffffff"
    android:elevation="5dp"
    android:minHeight="?attr/actionBarSize"
    app:contentInsetEnd="50dp"
    app:contentInsetRight="50dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:popupTheme="@style/AppTheme.PopupOverlay">

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

  <TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="A long unimportant text"/>

</android.support.v4.widget.NestedScrollView>

So I tried to wrap only the TextViews and Edittexts in a ScrollView like:

        <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#ffffff"
            android:elevation="5dp"
            android:minHeight="?attr/actionBarSize"
            app:contentInsetEnd="50dp"
            app:contentInsetRight="50dp"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay">
            </android.support.v7.widget.Toolbar>

            <ScrollView
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="A long unimportant text"/>

                </ScrollView>

        </LinearLayout>

But it's not working.

It's very important that a custom Toolbar is used.

Upvotes: 0

Views: 1049

Answers (2)

Ben P.
Ben P.

Reputation: 54204

ScrollView (and NestedScrollView) will allow you to add multiple direct children to them, but in order for them to work properly, they must have exactly one direct child. So, wrap the contents of your ScrollView in a wrap_content LinearLayout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

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

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="A long unimportant text"/>

            <!-- many more views -->

        </LinearLayout>

    </ScrollView>

</LinearLayout>

Upvotes: 0

Veener
Veener

Reputation: 5191

Try a relative layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".EmptyActivity">

    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ffffff"
        android:elevation="5dp"
        android:minHeight="?attr/actionBarSize"
        app:contentInsetEnd="50dp"
        app:contentInsetLeft="0dp"
        app:contentInsetRight="50dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/toolbar">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="A long unimportant text" />

    </ScrollView>

</RelativeLayout>

Upvotes: 1

Related Questions