android listview fast scroll customization issue
Asked Answered
D

2

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.

Doi answered 8/2, 2012 at 5:40 Comment(2)
Don't do anything to your ListView. Create the style, apply it to the Application tag in your Manifest.Hollington
<application android:icon="@drawable/logo" android:label="@string/app_name" android:debuggable="true" style="@drawable/listviewfastscrollstyle" But still it is not workingDoi
H
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" />
Hollington answered 8/2, 2012 at 6:2 Comment(14)
Should the file name of the style also be CustomTheme.xml?Doi
The name of the XML file is arbitrary, but it must use the .xml extension and be saved in the res/values/ folder.Hollington
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 <style name="listviewfastscrollstyle" parent="android:Theme"> > <item name="android:fastScrollTrackDrawable">@drawable/listselector</item> <item name="android:fastScrollThumbDrawable">@drawable/listselector</item> </style>Doi
Just copy and paste my edit. Everything works. Mark the answer as correct, please.Hollington
Surely I'll accept:).But still it is not working..Here is the style code and I have put it directly in values folder <?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>Doi
Here is the application tag <application android:icon="@drawable/optimum_logo" android:label="@string/app_name" android:debuggable="true" style="@style/listviewfastscrollstyle">Doi
You're Manifest is wrong. Change "style=@style..." to this android:theme="@style/listviewfastscrollstyle" Which is exactly what I have in my post.Hollington
I did the required change <application android:icon="@drawable/optimum_logo" android:label="@string/app_name" android:debuggable="true" android:theme="@style/listviewfastscrollstyle"> 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:)Doi
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.Hollington
There was some issue in application of themes.. I have fixed it , thanks aneal for your support:)Doi
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.Doi
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.Torchwood
I would also like to know how it would be for pre-honeycomb versions?Pashm
Any way to change this in runtime? Except setFastScrollStyle since it requires api 21...Adz
D
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)

Daub answered 5/3, 2014 at 22:32 Comment(1)
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?Accord

© 2022 - 2024 — McMap. All rights reserved.