Android ListView fastScrollThumbDrawable not working
Asked Answered
S

5

2

i am doing an app where i want the fastscroll tab on my listview to be a custom thumb tab. reading the docs online i thought this would do it:

<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:cacheColorHint="@color/myWhite"
android:scrollbars="vertical"
android:scrollbarThumbVertical="@drawable/scrollthumb"
android:scrollbarSize="12dip"
android:fastScrollThumbDrawable="@drawable/scrollbar_thumb"
android:fastScrollEnabled="true"
/>

The list view is fine, the custom scrollbar colours are working and the fastscrollbar tab displays, but it is using the default thumb image and not the png file scrollbar_thumb.

does the thumb image need to be in a certain format or size ? can it be changed to a custom graphic, if not can the colour of the thumb be changed at least ?

any help will be much appreciated

Surfperch answered 19/2, 2012 at 10:4 Comment(0)
P
2

In the ListView XML definition, add

android:fastScrollEnabled="true"

or in code

listView.setFastScrollEnabled(true);

Create file fastscroll_thumb.xml in the res/drawable folder as follows:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/fastscroll_pressed" />
    <item android:drawable="@drawable/fastscroll" />
</selector>

In AndroidManifest.xml, set a custom theme for your application:

<application
    android:theme="@style/ApplicationTheme"
    ...>

Create a values folder in the res folder. Create themes.xml files in res/values as follows:

<resources>
    <style name="ApplicationTheme">
        <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb</item>
    </style>
</resources>

Lastly make sure that fastscroll.png and fastscroll_pressed.png exist in your drawable folder

Polymer answered 4/7, 2013 at 11:31 Comment(3)
I like your clear explanations. I don't understand though why I need to set the android:theme in AndroidManifest instead of another layout file in the LinearLayout element.Jorie
In the version of android that this was written for, the only way to change the fast scroll thumbnail was to set it across the entire application using the application theme. It may be possible to set it on a LinearLayout in a more current version of android, but I will have to check that and get back to you.Polymer
I have followed this procedure, but i am still not able to change the fastScrollThumbDrawable.Larcener
P
0

Note that the android:fastScrollThumbDrawable attribute only applies for Android API Level 11 and later.

http://developer.android.com/reference/android/R.attr.html

Pilocarpine answered 22/4, 2012 at 6:59 Comment(2)
Mine still does nothing in ICS. I had to apply a theme to my application to get it custom fast scrollers.Igbo
@sgarman, could you please explain (as an answer perhaps?) what you mean by applying a theme to your application in order to get custom fastscroll thumbs?Telesthesia
B
0

I'm using the android:fastScrollThumbDrawable but I not know why it isn't working, so searching on web i found here a hard code solution, I not know if it works on old API but in my case was solved the problem. Note I'm using API 18 like target and a device with API 17 to test.

the code:

try {
    Field f = AbsListView.class.getDeclaredField("mFastScroller");
    f.setAccessible(true);
    Object o = f.get(<<your listView here>>);
    f = f.getType().getDeclaredField("mThumbDrawable");
    f.setAccessible(true);
    Drawable drawable = (Drawable) f.get(o);
    drawable = getResources().getDrawable(R.drawable.<<your thumb drawable here can be a selector>>);
    f.set(o, drawable);
} catch (Exception e) {
    e.printStackTrace();
}
Blanka answered 21/8, 2013 at 7:58 Comment(0)
C
0

In order to change the fastScrollThumbDrawable, the fastScrollTrackDrawable, or the text color of the fastscroll SectionIndexer you have to use a Context Theme. The other answers recommend overriding the application's theme via the AndroidManifest to do this. That does work but if you want different scrollbar appearances per ListView you can't do that. Also, the way you change the text color on SectionIndexer shouldn't be done in your app theme because it may have other undesired effects.

The best way to style a ListView for fastscrolling is to create a custom ListView that uses a ContextThemeWrapper.

Here is an example:

public class FastscrollThemedListView extends ListView {
    public FastscrollThemedListView(Context context, AttributeSet attrs) {
        super(new ContextThemeWrapper(context, R.style.FastScrollTheme), attrs);
    }
}

That is all you need. Your style will look like this:

<style name="FastScrollTheme">
    <item name="android:textColorPrimary">?android:textColorPrimaryInverse</item>
    <item name="android:fastScrollThumbDrawable">@drawable/fast_scrollbar_thumb</item>
    <item name="android:fastScrollTrackDrawable">@drawable/fast_scrollbar_track</item>
</style>

textColorPrimary is how you hook is how you hook into the SectionIndexer font color if you use it.

Your ListView would look like this:

<com.yourapp.view.FastscrollThemedListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:drawSelectorOnTop="false"
    android:cacheColorHint="@color/myWhite"
    android:scrollbars="vertical"
    android:scrollbarSize="12dip"
    android:fastScrollEnabled="true"/>
Craunch answered 5/3, 2014 at 22:24 Comment(0)
C
0

I tested the previous answers and came up with this simplified version w/minimum requirements for a custom thumb icon for fast scrolling:

Android Manifest: (application tag)

android:theme="@style/ApplicationTheme"

res/values/themes.xml:

<resources>
  <style name="ApplicationTheme">
    <item name="android:fastScrollThumbDrawable">@drawable/scrollbar</item>
  </style>
</resources>

layout\activity_main.xml: (or wherever your view is that you want to apply this to & enable fast scrolling) ADD this attibute:

<ListView android:fastScrollEnabled="true" />

Celebes answered 15/1, 2015 at 20:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.