Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing size of seekbar thumb

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.

like image 457
penguru Avatar asked Mar 14 '12 10:03

penguru


People also ask

How do you change the size of your thumb in SeekBar?

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.

How do I change the width of SeekBar?

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.

How can I increase my SeekBar size?

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.

How do I change my 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.


2 Answers

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" /> 

Seek bar with different thumb size

like image 161
andrew Avatar answered Sep 18 '22 11:09

andrew


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!

like image 21
Pooks Avatar answered Sep 17 '22 11:09

Pooks