57

Variations of this question can be found all over the internet but not an answer.

I want a seekbar with two-thumb range selection. I'm willing to program this myself but I lack experience with Android. Could someone give me some pointers on where to start. I mean, I know I will have to extend something (ProgressBar probably), but how should I go about to do that? Do I really have to recreate all the functionality of a standard seekbar, or is there an easier way?

Complete solutions are also welcome of course ;)

artless noise
  • 21,212
  • 6
  • 68
  • 105
SeeDoubleYou
  • 1,253
  • 2
  • 15
  • 25
  • 1
    Why not try extending SeekBar itself or perhaps AbsSeekBar? THis is the source for SeekBar...http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/2.1_r2/android/widget/SeekBar.java you could look at that and AbsSeekBar source to see what it takes to add and handle a second thumb. – Squonk Apr 26 '11 at 20:50
  • Hello MisterSquonk, I tried that but it is very complex to extend/ modify AbsSeekBar, I already create my own control using ImageView. It's very easy and simple –  May 09 '11 at 06:49
  • 2
    fully customize seekbar http://codingsignals.com/crystal-range-seekbar-in-android/ – Asif Jul 19 '16 at 06:51

5 Answers5

110

I too was looking for this, to no avail. So I made a new widget, feel free to use it: https://github.com/anothem/android-range-seek-bar

Screenshot of RangeSeekBar

Stephan Tittel
  • 1,313
  • 1
  • 10
  • 7
  • this one is awesome! It would be even more awesome if the start and end could be reversed and if it would maintain state (loaded from Preferences or something) – slinden77 Jul 09 '12 at 16:15
  • Anything like this but extending normal SeekBar? – powder366 May 28 '14 at 11:03
  • Thank you very much for allowing us to use this. I did have a question on how to get it on the layout though. In your example you had ` // add RangeSeekBar to pre-defined layout ViewGroup layout = (ViewGroup) findViewById(); layout.addView(seekBar);` but I wasn't sure how to get the viewgroup in the layout. Can you give an example of how to set the view in the layout? Thank you! (This really came out jumbled.. I apologize) – Brandon Aug 04 '15 at 21:05
  • 1
    @Brandon The example was meant to be as generic as possible. ViewGroup is a superclass of most layouts. Replace it with a specific layout of your choice - e.g. RelativeLayout. – Stephan Tittel Aug 05 '15 at 05:50
  • Stephan, later on in the evening I realized that is probably what you intended with the view group and not the actual item. Thank you for the quick response though! – Brandon Aug 05 '15 at 12:58
  • @StephanTittel, Thanks for your answer.it is working fine but i have one issue,time gap between the min n max value is 3.when time gap equals 3,range seek bar sliders are not moving to gether.how to do this?In my case both sliders not cross over. – Rajesh Mar 18 '16 at 14:12
  • 2
    Fully customize seek bar with two way http://codingsignals.com/crystal-range-seekbar-in-android/ – Asif Jul 21 '16 at 07:00
  • The link now goes to a "Google Code Archive" page. The source page is blank, the downloads page too. Does anyone have a copy of the code? – androidguy Dec 05 '16 at 06:39
  • @user3175580 it has been moved to https://github.com/anothem/android-range-seek-bar; I will edit the post. –  Jan 19 '17 at 10:13
  • RangeSeekbar is a 1000-line abomination of terrible code and poor performance and should not be used. – Paul Lammertsma Dec 20 '17 at 16:25
  • 1
    This library doesn't seem to build correctly anymore. Also deprecated. I don't know why. – John61590 Apr 10 '18 at 23:53
27

"I too was looking for this, to no avail. So I made a new widget, feel free to use it: https://code.google.com/p/range-seek-bar/"

This example Stephan linked works great! However, the user has to press any of the thumbs to get a respons. I wanted that if the user pressed the bar, the closest thumb would move to that position and work like normal.

I implemented this in the example code like this:

private final Thumb getClosestThumb(float touchX)

{
    double xValue = screenToNormalized(touchX);        
    return (Math.abs(xValue - normalizedMinValue) < Math.abs(xValue - normalizedMaxValue)) ? Thumb.MIN : Thumb.MAX;
}

And in the "public boolean onTouchEvent(MotionEvent event)",

if(pressedThumb == null),
   pressedThumb = getClosestThumb(mDownMotionX);
Atte Backenhof
  • 468
  • 5
  • 7
3

A windowed seek bar with left and right icons.
Presettable minimum window size and automatic anti crossover.

Screenshot of windowed-seek-bar

Mercurial repository at https://bitbucket.org/nimbusgb/windowed-seek-bar

JJD
  • 50,076
  • 60
  • 203
  • 339
nimbusgb
  • 393
  • 1
  • 3
  • 12
2

I have used the anothem/android-range-seek-bar library

https://github.com/anothem/android-range-seek-bar

In xml

<org.florescu.android.rangeseekbar.RangeSeekBar
            android:id="@+id/pricebar_with_label"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            rsb:absoluteMinValue="20"
            rsb:absoluteMaxValue="150"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            />

enter image description here

Nilesh
  • 1,013
  • 14
  • 21
2

Incase you are using any of the material components such as: Snackbar, BottomNavigationView etc. you don't have to use another 3rd party library.

The material library also ships with RangeSlider which can be used to select a range of values.

Material library - implementation 'com.google.android.material:material:latestVersion'

Oush
  • 3,090
  • 23
  • 22