Android listview scrollbar on top of list items

647 views Asked by At

I have a listview with a scroll bar. I want the scrollbar to sit on top of the list items on the right side, but have a small margin right. So that the scroll bar is slightly indented and does not rest against the right side of the list view.

I am using <item name="android:scrollbarStyle">insideOverlay</item> and have a custom drawable for my android:scrollbarThumbVertical. For my android:scrollbarTrackVertical I just have it set to a fully transparent color.

I tried adding a android:layout_marginRight and android:paddingRight to the listview, but that made it so that the listview items did not extend past the scrollbar. What I want is the listview items to go under the scorllbar and be slightly visible on the other side.

Is there something I am missing? I could post more code, but I wanted to keep it simple to start, but please let me know if more will be helpful.

Thanks!

EDIT: just in case it is helpful, here is more code:

<RelativeLayout
     android:id="@+id/mainRL"
     android:layout_width="0dp"
     android:layout_height="match_parent"
     android:layout_weight="1">
     <ListView
         android:id="@+id/mainLV"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         style="@style/scrollbar_shape_style">
     </ListView>
</RelativeLayout>

<style name="scrollbar_shape_style">
    <item name="android:scrollbarAlwaysDrawVerticalTrack">true</item>
    <item name="android:scrollbars">vertical</item>
    <item name="android:fadeScrollbars">true</item>
    <item name="android:scrollbarThumbVertical">@drawable/scrollbar_vertical_thumb</item>
    <item name="android:scrollbarTrackVertical">@color/fully_transparent</item>
    <item name="android:scrollbarSize">12dp</item>
    <item name="android:scrollbarFadeDuration">@integer/fade_ms</item>
    <item name="android:scrollbarDefaultDelayBeforeFade">@integer/ms_to_fade</item>
    <item name="android:scrollbarStyle">insideOverlay</item>
</style>

EDIT 2: I tried the answer from: How to set margin for Vertical Scrollbar from the right edge in ListView? (the image at the top is my goal btw) and that solution is not working. It just moves my listview items over as well, it does not make the scrollbars inset greater.

1

There are 1 answers

0
PFranchise On BEST ANSWER

I ended up altering my drawable. Here is that code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
    <shape>
        <gradient
            android:angle="0"
            android:endColor="@color/primary_dark"
            android:startColor="@color/primary_neutral" />
        <corners android:radius="6dp" />

        <stroke
            android:width="16dp"
            android:color="@color/fully_transparent" />
    </shape>
</item>
</layer-list>

Then, I changed <item name="android:scrollbarSize">12dp</item> to be 24dp. This resulted in a bar that is 8dp wide and offset 16dp.