Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8680735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:19:29+00:00 2026-06-12T21:19:29+00:00

I’ve used this link to implement SeekBar with two thumbs and I got the

  • 0

I’ve used this link to implement SeekBar with two thumbs and I got the output.

But this seekbar has one issue that it allows crossing of thumb 1 and thumb 2 i.e. it allows thumb 1 value to be greater than thumb 2 value which should not be allowed as per my requirement.

What modification in following code should be performed to achieve this?

public class SeekBarWithTwoThumb extends ImageView {

    private String TAG = this.getClass().getSimpleName();
    private Bitmap thumb = BitmapFactory.decodeResource(getResources(),
            R.drawable.leftthumb);
    private int thumb1X, thumb2X;
    private int thumb1Value, thumb2Value;
    private int thumbY;
    private Paint paint = new Paint();
    private int selectedThumb;
    private int thumbHalfWidth;
    private SeekBarChangeListener scl;


    public SeekBarWithTwoThumb(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public SeekBarWithTwoThumb(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SeekBarWithTwoThumb(Context context) {
        super(context);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getHeight() > 0)
            init();
    }

    private void init() {
        printLog("View Height =" + getHeight() + "\t\t Thumb Height :"
                + thumb.getHeight());
        if (thumb.getHeight() > getHeight())
            getLayoutParams().height = thumb.getHeight();

        thumbY = (getHeight() / 2) - (thumb.getHeight() / 2);
        printLog("View Height =" + getHeight() + "\t\t Thumb Height :"
                + thumb.getHeight() + "\t\t" + thumbY);

        thumbHalfWidth = thumb.getWidth()/2;
        thumb1X = thumbHalfWidth;
        thumb2X = getWidth()/2 ;
        invalidate();
    }
    public void setSeekBarChangeListener(SeekBarChangeListener scl){
        this.scl = scl;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(thumb, thumb1X - thumbHalfWidth, thumbY,
                paint);
        canvas.drawBitmap(thumb, thumb2X - thumbHalfWidth, thumbY,
                paint);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int mx = (int) event.getX();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if (mx >= thumb1X - thumbHalfWidth
                    && mx <= thumb1X + thumbHalfWidth) {
                selectedThumb = 1;
                printLog("Select Thumb 1");
            } else if (mx >= thumb2X - thumbHalfWidth
                    && mx <= thumb2X + thumbHalfWidth) {
                selectedThumb = 2;
                printLog("Select Thumb 2");
            }
            break;
        case MotionEvent.ACTION_MOVE:
            printLog("Mouse Move : " + selectedThumb);

            if (selectedThumb == 1) {
                thumb1X = mx;
                printLog("Move Thumb 1");
            } else if (selectedThumb == 2) {
                thumb2X = mx;
                printLog("Move Thumb 2");
            }
            break;
        case MotionEvent.ACTION_UP:
            selectedThumb = 0;
            break;
        }

        if(thumb1X < 0)
            thumb1X = 0;

        if(thumb2X < 0)
            thumb2X = 0;

        if(thumb1X > getWidth() )
            thumb1X =getWidth() ;

        if(thumb2X > getWidth() )
            thumb2X =getWidth() ;

        invalidate();
        if(scl !=null){
            calculateThumbValue();
            scl.SeekBarValueChanged(thumb1Value,thumb2Value);
        }
        return true;
    }

    private void calculateThumbValue(){
        thumb1Value = (100*(thumb1X))/(getWidth());
        thumb2Value = (100*(thumb2X))/(getWidth());
    }
    private void printLog(String log){
        Log.i(TAG, log);
    }

    interface SeekBarChangeListener{
        void SeekBarValueChanged(int Thumb1Value,int Thumb2Value);
    }


}

Any help appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:19:31+00:00Added an answer on June 12, 2026 at 9:19 pm

    At the end of OnTouch event, replace your code with below code. Hope it helps

        calculateThumbValue();
    
        if(thumb1Value < thumb2Value) {
            invalidate();
            if(scl != null){
                scl.SeekBarValueChanged(thumb1Value, thumb2Value);
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a French site that I want to parse, but am running into
This could be a duplicate question, but I have no idea what search terms
I know there's a lot of other questions out there that deal with this
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.