4

I need to customize the fast scroll letter preview (the big M in the screenshot) with a custom background asset, but I can't find any documentation about it. Can you help me out?

enter image description here

Santacrab
  • 3,165
  • 2
  • 25
  • 31

2 Answers2

6

Thanks to @MAB and looking at the Lollipop source code, I managed to obtain exactly what I needed

My c_fastscroller_preview.xml:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<corners
    android:topLeftRadius="44dp"
    android:topRightRadius="44dp"
    android:bottomLeftRadius="44dp" />
<padding
    android:paddingLeft="22dp"
    android:paddingRight="22dp" />

<solid android:color="@color/c_red_e60000" /></shape>

where the c_fastscroller_thumb.xml is:

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

And in my application styles.xml:

        <!-- This belongs in a FastScroll style -->
    <item name="android:fastScrollThumbDrawable">@drawable/c_fastscroller_thumb</item>
    <item name="android:fastScrollPreviewBackgroundRight">@drawable/c_fastscroller_preview </item>
    <item name="android:fastScrollOverlayPosition">aboveThumb</item>
    <item name="android:fastScrollTextColor">@color/c_white</item>
Santacrab
  • 3,165
  • 2
  • 25
  • 31
  • It works perfectly on Lollipop, but in previous versions of Android, the fastScrollPreview is not shown at all. How can I solve it? – Santacrab May 26 '15 at 08:36
  • Strange! it seems that Holo can't handle with aboveThumb but only with floating and atThumb (default in the holo source code). But using one of them the material shape doesn't fit well. So you can try to use a specific style for Holo with a different overlay position and preview. – Pedro May 26 '15 at 21:42
2

You can achieve that by creating a custom style under res/values/styles.xml

<style name="AppTheme" parent="AppBaseTheme">
    <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb</item>
    <item name="android:fastScrollOverlayPosition">atThumb</item>
    <item name="android:fastScrollTextColor">@color/your_color</item>
    <item name="android:fastScrollTrackDrawable">@drawable/fastscroll_thumb_pressed</item>
</style>

where fastScroll_thumb is a selector

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

and fastfastscroll_thumb_pressed/fastscroll_thumb_default are drawables that you can customize to your liking

PS: Don't forget to set the style to your activity in the manifest.

Correct me if I'm wrong, but I think there are already a lot of questions discussing the same issue.

Best of luck.

MAB
  • 953
  • 7
  • 14