6

here is my listview

<ListView android:layout_width="match_parent"
            android:layout_height="match_parent" android:id="@+id/ListView"
            android:fastScrollEnabled="true" android:divider="@null"
            style="@drawable/listviewfastscrollstyle"
            ></ListView>

This is listviewfastscrollstyle style file

<style> 
<item name="android:fastScrollTrackDrawable">@drawable/listselector</item> 
<item name="android:fastScrollThumbDrawable">@drawable/listselector</item> 
</style>

This is listselector file

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" android:drawable="@drawable/channelarrows_down" /> 
 <item android:drawable="@drawable/minimize" /> 
 </selector>

But still the list view fast scroll bar is not getting customized.

2 Answers2

9

The style you created isn't correct. You need to give it a name. I'm not sure what happened to your last post about this. Do the following:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="listviewfastscrollstyle" parent="android:Theme">
    <item name="android:fastScrollTrackDrawable">@drawable/listselector</item>
    <item name="android:fastScrollThumbDrawable">@drawable/listselector</item>
</style>

</resources>

In your Manifest set the style like this:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/CustomTheme">

As requested, this is the ListView I was testing on:

<ExpandableListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:fastScrollAlwaysVisible="true"
    android:fastScrollEnabled="true"
    android:indicatorLeft="8dip"
    android:indicatorRight="52dip"
    android:paddingRight="32dip" />
pablisco
  • 14,027
  • 4
  • 48
  • 70
adneal
  • 30,484
  • 10
  • 122
  • 151
  • Should the file name of the style also be CustomTheme.xml? –  Feb 08 '12 at 06:16
  • The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder. – adneal Feb 08 '12 at 06:18
  • I have put the style file directly in values folder with the name listviewfastscrollstyle.xml. But I'm getting compilation error that " D:\30_JAN_2012\res\values\listviewfastscrollstyle.xml:1: error: Invalid start tag Style". Here is the code of that –  Feb 08 '12 at 06:32
  • Just copy and paste my edit. Everything works. Mark the answer as correct, please. – adneal Feb 08 '12 at 06:36
  • Surely I'll accept:).But still it is not working..Here is the style code and I have put it directly in values folder –  Feb 08 '12 at 06:59
  • Here is the application tag –  Feb 08 '12 at 07:02
  • You're Manifest is wrong. Change "style=@style..." to this `android:theme="@style/listviewfastscrollstyle"` Which is exactly what I have in my post. – adneal Feb 08 '12 at 07:05
  • I did the required change But still it is not resolved dont know what the issue is..That is the reason I have asked you to send the tested code across:)..My issue is not yet fixed if you still want me to accept the answer ,I'll do that..But I should appreciate for consitent support:) –  Feb 08 '12 at 07:28
  • Accepting my answer is up to you. I've tested everything I posted to double check it wasn't me throwing you off. You should take some time to figure out where you're messing up. Everything I've posted is correct. I've done all I can do. It's up to YOU now. – adneal Feb 08 '12 at 07:39
  • There was some issue in application of themes.. I have fixed it , thanks aneal for your support:) –  Feb 09 '12 at 03:54
  • There is an issue with the customized fast scroll drawable..When I scroll the list view items by scrolling the items(not with fast scroll thumb), the fast scroll bar thumb's image size is reducing initially and again increasing. The height of the fast scroll track is not remaining constant. –  Feb 09 '12 at 06:05
  • Is there a way to apply this on a per listview or per activity basis. It seems insane that I have to apply this style to the application but I could not get a custom style working for a single activity or theme. The style attributes only applied when applied to the whole application. – sgarman May 15 '12 at 21:49
  • I would also like to know how it would be for pre-honeycomb versions? – Sandra Oct 17 '13 at 08:52
  • Any way to change this in runtime? Except setFastScrollStyle since it requires api 21... – JanBo Mar 02 '16 at 08:33
8

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:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/ListView"
    android:fastScrollEnabled="true" 
    android:divider="@null"/>

and in case you need it this is what your ThumbDrawable could look like:

fast_scrollbar_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="10dp" android:bottom="10dp">
        <shape android:shape="rectangle">
            <size
                android:height="45dp"
                android:width="5dp" />
            <solid android:color="#DA414A" />
        </shape>
    </item>
</layer-list>

AFAIK there is no why to style the FastScroll bar pre-HoneyComb (API 11)

JustinMorris
  • 7,259
  • 3
  • 30
  • 36
  • thanks a lot for this! Do you know if I can change the text size of the label that appears as I scroll with this method? – MinaHany Nov 12 '16 at 14:41