2

I have a SeekBar defined in my application as shown below:

 <SeekBar
    android:id="@+id/seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

I want to place an image beside it that is the same height of the SeekBar. I need to set the height of the image programmatically so that it remains constant across all devices. However I am unable to get the height of the SeekBar.

The code below returns the height of the view and not the SeekBar.

 mReadingSeekBar = (ReadingSeekBar) findViewById(R.id.seekbar);
 mBarHeight = mReadingSeekBar.getHeight();

And this code doesn't work either. It just returns 0:

mBarHeight = mReadingSeekBar.getProgressDrawable().getMinimumHeight();

Is there a way of finding the actual size of the SeekBar and not just the view size?

DMC
  • 1,184
  • 5
  • 21
  • 50
  • This may (or may not) be relevant, but getting the height and width of stuff in onCreate() can be problematic, see http://stackoverflow.com/a/8171004/2728623 – pfairbairn Sep 11 '13 at 11:57
  • No Im using public void onWindowFocusChanged(boolean hasFocus) to get the width from onCreate() – DMC Sep 11 '13 at 12:03
  • Odd...I tried it with a normal seekBar and got 16 for getMinimumHeight(). I then set the drawable to the ic_launcher, and getMinimumHeight() returned 64 - this was actually in onCreate(). Have you actually tried it in onCreate()? – pfairbairn Sep 11 '13 at 12:13

3 Answers3

5

I figured out a better way of doing this. I set the height of the ProgressDrawable in my styles file so that it remained the same size across all devices. Below is how I declared it in my styles xml file:

<style name="tallerBarStyle" parent="@android:style/Widget.SeekBar">
        <item name="android:indeterminateOnly">false</item>
        <item name="android:progressDrawable">@drawable/styled_progress</item>
        <item name="android:indeterminateDrawable">@drawable/styled_progress</item>
        <item name="android:minHeight">8dp</item>
        <item name="android:maxHeight">21dp</item>
    </style>

I then use it in my SeekBar as shown below:

<SeekBar
    android:id="@+id/seekbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    style="@style/tallerBarStyle" />
DMC
  • 1,184
  • 5
  • 21
  • 50
0

I know you solved this question with other way. But that is not the right answer for your question, I think.

By using this method,

seekBar.getProgressDrawable().getBounds()

it returns progress drawable's Rect {left, top, right, bottom}.

(bottom - top) gives me an progress drawable height, not the seekBar height.

chanil
  • 351
  • 1
  • 11
  • Note that this method gives you the height of the entire SeekBar, including the Thumb. If you want the height of the Seekbar alone, you must calculate it reducing the size of the Thumb. – Minoru Sep 13 '17 at 13:45
0

From diving into source code I found indeterminateDrawable to have the height of the (default) progress drawable

mBarHeight = mReadingSeekBar.getIndeterminateDrawable().getIntrinsicHeight();
Jure Sencar
  • 554
  • 4
  • 13