I am trying to make an Android application with two seekbars that can be controlled at the same time (multi-touch). The full source code can be found here
The app is a robot controller, each seekbar controls a motor so to move forward you slide both bars up and so on (speed is controlled by how far you push it up or down) where 50 is the stop state, 100 is the maximum speed forward and 0 is maximum speed backward.
What I am trying to implement is that after you let go of the seekbar it will go back to the stop state (progress 50), however, even though the value is actually set to 50 (using setProgress
) but the view won't update to reflect that.
I tried using .invalidate()
, .requestLayout()
and some old solutions here and here but couldn't get it to work.
Here are parts of the code: (for full code check the github link above)
public class MainActivity extends ActionBarActivity {
...
...
public static void ResetSeekBar(View v) {
if ( v.getId() == R.id.sb1) {
Log.d("before", "Progress = " + Integer.toString(sb1.getProgress()));
sb1.setProgress(50);
Log.d("after", "Progress = " + Integer.toString(sb1.getProgress()));
sb1.invalidate();
//sb1.requestLayout();
} else if ( v.getId() == R.id.sb2) {
sb2.setProgress(50);
}
}
}
public class MySeekBar extends SeekBar {
public MySeekBar(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean onTouchEvent(final MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
Log.d("tag", "UP");
MainActivity.ResetSeekBar(this);
} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
Log.d("tag", Integer.toString(getProgress()));
MainActivity.UpdateText(this, Integer.toString(getProgress()));
} else if (event.getAction() == MotionEvent.ACTION_DOWN) {
Log.d("tag", "DOWN ");
MainActivity.UpdateText(this, Integer.toString(getProgress()));
}
return super.onTouchEvent(event);
}
}
Using a regular button onClick works and do update the view but not after MotionEvent up. So what am I doing wrong?
Also how can I make it reset to 50 but with a fast transition instead of suddenly jumping to the middle?