Marcus
Marcus

Reputation: 6717

Android change SeekBar progressDrawable and thumb at runtime

I'm currently using a customized SeekBar from holo-colors.com. This is working as intended. However, I'd like for the user to be able to change the color theme of the app, including the color of the SeekBar. So I've downloaded five different versions with different colors. The question is, how do I change the style of the seek bar at runtime, i.e. when the user changes color theme (preference), how do I apply these changes?

SeekBar from my activity layout xml file:

<SeekBar
    android:id="@+id/sbPlaySongSeekBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:progressDrawable="@xml/apptheme_scrubber_progress_horizontal_holo_dark"
    android:thumb="@xml/apptheme_scrubber_control_selector_holo_dark" 
/>

apptheme_scrubber_progress_horizontal_holo_dark.xml:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="false" android:drawable="@drawable/apptheme_scrubber_control_disabled_holo" />
    <item android:state_pressed="true" android:drawable="@drawable/apptheme_scrubber_control_pressed_holo" />
    <item android:state_selected="true" android:drawable="@drawable/apptheme_scrubber_control_focused_holo" />
    <item android:drawable="@drawable/apptheme_scrubber_control_normal_holo" />
</selector>

apptheme_scrubber_control_selector_holo_dark.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@android:id/background"
            android:drawable="@drawable/apptheme_scrubber_track_holo_dark" />
    <item android:id="@android:id/secondaryProgress">
        <scale android:scaleWidth="100%"
                android:drawable="@drawable/apptheme_scrubber_secondary_holo" />
    </item>
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%"
                android:drawable="@drawable/apptheme_scrubber_primary_holo" />
    </item>
</layer-list>

Above is my current setup, which gives me a green SeekBar. So, I have four other versions of apptheme_scrubber_progress_horizontal_holo_dark.xml and apptheme_scrubber_control_selector_holo_dark.xml.

I was thinking that it might be possible to change the progressDrawable and thumb xml attributes of my SeekBar, but I do not know how to do this at runtime.

Is it even possible? Is there a better way of achieving this?

Thankful for any help.

Upvotes: 2

Views: 1209

Answers (1)

vilpe89
vilpe89

Reputation: 4702

These methods will do the job: setThumb(Drawable thumb) and setProgressDrawable(Drawable d).

SeekBar seekBar = (SeekBar) findViewById(R.id.sbPlaySongSeekBar);
seekBar.setThumb(getResources().getDrawable(R.drawable.thumb_drawable_1));
seekBar.setProgressDrawable(getResources().getDrawable(R.drawable.progress_drawable_1));

Upvotes: 2

Related Questions