Android How to add intervals texts in a seekbar

Sanghita picture Sanghita · Dec 10, 2012 · Viewed 11.4k times · Source

I am using a seekbar, and I want to add certain marks in the seekbar with text to use it as a scale. The selected interval points should be highlighted. enter image description here

The corresponding code is here:

   seekbar = (SeekBar) findViewById(R.id.seekBar1);
    seekbar.setMax(max);
    seekbar.setProgress(50);
    seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            // TODO Auto-generated method stub
            value.setText("SeekBar value is " + progress);
        }

        public void onStartTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }

        public void onStopTrackingTouch(SeekBar seekBar) {
            // TODO Auto-generated method stub
        }
    });

Answer

cjds picture cjds · Dec 10, 2012

To make the above seek bar you first have to add a measuring scale as a background drawable. Then you'll have to make textboxes for each interval on the measuring scale.

<SeekBar 
        ....
android:progressDrawable="@drawable/progress"
android:thumb="@drawable/thumb"
        ....
/>

Then

public void onProgressChanged(SeekBar seekBar, int progress,boolean fromUser) {   
   progress = ((int)Math.round(progress/interval))*interval;
   seekBar.setProgress(progress);
   if(progress==5) textBox5.setTextColor(Color.RED);
   else if(progress==10) textBox10.setTextColor(Color.RED);
   //......... for all the text boxes.
}