I am trying to create a seekbar with a thumb that changes color when it is pressed by the user and change back when the user lets go. I managed to change the thumb drawable using set thumb. However, I also had to set the drawable boundries in java to get it to appear after the change. Now when the user lets go of the slider and it reverts to its original drawable it resets the drawable to the zero position and leaves the progress where it should be. I have tried using a global variable to reset the seekbar progress after the drawable is changed, but it doesn't seems to work. If I use a static integer to reset the progress it works the way I want it to.
Kinda new to android and I don't know what I am doing wrong.
This is the code that changes the drawable when it is touched.
private void Seek_Height_Click() {
Drawable myThumb = getResources().getDrawable(R.drawable.slider_button_click);
myThumb.setBounds(new Rect(0, 0, myThumb.getIntrinsicWidth(),myThumb.getIntrinsicHeight()));
Height_of_Target_skbr.setThumb(myThumb);
Height_of_Target_skbr.setThumbOffset(-1);
}
This is the code to set the thumb back to the original drawable.
private void Seek_Height_UnClick() {
Drawable myThumb = getResources().getDrawable(R.drawable.slider_button);
myThumb.setBounds(new Rect(0, 0, myThumb.getIntrinsicWidth(),myThumb.getIntrinsicHeight()));
Height_of_Target_skbr.setThumb(myThumb);
Height_of_Target_skbr.setThumbOffset(-1);
Height_of_Target_skbr.setProgress(Height_Prog_int);
}
This is the code that calls the above method.
this.Height_of_Target_skbr.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
returnHeight_skbr();
Seek_Height_UnClick();
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
returnHeight_skbr();
Seek_Height_Click();
}
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
returnHeight_skbr();
}
});
I was able to make this work using by creating a style in the drawable folder and then setting the thumb drawable to that style.
I refereed to this tutorial to make this work. http://www.youtube.com/watch?v=zXXCFmfJMNw
Code for Thumb Style
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="@drawable/slider_button_click">
</item>
<item
android:drawable="@drawable/slider_button">
</item>
</selector>