Stack Overflow archive
5 score

fast scroll custom thumb

score
5
question views
6.1K
license
CC BY-SA 3.0

Setting the Drawable from styles is the way to go. However, if you want to do this programmatically here are two methods that should be useful:

java
/**
 * Set a ListView or GridView fast scroll thumb image.
 * 
 * @param listView The {@link android.widget.ListView} or {@link android.widget.GridView}
 * @param thumb The fast-scroll drawable
 * @return {@code true} if successfully set.
 */
public static boolean setFastScrollThumbImage(AbsListView listView, Drawable thumb) {
    try {
        Field f;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            f = AbsListView.class.getDeclaredField("mFastScroll");
        } else {
            f = AbsListView.class.getDeclaredField("mFastScroller");
        }
        f.setAccessible(true);
        Object o = f.get(listView);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            f = f.getType().getDeclaredField("mThumbImage");
            f.setAccessible(true);
            ImageView iv = (ImageView) f.get(o);
            iv.setImageDrawable(thumb);
        } else {
            f = f.getType().getDeclaredField("mThumbDrawable");
            f.setAccessible(true);
            Drawable drawable = (Drawable) f.get(o);
            drawable = thumb;
            f.set(o, drawable);
        }
        return true;
    } catch (Exception ignored) {
    }
    return false;
}

/**
 * Set a ListView or GridView fast scroll thumb color.
 * 
 * @param listView The {@link android.widget.ListView} or {@link android.widget.GridView}
 * @param color The color for the fast-scroll thumb
 * @return {@code true} if successfully set.
 */
public static boolean setFastScrollThumbColor(AbsListView listView, int color) {
    try {
        Field f;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
            f = AbsListView.class.getDeclaredField("mFastScroll");
        } else {
            f = AbsListView.class.getDeclaredField("mFastScroller");
        }
        f.setAccessible(true);
        Object o = f.get(listView);
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
            f = f.getType().getDeclaredField("mThumbImage");
            f.setAccessible(true);
            ImageView iv = (ImageView) f.get(o);
            iv.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_ATOP);
        } else {
            f = f.getType().getDeclaredField("mThumbDrawable");
            f.setAccessible(true);
            final Drawable drawable = (Drawable) f.get(o);
            drawable.setColorFilter(color, android.graphics.PorterDuff.Mode.SRC_ATOP);
        }
        return true;
    } catch (Exception ignored) {
    }
    return false;
}

Originally posted on Stack Overflow. Public user contributions are licensed under Creative Commons Attribution-ShareAlike.