Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting thumb position in SeekBar prior to API version 16

Basically, I need to detect when the progress changes in the SeekBar and draw a text view on top of the thumb indicating the progress value.

I do this by implementing a OnSeekBarChangeListener and on the public void onProgressChanged(SeekBar seekBar, int progress, boolean b) method, I call Rect thumbRect = seekBar.getThumb().getBounds(); to determine where the thumb is positioned.

This works perfectly fine, but apparently getThumb() is only available in API level 16+ (Android 4.1), causing a NoSuchMethodError on earlier versions.

Any idea how to work around this issue?

like image 710
Henrique Avatar asked Sep 04 '13 19:09

Henrique


People also ask

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 thumb SeekBar?

Android supports it via xml. Just add android:thumbTint="@color/yourColor" in your seekbar.

How do I SeekBar progress?

You are looking for the method getProgress() of the ProgressBar class as SeekBar is a subclass of ProgressBar . So basically it would be something like that. int value = seekBar. getProgress();


1 Answers

I was able to use my own class to get the Thumb:

MySeekBar.java

package mobi.sherif.seekbarthumbposition;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.SeekBar;

public class MySeekBar extends SeekBar {

    public MySeekBar(Context context) {
        super(context);
    }
    public MySeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MySeekBar(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    Drawable mThumb;
    @Override
    public void setThumb(Drawable thumb) {
        super.setThumb(thumb);
        mThumb = thumb;
    }
    public Drawable getSeekBarThumb() {
        return mThumb;
    }

}

In the activity this works perfectly:

package mobi.sherif.seekbarthumbposition;

import android.app.Activity;
import android.graphics.Rect;
import android.os.Bundle;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class MainActivity extends Activity implements OnSeekBarChangeListener {
    MySeekBar mSeekBar;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mSeekBar = (MySeekBar) findViewById(R.id.seekbar);
        mSeekBar.setOnSeekBarChangeListener(this);
    }
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
        Rect thumbRect = mSeekBar.getSeekBarThumb().getBounds();
        Log.v("sherif", "(" + thumbRect.left + ", " + thumbRect.top + ", " + thumbRect.right + ", " + thumbRect.bottom + ")");
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
        // TODO Auto-generated method stub

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

    }
}
like image 60
Sherif elKhatib Avatar answered Oct 01 '22 14:10

Sherif elKhatib