Stuart
Stuart

Reputation: 66882

How to override the Android ListView fast scroller appearance

I'm trying to implement a ListView with a FastScroll mechanism which uses time rather than A-Z

Unfortunately I can't seem to find a way into the layout used by the FastScroller index - it seems determined to show a small black square with very large white text

I've looked at the source:

Both of these seem to show the key fast scroll member variables are private, and that the resource id used is fixed:

mOverlayDrawable = res.getDrawable(com.android.internal.R.drawable.menu_submenu_background);

Is there any way to override this? Ideally I'm targeting 2.2. and above.

Upvotes: 2

Views: 974

Answers (1)

dmon
dmon

Reputation: 30168

You can try with reflection with something like this:

try {
  Field scrollerField = AbsListView.class.getDeclaredField("mFastScroller"); //java.lang.reflect.Field
  scrollerField.setAccessible(true);
  FastScroller instance = scrollerField.get(listViewInstance);

  Field overlayField = instance.getClass().getDeclaredField("mOverlayDrawable");
  overlayField.setAccessible(true);
  overlayField.set(instance, yourValueHere);
} catch (Exception e) {
  Log.d("Error", "Could not get fast scroller");
}

I just typed it out so it might or might not compile straight off the bat, but that's the idea. I didn't check if the fields were called the same in all of the versions, you might have to adjust.

Upvotes: 1

Related Questions