23

Hey i implemented a NestedScrollView in an Activity, but i cant show the scrollbar like i do in a ScrollView, can you guys.

How can I show it?

<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/appBar">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipChildren="false"
            android:clipToPadding="false"
            android:orientation="vertical"
            android:paddingLeft="@dimen/dimen_2"
            android:paddingRight="@dimen/dimen_2">
        </LinearLayout>
</android.support.v4.widget.NestedScrollView>
Kokusho
  • 1,113
  • 1
  • 9
  • 14
  • 3
    you already knew the answer, you wrote the question and answer at same time. please refrain from doing this! – rupinderjeet Sep 25 '16 at 14:05
  • 3
    @rupinderjeet Actually, it is quite ok in stackoverflow, to post a question and the answer. After all, the goal of stackoverflow is, to be something where developers can find answers to their questions. It's not important, who provides the answer, as long as it is correct. – Ridcully Apr 16 '19 at 12:06
  • @Ridcully thank you. I was very naive these days. I still am sometimes, I am sorry. – rupinderjeet Apr 19 '19 at 04:42

2 Answers2

46

Use the android:scrollbars attribute.

Such as:

android:scrollbars="vertical"

android:scrollbars="horizontal"

android:scrollbars="vertical|horizontal"

For example:

<android.support.v4.widget.NestedScrollView
    android:id="@+id/foo"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="vertical">

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

Documentation link: https://developer.android.com/reference/android/view/View.html#attr_android:scrollbars

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Michael Peterson
  • 10,383
  • 3
  • 54
  • 51
6

I found the solution, first set the NestedScrollView behaviour to "@string/appbar_scrolling_view_behavior" then, I created a style to show the scrollbars in all NestedScrollViews where I need it.

in styles.xml:

<resources>
    <!-- other styles -->

    <style name="NestedScrollBarStyle">
        <item name="android:scrollbarFadeDuration">2</item>
        <item name="android:scrollbars">vertical</item>
        <item name="android:fillViewport">true</item>
        <item name="android:orientation">vertical</item>
    </style>
</resources>

in the layout:

<android.support.v4.widget.NestedScrollView
    style="@style/NestedScrollBarStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_below="@+id/appBar"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipChildren="false"
        android:clipToPadding="false"
        android:orientation="vertical"
        android:paddingLeft="@dimen/dimen_2"
        android:paddingRight="@dimen/dimen_2">
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>
Simon Warta
  • 10,850
  • 5
  • 40
  • 78
Kokusho
  • 1,113
  • 1
  • 9
  • 14
  • 1
    Thanks, two remarks: 1. do you think 2 milliseconds is a good default value for `scrollbarFadeDuration`? A ScrollView on Android 6.0 uses 250 milliseconds (confirm `Log.d(TAG, "" + (new ScrollView(this)).getScrollBarFadeDuration());`). 2. do you really need to set `app:layout_behavior`? In my test, this setting is irrelevant. – Simon Warta Feb 09 '17 at 18:21
  • I can also confirm that the app:layout_behavior attribute is not required for this, and that 250ms is a much more natural feeling fade duration. See my answer fora much simpler solution. – Michael Peterson Sep 20 '17 at 14:20