I'm using a drawable for seekbar thumb with
android:thumb="@drawable/thumb"
How can I set the size of this thumb in dip unit? Because I use a style for seekbar like
<style name="CustomSeekBar"> <item name="android:indeterminateOnly">false</item> <item name="android:minHeight">8dip</item> <item name="android:maxHeight">8dip</item> <item name="android:thumbOffset">8dip</item> </style>
and I want to have the thumb with 12dip
height and width.
Show activity on this post. Then I used vector code in . xml file, which has attrs android:width="28dp" and android:height="28dp" in the vector tag. Here we can change the size of the thumb.
You can use the Slider provided by the Material Components Library. Use the app:trackHeight="xxdp" (the default value is 4dp ) to change the height of the track bar.
Edit the maxHeight to your desired height what you want to achieve. It will work perfectly. Show activity on this post. You have to add some padding for all directions and also set the min and max height of the SeekBar.
Now open MainActivity. java class Declare objects of SeekBar and TextView, inside onCreate method initialize both objects using findViewById() method. Perform an event of SeekBar change listener that will hold progress value, and by using this event set the progress value inside TextView. seekBar.
The most flexible way to set thumb size for me is to create layered-drawable with shape as placeholder thumb_image.xml
:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape> <size android:height="40dp" android:width="40dp" /> <solid android:color="@android:color/transparent" /> </shape> </item> <item android:drawable="@drawable/scrubber_control_normal_holo"/> </layer-list>
So basically changing the size of shape will stretch drawable, the shape is transparent so it won't be visible. Also the minimum size of such thumb is size of bitmap.
Here is example of layout:
<SeekBar android:id="@+id/seekBar1" android:layout_width="match_parent" android:layout_height="wrap_content" android:thumb="@drawable/thumb_image" /> <SeekBar android:id="@+id/seekBar2" android:layout_width="match_parent" android:layout_height="wrap_content" />
I was also looking for a way to do something similar and after looking at some other questions and putting all the answers together, I came up with a way to resize the Seekbar thumb in onCreate().
Here's my code from onCreate()
, slightly adapted to your needs.
ViewTreeObserver vto = mySeekBar.getViewTreeObserver(); vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() { public boolean onPreDraw() { Resources res = getResources(); Drawable thumb = res.getDrawable(R.drawable.thumb); int h = mySeekBar.getMeasuredHeight() * 1.5; // 8 * 1.5 = 12 int w = h; Bitmap bmpOrg = ((BitmapDrawable)thumb).getBitmap(); Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOrg, w, h, true); Drawable newThumb = new BitmapDrawable(res, bmpScaled); newThumb.setBounds(0, 0, newThumb.getIntrinsicWidth(), newThumb.getIntrinsicHeight()); mySeekBar.setThumb(newThumb); mySeekBar.getViewTreeObserver().removeOnPreDrawListener(this); return true; } });
Note that this works for resizing your thumb, but it might be clipped because it's bigger than the containing seekbar (not sure about that). If it does get clipped, you might need to create a taller seekbar and create your own seekbar background matching the size you want for the display.
Hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With