33

I've been trying to make a custom SeekBar. It's supposed to have round corners. Even the progress of SeekBar should have round corners at both sides. I don't need a thumb. Something like this.

Custom Seekbar with round corners

To achieve this, I've made a layer-list xml file, named custom_seekbar.xml as follows:

<?xml version="1.0" encoding="utf-8"?> 
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/background">
        <shape android:shape="rectangle">
            <corners android:radius="20dp" />
            <solid android:color="#F0E9DC" />
        </shape>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape android:shape="rectangle">
                <corners android:radius="20dp" />
                <solid android:color="#F0E9DC" />
            </shape>
        </clip>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape android:shape="rectangle">
                <corners android:radius="20dp" />
                <solid android:color="#E38F71" />
            </shape>
        </clip>
    </item>

</layer-list>  

And to deal with the moving round corner, I thought of using a circle thumb with height equal to that of the SeekBar. Here's thumb.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <size android:height="16dp"
        android:width="16dp"/>

    <corners android:radius="20dp"/>

    <solid android:color="#E38F71"/>

</shape>  

And I'm showing the SeekBar like this in my activity:

<SeekBar
        android:layout_width="250dp"
        android:layout_height="16dp"
        android:layout_centerInParent="true"
        android:progressDrawable="@drawable/custom_seekbar"
        android:thumb="@drawable/thumb" />  

The only problem is the thumb. It is not showing as expected. It has an unwanted background like this:

Custom Seekbar thumb with unwanted background

If I set a transparent thumb, the progress is not round anymore. I even tried using an Oval shape for thumb. Someone please tell me where I'm going wrong? Or if there's some other way I can achieve the desired results. Any help would be really appreciated.

Anjani
  • 1,533
  • 3
  • 15
  • 26
  • 1
    create a custom `Drawable` class and use it by calling `setProgressDrawable` like in [this](http://stackoverflow.com/a/21060826/2252830) answer – pskink Oct 09 '15 at 12:16
  • Create thumb.xml using layerlist – Pavya Oct 09 '15 at 12:19
  • @user3676184 can you please show me how to go about it. I tried a lot many things but couldn't get what I want. – Anjani Oct 09 '15 at 12:21
  • `"I tried a lot many things but couldn't get what I want"` did you try a custom `Drawable` ? – pskink Oct 09 '15 at 12:22
  • @pskink Apologies for not replying to your comment. I mistakenly skipped it. I didn't try a custom drawable class. Thought I might be missing upon something really small & a whole new class might not be needed. That I guess would be my last resort. – Anjani Oct 09 '15 at 12:30
  • @user3676184 I tried layer-list. Still the same. But where is this background color coming from is beyond my understanding! It's there in the background but there is no reference to it in the custom seekbar file. – Anjani Oct 09 '15 at 12:33
  • ok so keep fighting with xml... instead of writing maybe 10 lines of code... – pskink Oct 09 '15 at 12:50
  • ok... remove from your old code and check – Pavya Oct 09 '15 at 13:16
  • @pskink Thank you for spending your time to come up with a working solution & telling me that it only took 7 lines. I would've appreciated a lot more if you had shared the solution as well. Anyway, Blackbelt's solution worked for me. Adding just one line, instead of 7, `android:splitTrack="false"`, did the job. – Anjani Oct 10 '15 at 07:17
  • @Anjani no at all, your codes takes ~30 lines of code (custom_seekbar.xml) + 5 of (thumb.xml), not one – pskink Oct 10 '15 at 08:11
  • @pskink You win brother! Peace! :) Now if you could please share your solution with me. I'm sure it'll help me at some point. Also I've never worked on a custom drawable before so it'll also help me understand how to go about it. I'd be really grateful. Thanks. – Anjani Oct 10 '15 at 08:24
  • @Anjani i already posted a link, didn't i? – pskink Oct 10 '15 at 11:29

1 Answers1

96

adding android:splitTrack="false", will fix your problem. I would also use android:shape="oval", instead of rectangle with round corners, for your thumb.xml

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • I was using the "oval" shape. It had the same issue. That's why I tried with a round corner rectangle. Still nothing changed. So I just posted it like that. – Anjani Oct 09 '15 at 12:15
  • 4
    could you try adding it `android:splitTrack="false"` to your seekbar ? – Blackbelt Oct 09 '15 at 12:49
  • That's awesome. Just one line & my problem is gone! Thank you so much. You saved me hours! Please edit your answer so that I can accept it. – Anjani Oct 10 '15 at 07:09
  • 2
    this works beautifully! did you find any solution for devices pre API 21 though? – andreimarinescu Jan 28 '16 at 16:38
  • 3
    It works!! @andreimarinescu, I tried my app on pre API 21 devices and there is no problem. The situation above is only happened on post API 21 devices. – Wooseong Kim May 18 '16 at 08:53
  • I have a problem... I want a sliding effect when i move thumb right or left, please see this http://stackoverflow.com/questions/37414212/how-to-design-my-layout-like-asus-calling-screen – Bajrang Hudda May 26 '16 at 11:04
  • @Blackbelt Sorry, I'm confused as I'm not a native english speaker. I'll delete it. – iroiroys Jul 19 '16 at 05:09