1

I have been stuck on this problem since a long time. I left it then came back to it but so far I haven't found a working solution. Problem is simple, I have a vertical seekbar and I want to update it programmatically without touching it, but it doesn't happen. This is my vertical seekbar code

public class VerticalSeekBar extends SeekBar {

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

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

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

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (!isEnabled()) {
            return false;
        }

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_MOVE:
            case MotionEvent.ACTION_UP:
                setProgress(getMax() - (int) (getMax() * event.getY() / getHeight()));
                onSizeChanged(getWidth(), getHeight(), 0, 0);
                break;

            case MotionEvent.ACTION_CANCEL:
                break;
        }
        return true;
    }
}

This is how the seekbar is declared in the xml

<com.gnr.kumar.varun.songapp.views.VerticalSeekBar
                android:id="@+id/slider_1"
                android:layout_width="wrap_content"
                android:layout_height="200dp"
                android:max="100"
                android:padding="10dp"
                android:progress="50"
                android:progressBackgroundTint="#9cf2ec"
                android:progressTint="#9cf2ec"
                android:thumb="@drawable/thumb" />

I try to do things like

SeekBar sliders[] = new SeekBar[NUM_OF_SLIDERS];

sliders[0] = (SeekBar) findViewById(R.id.slider_1);

sliders[0].setProgress(100);

But it just changes the seekbar position to slightly more than 0 when in fact it should be at its maxima. No matter how big number I use instead of 100, it is the same. Can someone please help !! Thanks in advance !!!

varunkr
  • 5,364
  • 11
  • 50
  • 99
  • Try just rotating your canvas, I think that reversing the dimensions in your onSizeChanged/onMeasure calls may be the issue. – zgc7009 May 10 '16 at 19:54
  • @zgc7009 Thnx for the input, I solved it today finally. – varunkr May 10 '16 at 21:35
  • Awesome! If you could post your answer for others in the future it would be appreciated – zgc7009 May 10 '16 at 21:39
  • 1
    @zgc7009 yes dude, just posted it, got the answer from a comment on a question regarding vertical seekbar. Just stumbled upon it after some more googling. I thought it must be on stackoverflow, coz it is a very general thing. It was buried in the comments :D – varunkr May 10 '16 at 21:41

1 Answers1

1

I was finally able to solve my problem after a long time. All I had to do was to override this in Vertical Seekbar class. A comment on the accepted answer to this question helped me.

@Override public synchronized void setProgress(int progress){           
    super.setProgress(progress);
    onSizeChanged(getWidth(), getHeight(), 0, 0); 
}
Community
  • 1
  • 1
varunkr
  • 5,364
  • 11
  • 50
  • 99