I\'m trying to make a SeekBar move more smoothly than just jumping straight to the position. I\'m doing this as I\'ve got a SeekBar with 3 options for a slider-type implementati
I found the following solution to make the seekbar move smoothly, yet still snap to a limited range of values. Assuming you have the following views in your layout:
You can use the following code in your activity (change the value of android:max in the above xml, and the smoothness factor in the code below according to your needs - higher values = smoother):
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
sliderListener sldListener = new sliderListener();
SeekBar sldProgress = (SeekBar) findViewById(R.id.sldProgress);
sldProgress.setOnSeekBarChangeListener(sldListener);
}
private class sliderListener implements OnSeekBarChangeListener {
private int smoothnessFactor = 10;
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
progress = Math.round(progress / smoothnessFactor);
TextView lblProgress = (TextView) findViewById(R.id.lblProgress);
lblProgress.setText(String.valueOf(progress));
}
public void onStartTrackingTouch(SeekBar seekBar) {
}
public void onStopTrackingTouch(SeekBar seekBar) {
seekBar.setProgress(Math.round((seekBar.getProgress() + (smoothnessFactor / 2)) / smoothnessFactor) * smoothnessFactor);
}
}
}