I am developing an android app. One of the images is a button that redirects to a certain task. I would like that when I click, for a second, it makes the effect that it is pressed and returns to its normal state.
I have already prepared the icon without pressing and pressing.
In the onclick event of the image, if I program it like this there is no problem:
ImageView imagen=(ImageView)findViewById(R.id.imgcorreo);
imagen.setImageResource(R.drawable.correo2);
After that, I want it to return to the previous image, which is this:
R.drawable.correo2
Any suggestions to do all of this?
Thanks in advance.
You can use a selector:
Save this file in your drawable folder and assign it to your view.
All the best.
Well, testing the code, I noticed that the ACTION_UP was not being called and I found the solution here https://stackoverflow.com/questions/15799839/motionevent-action-up-not-called , with return true it would be solved:
I tried it and it works perfectly, greetings.
There are several options if you are going to determine a new icon when clicking on the ImageView you can use an OnTouchListener and when the button is clicked, execute the ACTION_DOWN event and change the icon, when releasing the button the ACTION_UP event is executed and you change the icon initial:
very important to define
return true;
Another option is using a selector .
you define this .xml inside the folder
res/drawable
and assign it as drawable in your ImageView.important to know that if you define the drawable directly in the layout of your ImageView you must add the property
example:
Both forms are valid to be able to do something like this, when clicking the button the icon changes and when releasing it changes to its original image.
I have a doubt. Instead of it being an ImageView. This code block can be applied:
to a floating button?