Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting thumb to a particular position(not the offset) of seekbar programmatically?

I have a customized seekbar in which am updating the progress and maximum amplitude of the sound. So the seekbar varies according to sound detection and thumb shows the maximum amblitude of sound. I want to set the position of the thumb to a particular position(Max Amblitude) like we set the progress in seekbar. I had gone through loads of questions and answers and also I ran through the Developer docs. In developer docs its mentioned that only offsets can be given which maintains the thumb off the track of progress.

From Developer Doc setThumbOffset(int thumbOffset)-- Sets the thumb offset that allows the thumb to extend out of the range of the track.

Is there any possible way to set the thumb to a particular position. Sample is shown below. In this I had set the thumbs offset for just showing a sample, instead of this I want to set thumb to an accurate position.

enter image description here

like image 667
Sreedev Avatar asked May 15 '13 05:05

Sreedev


3 Answers

I think you could set position of thumb using setProgress(int progress), for in SeekBar, the thumb will be shown at the end of the progress meter.
If you want set the thumb's position independent to the progress, then you could implement your own SeekBar extends AbsSeekBar, you could refer to src of SeekBar.

like image 138
Bolton Avatar answered Nov 15 '22 23:11

Bolton


Make a custom progress bar with a transparent progress color.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@android:id/background"
        android:drawable="@drawable/my_background"/>
    <item android:id="@android:id/progress">
        <clip android:drawable="@android:color/transparent" />
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip android:drawable="@drawable/my_visual_progress" />
    </item>

</layer-list> 

Use setSecondaryProgress(int) with setting the visual "progress" of the progress bar.

And use setProgress(int) to move the thumb accordingly.

like image 25
EricRobertBrewer Avatar answered Nov 15 '22 21:11

EricRobertBrewer


I don't understand what you really mean but as I can understand from what you say do what I did Try using different drawbles Let me show you an example:

Your code .java

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.SeekBar;

public class MainActivity extends Activity {
    private SeekBar seekBar1;

    private SeekBar seekBar2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
         seekBar2 = (SeekBar) findViewById(R.id.seekBar2);
         seekBar1.setProgress(15);
         seekBar2.setProgress(55);
         Drawable ii = getResources().getDrawable(R.drawable.ii);
        // Drawable iii = getResources().getDrawable(R.drawable.ii);
         seekBar1.setThumb(ii);
         seekBar2.setThumb(ii);

    }



}

Your problem:

enter image description here

What you can do is simple just rename the same drawable like this:

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.Drawable;
import android.widget.SeekBar;

public class MainActivity extends Activity {
    private SeekBar seekBar1;

    private SeekBar seekBar2;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
         seekBar2 = (SeekBar) findViewById(R.id.seekBar2);
         seekBar1.setProgress(15);
         seekBar2.setProgress(55);
         Drawable ii = getResources().getDrawable(R.drawable.ii);
         Drawable iii = getResources().getDrawable(R.drawable.ii);
         seekBar1.setThumb(ii);
         seekBar2.setThumb(iii);

    }
}

And that's the result:

enter image description here

like image 20
noobProgrammer Avatar answered Nov 15 '22 21:11

noobProgrammer