0

I want to add background images to my seekbar through java code not through xml. I want my seekbar to look like below image.

http://imageshack.us/a/img32/3834/sliderbar.png

My seekbar is also created through java code only not using any xml file.

I dont want to refer any xml file to do this, everything should be done through code.!

Can somebody help to do this. Thanks in advance.

user1525410
  • 83
  • 3
  • 9

2 Answers2

0

Try using setThumb,setProgressDrawable or setBackground methods of your SeekBar! If you really want a cool customized seekbar like this, I suggest you try referring xml files. It would make your life easier.

I've wrote a simple example that will give you a start point.

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);//the layout that has your seekbar

        SeekBar mSeekBar = (SeekBar)findViewById(R.id.seek_bar);//your seek bar

        mSeekBar.setThumb(R.drawable.thumb_image);//your thumb image
        mSeekBar.setProgressDrawable(R.drawable.progress_image);//your progress image
        mSeekBar.setBackground(R.drawable.background_image);//your background image
    }
}
Thiago M Rocha
  • 1,416
  • 1
  • 20
  • 26
  • I tried refering to xml files but was always getting ResourceNotFoundException, i could not get rid of this even after cleaning and rebuilding my project hundreds of times.There were no errors in my xml files. Basically i want to avoid hardcoding through xml. – user1525410 Oct 17 '12 at 12:22
  • I want seekbar to look exactly like in the image above, this code doesnt give me that. – user1525410 Oct 17 '12 at 12:29
  • The example is just the IDEA of the solution according to your question. Perhaps you could change the parameters according to the xml files of your project... – Thiago M Rocha Oct 17 '12 at 12:39
  • Is there any way to do progrmmatically what we generally do through xml files in this case? I mean defining layerlist drawables by java code. – user1525410 Oct 17 '12 at 12:44
  • Of course! You can instantiate your `View` and set the parameters, one by one, in the same way like the example above. – Thiago M Rocha Oct 17 '12 at 12:48
  • Above code resulted in this : http://imageshack.us/a/img202/8252/sliderib.png only thumb looks fine, i think backgroundDrawable is overwritten by progressDrawable. green image is filled entire seekbar. – user1525410 Oct 17 '12 at 13:05
  • Check out this link. It might helps you http://stackoverflow.com/questions/7056803/making-a-custom-skinny-progressbar-seekbar – Thiago M Rocha Oct 18 '12 at 11:21
0

Try this for seekbar clip

Drawable thumb = ContextCompat.getDrawable(getActivity(), R.mipmap.cir_32);
mSeekBar.setThumb(thumb);
HebeleHododo
  • 3,620
  • 1
  • 29
  • 38
Ashwin H
  • 695
  • 7
  • 24