48

I've got a RecyclerView and would like to have scrollbar showing, when it covers more than one page.

I get no scrollbar at all. Any idea?

My layout:

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

    <CheckBox
        android:id="@+id/cl_only_empty"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="5dp"
        android:text="@string/cl_only_empty"
        android:textColor="@color/white" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/callsList"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical" />

</LinearLayout>
Sufian
  • 6,405
  • 16
  • 66
  • 120
Michael Schmidt
  • 1,199
  • 4
  • 14
  • 18
  • 1
    This looks correct. Are you sure there is no theming issue or whatever that renders scrollbars same color with background? Is there a sample project that we can look at ? – yigit Dec 09 '14 at 21:07
  • @Michael Schmidt This should work, Just remember to always use `android:scrollbars="vertical"` when you want to display the `scrollbar` with `recyclerview` – Sheraz Ahmad Khilji Nov 10 '15 at 16:26
  • Maybe you need to add `android:scrollbarStyle="outsideOverlay"` to your `RecyclerView`. – Sufian Aug 27 '16 at 18:15
  • Did you resolve it somehow? – hushed_voice Aug 09 '19 at 09:43
  • Found a good article for scrollbar styling(working for scrollbar and RecyclerView both)... http://androidopentutorials.com/android-vertical-scrollbar-styling/ – Shubham Suryavanshi Dec 06 '19 at 07:54

12 Answers12

38

The solution is to set the vertical (or horizontal) scrollbar in the xml layout:

<android.support.v7.widget.RecyclerView
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:scrollbars="vertical" />
j2ko
  • 2,479
  • 1
  • 16
  • 29
jbiral
  • 1,431
  • 11
  • 16
32

Use android:scrollbars attribute "vertical" and android:scrollbarThumbVertical attribute to set the color and android:scrollbarSize attribute to specifiy size:

<android.support.v7.widget.RecyclerView
        android:id="@+id/document_listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="3dp"
        android:layout_marginTop="3dp"
        android:scrollbars="vertical"
        android:scrollbarThumbVertical="@android:color/darker_gray"
        android:scrollbarSize="5dp"
        android:background="@color/activity_bg"
        android:dividerHeight="4dp" />
Mandeep Yadav
  • 707
  • 6
  • 17
  • 2
    Note: Setting the background to some color or even transparent is essential on some devices (got a problem with some 8.1 tablet)! – Dominikus K. Nov 15 '19 at 11:13
9

use recyclerView as below in xml layout

   <android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerView"
    android:scrollbars="vertical"
    android:fadeScrollbars="true"
    android:layout_width="match_parent"
    android:layout_height="match_parent"  />

and add below code for scrollview in java, it will be okay

 RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
 recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
 recyclerView.setHasFixedSize(true);
Soe Naing Tun
  • 375
  • 4
  • 10
9

I had the same problem on my old HTC Desire X (api 16) only.
I don't know why, but scollbars of RecyclerView doesn't work properly on this device if the android:background property is not set. Try to set it to any color or to transparent - it works for me, hope it helps you too

smbd uknow
  • 2,282
  • 1
  • 14
  • 22
  • 1
    I was able to reproduce it on different emulators, adding background to RecyclerView does help! The correct answer to original question should be setting both `android:scrollbars` and `android:background` properties. – ntoskrnl Nov 30 '18 at 13:13
  • This is the actual solution! Thanks – Harminder Singh Sep 26 '22 at 20:34
5

You can use from :

setScrollbarFadingEnabled(boolean)

Scrollbar Link

5

Scroller can be set to recyclerview on multiple ways. 1st you can simply add scrollbar in xml and set its property android:fadeScrollbars="false" to always show it.

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerViewMachine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
         android:scrollbars="vertical"
        android:fadeScrollbars="false"
        android:scrollbarThumbVertical="@android:color/darker_gray"
        android:scrollbarSize="5dp"
        android:scrollbarStyle="outsideOverlay"/>

Or you can make a style theme and use it programitically when initializing recyclerview

  <style name="ScrollbarRecyclerView" parent="android:Widget">
        <item name="android:scrollbars">vertical</item>
    </style>

RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));

thanks

Mudassir Khan
  • 1,714
  • 1
  • 20
  • 25
4

Besides the android:scrollbars attribute, you should add android:fadeScrollbars attribute in false state like this:

<android.support.v7.widget.RecyclerView
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:scrollbars="vertical"
  android:fadeScrollbars="false"/>

This way, the vertical scrollbar is always showing when it's using more height of the layout permitted.

Dan Ponce
  • 636
  • 1
  • 8
  • 24
2

Try this:

mLayoutManager = new LinearLayoutManager (this);  
mLayoutManager.setSmoothScrollbarEnabled (true);
Pang
  • 9,564
  • 146
  • 81
  • 122
mio4kon
  • 1,503
  • 1
  • 10
  • 15
0

If you are fine to set ScrollBar programmatically, then you can use ContextThemeWrapper. First you need to define styling in Style.xml file:

<style name="ScrollbarRecyclerView" parent="android:Widget">
    <item name="android:scrollbars">vertical</item>
</style>

And then apply styling when you initialize your RecylerView:

RecyclerView recyclerView = new RecyclerView(new ContextThemeWrapper(context, R.style.ScrollbarRecyclerView));
Ayaz Alifov
  • 8,334
  • 4
  • 61
  • 56
0

Add the code to Recycler view xml to make scroll bar visible.

<android.support.v7.widget.RecyclerView
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:fadeScrollbars="false"
 android:scrollbars="vertical"
 android:scrollbarSize="@dimen/_2sdp"
 android:scrollbarThumbVertical="@color/white"/>
0

Actually, it's because of the theme. The scrollbars are there but can't be seen because they blend with the color on the parent view or some other view in the line of its ancestors.

You can create a drawable shape and give it the color you want then set that as the vertical or horizontal scrollbar drawable. That is, if you do not want to mess around with your theme colors.

TheRealChx101
  • 1,468
  • 21
  • 38
0

Using xml android:fadeScrollbars="false"

Using java ScrollView.setScrollbarFadingEnabled(false);

tk_
  • 16,415
  • 8
  • 80
  • 90