Button background change layout on pressed

1.1k Views Asked by At

i'm currently working with the camera api and want to implement a button, which starts and stops video recordings. the button has four different images: video_start_default, video_start_pressed, video_stop_default and video_stop_pressed. it should change its layout when pressed and after starting/stopping the video.

i created this xml file to do so.

<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
<item     
    android:state_selected="false" 
    android:state_pressed="false"   
    android:drawable="@drawable/video_start_default" /> 
<item    
    android:state_selected="false"   
    android:state_pressed="true" 
    android:drawable="@drawable/video_start_pressed" /> 
<item
    android:state_selected="true"     
    android:state_pressed="false"    
    android:drawable="@drawable/video_stop_default" /> 
<item
    android:state_selected="true"
    android:state_pressed="true"
    android:drawable="@drawable/video_stop_pressed" /> 
<item android:drawable="@drawable/video_default" /> 
</selector> 

in order to make the change between start and stop work, i set the select attribute in the onclicklistener

  if(videoButton.isSelected())
                videoButton.setSelected(false);
            else{
                videoButton.setSelected(true); 
            }

basically this works.

while pressing video_start_default, video_start_pressed is correctly shown but the problem is that after releasing video_start_pressed, video_stop_pressed is shown for a millisecond instead of video_stop_default. strangely the problem doesn't occure after clicking video_stop_pressed. here is video_start_default directly visible.

i hope the description is not to confusing

1

There are 1 best solutions below

0
On BEST ANSWER

I think you have to make two selectors one for the play state and another for the pause state:

I have a nice tutorial for you that can help you a lot in your project

you have to see this link.

enter image description here

Cheers