There are several options if you are going to determine a new icon by clicking on the ImageView you can use an OnTouchListener and when you click on the button, run the event ACTION_DOWN and you change the icon, when releasing the button the event is executed ACTION_UP and change to the initial icon:
ImageView imagen=(ImageView)findViewById(R.id.imgcorreo);
imagen.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN :
imagen.setImageResource(R.drawable.correo2);
break;
case MotionEvent.ACTION_UP :
imagen.setImageResource(R.drawable.imgcorreo);
break;
}
return true;
}
});
very important to define return true;
Another option is using a selector .
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/correo2" android:android:state_pressed ="true" />
<item android:drawable="@drawable/correo2" android:android:state_selected ="true" />
<item android:drawable="@drawable/imgcorreo" android:state_selected="false" />
</selector>
You define this .xml inside the res/drawable
folder 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
android:clickable="true"
example:
<ImageView
android:id="@+id/imgcorreo"
android:clickable="true"
...
...
...
android:src="@drawable/mi_selector"
</ImageView>
Both forms are valid to be able to do something like this, by clicking the button changes the icon and when you release it changes to its original image.