I have created a button that fades in when it is being touched and fades out when it isn't. I was able to achieve this by using setAlpha in java. The code and the problem is shown below:
buttonPressed.getBackground().setAlpha(0);
buttonPressed.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
buttonPressed.getBackground().setAlpha(255);
buttonPressed.startAnimation(animationFadeIn);
return true;
} else if (event.getAction() == MotionEvent.ACTION_UP) {
buttonPressed.startAnimation(animationFadeOut);
return true;
}
return false;
}
});
The issue I have is whenever I release the button, the alpha is set to 0 before animationFadeOut
is able to fully play, so the button does not fade back.
If I remove the line:
buttonPressed.getBackground().setAlpha(0);
then animationFadeOut
will play but it will immediately go back to setAlpha(255)
.
How can I get animationFadeOut
to play fully and have the button alpha be 0
when the user stops touching the button?