How to mark a range in Android ProgressBar/SeekBar

There’s no straight forward way to mark a range in Android ProgressBar or SeekBar. They only have the setProgress method. But I needed to fill the ProgressBar from 35 to 70. I managed to do it using ClipDrawables.

In the xml file,

<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dip"
android:max="100" />

In the code,

int left = 35;
int right = 70;

ProgressBar progressBar = (ProgressBar)findViewById(R.id.progressBar1);
Drawable drawable = progressBar.getProgressDrawable();
ClipDrawable clipDrawable = new ClipDrawable(drawable, 
Gravity.LEFT, ClipDrawable.HORIZONTAL);
ClipDrawable clipDrawable2 = new ClipDrawable(clipDrawable, 
Gravity.RIGHT, ClipDrawable.HORIZONTAL);
progressBar.setProgressDrawable(clipDrawable2);
clipDrawable2.setLevel((100-left)*100);
clipDrawable.setLevel(right*100);