Seekbar - Showing Progress With Textview Above
I'm trying the (surely simple?) task of having a TextView follow the thumb on a progress bar and show the progress in the TextView. The problem is that for progress values of less
Solution 1:
This works for me:
privatevoidsay_minutes_left(int how_many)
{
Stringwhat_to_say= String.valueOf(how_many);
fade_text.setText(what_to_say);
intseek_label_pos= (((fade_seek.getRight() - fade_seek.getLeft()) * fade_seek.getProgress()) / fade_seek.getMax()) + fade_seek.getLeft();
if (how_many <=9)
{
fade_text.setX(seek_label_pos - 6);
}
else
{
fade_text.setX(seek_label_pos - 11);
}
}
Thanks to Pushpendra Kuntal & flightplanner @ unable to get right position of texBox in Thumb of seekbar
Solution 2:
The following solution works for API >= 15:
Override SeekBar to create the getThumb() method that is not available on api 15:
publicclassCustomSeekBarextendsSeekBar { private Drawable thumb; publicCustomSeekBar(Context context) { super(context); } publicCustomSeekBar(Context context, AttributeSet attrs) { super(context, attrs); } publicCustomSeekBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @OverridepublicvoidsetThumb(Drawable thumb) { super.setThumb(thumb); this.thumb = thumb; } @Overridepublic Drawable getThumb() { return thumb; } }
And use it Like this:
correctX = customSeekBar.getThumb().getBounds().left;
Solution 3:
//Get the thumb bound andget its leftvalueint x = seekBar.getThumb().getBounds().left;
//set the leftvalueto textview x value
textView.setX(x);
use this to move text view according to progress
Solution 4:
Works good with Ken Nichols code, but here is a better decision, for example, to center you TextView to current position in ProgressBar(SeekBar):
privatevoidsay_minutes_left(int how_many)
{
Stringwhat_to_say= String.valueOf(how_many);
fade_text.setText(what_to_say);
intseek_label_pos= (((fade_seek.getRight() - fade_seek.getLeft()) * fade_seek.getProgress()) / fade_seek.getMax()) + fade_seek.getLeft();
fade_text.setX(seek_label_pos - fade_text.getWidth() / 2);
}
If you have padding in your ProgressBar(SeekBar), you can use more general version of this code:
privatevoidsay_minutes_left(int how_many)
{
Stringwhat_to_say= String.valueOf(how_many);
fade_text.setText(what_to_say);
intleft= fade_seek.getLeft() + fade_seek.getPaddingLeft();
intright= fade_seek.getRight() - fade_seek.getPaddingRight();
intseek_label_pos= (((right - left * fade_seek.getProgress()) / fade_seek.getMax()) + left;
fade_text.setX(seek_label_pos - fade_text.getWidth() / 2);
}
Solution 5:
Use this , we are calculating the x position based on progress and setting it to textview
seekBar.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
overridefunonProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
var xPosition= (((seekBar1.right - seekBar1.left) / seekBar1.max) * seekBar1.progress ) + seekBar1.left
textView1.translationX = xPosition.toFloat() - (textView1.width/2)
}
overridefunonStartTrackingTouch(seekBar: SeekBar?) {
}
overridefunonStopTrackingTouch(seekBar: SeekBar?) {
}
})
Post a Comment for "Seekbar - Showing Progress With Textview Above"