0

I had implemented a custom SeekBar and set custom thumb image to that. When ever progress changes, I am displaying it on the thumb. Initially, it is being displayed correctly. When I slide it forward, backward, the thumb size is getting reduced.

Here is the code for the same.

XML component

<SeekBar
                android:id="@+id/sb_timelimit"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_margin="5dp"
                android:max="60"
                android:maxHeight="6dp"
                android:minHeight="6dp"
                android:progress="10"
                android:progressDrawable="@drawable/timelimitprogress"
                android:thumb="@drawable/timecircle" />

Layer list

<?xml version="1.0" encoding="utf-8"?>
<item
    android:id="@android:id/background"
    android:drawable="@drawable/greyprocessbar"/>
<item android:id="@android:id/progress">
    <scale
        android:drawable="@drawable/orangeprocessbar"
        android:scaleWidth="100%" />
</item>

Seekbar listener

new OnSeekBarChangeListener() {
        
        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {
        }
        
        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {
        }
        
        @Override
        public void onProgressChanged(SeekBar seekBar, int progress,
                boolean fromUser) {
            int value = seekBar.getProgress(); //this is the value of the progress bar (1-100)
            //value = progress; //this should also work
            String valueString = value + " sec"; //this is the string that will be put above the slider
            seekBar.setThumb(writeOnDrawable(R.drawable.timecircle, valueString));
        }
    });

private BitmapDrawable writeOnDrawable(int drawableId, String text){

    Options options = new BitmapFactory.Options();
    options.inScaled = false;
    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId, options).copy(Bitmap.Config.ARGB_8888, true);

    if(paint == null){
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    }
    paint.setStyle(Style.FILL);  
    paint.setColor(getResources().getColor(R.color.grey_text)); //Change this if you want other color of text
    paint.setTextSize(30); //Change this if you want bigger/smaller font

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 10, bm.getHeight()/4, paint); //Change the position of the text here

    return new BitmapDrawable(bm);
}

Reference screenshots

Before seek change

enter image description here

After seek change

enter image description here

Any one has idea about how to resolve this issue? Thanks in advance.

Community
  • 1
  • 1
Kameswari
  • 738
  • 7
  • 27

1 Answers1

1

This issue is being resolved by changing the below line

return new BitmapDrawable(bm);

to

return new BitmapDrawable(getResources(), bm);
Kameswari
  • 738
  • 7
  • 27