Search code examples
androidviewprogress-barseekbar

Android: setting maxHeight parameter programmatically for ProgressBar/SeekBar


One can create a SeekBar in XML as follows...

<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/bpseekbar"
      android:layout_width="fill_parent"
      android:layout_height="wrap_content"
      android:layout_marginTop="3dp"
      android:max="100"
      android:maxHeight="8dp"
      ...

Now I want to do this programmatically since I need to adjust maxHeight based on screen resolution (yes, I know one shouldn't do that, but I have my valid reasons for this). I have no problems with setting the parameters programmatically except that I can't find out how to set maxHeight. I feel like I need to use Attributes but I couldn't find exactly how. Any hints?


Solution

  • It's not directly possible. SeekBar (actually ProgressBar which SeekBar extends) gets maxHeight only from XML layout attributes, there's no corresponding method.

    Additionally, ProgressBar increases its maxHeight in call to setProgressDrawable to be at least height of the drawable. You may utilize this.

    Another possibility is to extend SeekBar and provide own onMeasure method. This is actually where maxHeight is used. You can then use your extended SeekBar in xml layout.