11

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

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sreedev
  • 6,563
  • 5
  • 43
  • 66

4 Answers4

2

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.

Bolton
  • 2,226
  • 3
  • 25
  • 29
  • Yes came across some post showing its possible with AbsSeekbar but can you plaese let me know about any sources of implemented using this. – Sreedev May 24 '13 at 09:23
  • @SreedevR You could take a look at the src of AbsSeekbar here: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/AbsSeekBar.java#AbsSeekBar to know about how the thumb is drawn and how the postion is being set. – Bolton May 24 '13 at 10:12
  • @Sreedev R exact position means as shown in image..??slightly far from actual process? – TheFlash May 24 '13 at 11:58
2

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.

Community
  • 1
  • 1
EricRobertBrewer
  • 1,750
  • 1
  • 22
  • 25
1

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

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
noobProgrammer
  • 1,443
  • 4
  • 14
  • 33
0

Use a LayerDrawable as a thumb drawable. Than change the drawable inside the layer and not the drawable of thumb.

<?xml version="1.0" encoding="utf-8"?>

<item  android:id="@+id/thumbImage"     android:drawable="@drawable/normalDrawable"/>

Than change the drawble throught code:

(thumb as LayerDrawable).setDrawableByLayerId(R.id.thumbImage, otherDrawable)