Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Seekbar thumb position in pixel

How can I get current position of thumb in pixel for SeekBar?

Currently I am trying to get the position on the bases of calculation.

Screen width, Seek Bar width, current seek progress and all. But I am not able to get exact position out of it.

Do any one have any idea?


Coding for same is as below.

I want to drow one component on top of SeekBar thumb.

translation = calculateTranslation(this.deviceScreenWidth, seekBarWidth, seekBar1T.getMax(), Integer.valueOf(ConstantData.seekbar_fifth.toString()), topComponentWidth, 0);
        if (currentapiVersion < android.os.Build.VERSION_CODES.HONEYCOMB){
            TranslateAnimation anim = new TranslateAnimation(translation - 1,
                    translation, 0, 0);
            anim.setFillAfter(true);
            anim.setDuration(0);
            edtTextRodUpDown.startAnimation(anim);  
        }else{
            edtTextRodUpDown.setTranslationX(translation);
        }

and calculate Translation like

    private float calculateTranslation(int screenWidth, int seekBarWidth, int seekMaxProgress, int currentProgress, int componentWidth, int extraDifference){
    double calculatedPosition = 0f;
    calculatedPosition = (double)((double)(seekBarWidth-((screenWidth-seekBarWidth))) / (double)seekMaxProgress) * (double)currentProgress;
    calculatedPosition = calculatedPosition - (double)(componentWidth/2);
    calculatedPosition = calculatedPosition + extraDifference;
    Double d = new Double(calculatedPosition);
    if(d < 0){
        return 0.0f;
    }else if(d + componentWidth > screenWidth){
        return screenWidth-componentWidth;
    }else{
        return d.floatValue();
    }
}
like image 386
Ketan Avatar asked Dec 10 '13 11:12

Ketan


People also ask

How do I change the SeekBar thumb on Android?

Just add android:thumbTint="@color/yourColor" in your seekbar.

What is SeekBar thumb?

android.widget.SeekBar. A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left or right to set the current progress level or use the arrow keys. Placing focusable widgets to the left or right of a SeekBar is discouraged.

How do I change my SeekBar?

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.

How do I start SeekBar on Android?

Step1: Create a new project. After that, you will have java and XML file. Step2: Open your xml file and add a SeekBar and TextView for message as shown below, max attribute in SeekBar define the maximum it can take. Assign ID to SeekBar And TextView.


2 Answers

I did this:

int width = seekBar.getWidth()
            - seekBar.getPaddingLeft()
            - seekBar.getPaddingRight();
int thumbPos = seekBar.getPaddingLeft()
            + width
            * seekBar.getProgress()
            / seekBar.getMax();
like image 191
chw Avatar answered Oct 05 '22 10:10

chw


You can use the getBounds() to get this info.

This sample code below is for instance aligning a TextView with the seekbar thumb position.

TextView sliderIndicator = ...
Rect bounds = seekBar.getThumb().getBounds();
sliderIndicator.setTranslationX(seekBar.getLeft() + bounds.left);
like image 35
Alécio Carvalho Avatar answered Oct 05 '22 09:10

Alécio Carvalho