Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use setProgress to change a seek bar programatically

I am using an android seekBar as a camera zoom controller.

The seek bar works like a charm when dragging but I wanted to add a zoomIn and zoomOut button that would increment the progress. Problem is two fold.

  1. When I click the button I have to click it 4 times before the progress actually increments

  2. It is not updating the draggable thumb on the progress bar

    btn_zoomIn.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        try {
          zoomCtrl.setProgress(zoomCtrl.getProgress()+1);
          onProgressChanged(zoomCtrl, zoomCtrl.getProgress(), true);
        } catch(Exception ex) { }
      }
    });
    
    btn_zoomOut.setOnClickListener(new View.OnClickListener() {
      public void onClick(View view) {
        try {
          zoomCtrl.setProgress(zoomCtrl.getProgress()-1);
        } catch(Exception ex){ }
      }
    });
    

Can anyone help please?

ADDITION:

this is the verticalSeekBar class i am using instead of seekBar:

public class VerticalSeekBar extends SeekBar {

/**
 * Instantiates a new vertical seek bar.
 *
 * @param context the context
 */
public VerticalSeekBar(Context context) {

    super(context);
}

/**
 * Instantiates a new vertical seek bar.
 *
 * @param context the context
 * @param attrs the attrs
 * @param defStyle the def style
 */
public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {

    super(context, attrs, defStyle);
}

/**
 * Instantiates a new vertical seek bar.
 *
 * @param context the context
 * @param attrs the attrs
 */
public VerticalSeekBar(Context context, AttributeSet attrs) {

    super(context, attrs);
}

/* (non-Javadoc)
 * @see android.widget.AbsSeekBar#onSizeChanged(int, int, int, int)
 */
protected void onSizeChanged(int w, int h, int oldw, int oldh) {

    super.onSizeChanged(h, w, oldh, oldw);
}

/* (non-Javadoc)
 * @see android.widget.AbsSeekBar#onMeasure(int, int)
 */
@Override
protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(heightMeasureSpec, widthMeasureSpec);
    setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
}

/* (non-Javadoc)
 * @see android.widget.AbsSeekBar#onDraw(android.graphics.Canvas)
 */
protected void onDraw(Canvas c) {

    c.rotate(-90);
    c.translate(-getHeight(), 0);

    super.onDraw(c);
}

/* (non-Javadoc)
 * @see android.widget.AbsSeekBar#onTouchEvent(android.view.MotionEvent)
 */
@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;
}

}

and i am adding it to my activity like so:

zoomCtrl = new VerticalSeekBar(this);
        zoomCtrl.setLayoutParams(zCtrl);

        zoomCtrl.setProgressDrawable(getResources().getDrawable(R.drawable.zoombar_progress_bg));
        zoomCtrl.setThumb(getResources().getDrawable(R.drawable.imagebtn_zoom_thumb));
        zoomCtrl.setBackgroundDrawable(getResources().getDrawable(R.drawable.zoom_track));
        zoomCtrl.setPadding(25, 0, 25, 0);
        zoomCtrl.setOnSeekBarChangeListener(this);


        rel.addView(zoomCtrl);
like image 448
erik Avatar asked Dec 07 '12 13:12

erik


People also ask

How do I change the progress of a SeekBar?

In Order to the seekBar to Change while you are in the Activity you need to Call the method setProgress(), and pass it a boolean animate value.

How do I change the color of my SeekBar track?

If you are using default SeekBar provided by android Sdk then their is a simple way to change the color of that . just go to color. xml inside /res/values/colors. xml and change the colorAccent.

What is difference between ProgressBar and SeekBar?

What is difference between ProgressBar and SeekBar? An example of SeekBar is your device's brightness control and volume control. Important Note: Attribute of a SeekBar are same as ProgressBar and the only difference is user determine the progress by moving a slider (thumb) in SeekBar.

How can I make my own SeekBar?

Now open MainActivity. java class Declare objects of SeekBar and TextView, inside onCreate method initialize both objects using findViewById() method. Perform an event of SeekBar change listener that will hold progress value, and by using this event set the progress value inside TextView. seekBar.


2 Answers

Solved the issue by overriding setProgress in VerticalSeekbar class. @Override public synchronized void setProgress(int progress) { super.setProgress(progress); onSizeChanged(getWidth(), getHeight(), 0, 0); }

like image 117
Alfred Avatar answered Sep 28 '22 16:09

Alfred


Create updateThumb(); method in VerticalSeekbar

    public void updateThumb(){
         onSizeChanged(getWidth(), getHeight(), 0, 0);
    }

And then call update thumb method after setting progress.

seekBar.setProgress(100 - (int) progress);
seekBar.updateThumb();
like image 23
Swapnil Godambe Avatar answered Sep 28 '22 15:09

Swapnil Godambe