Change icon and return to it on an Android onClick

1

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 takes effect that it is pressed and returns to its normal state.

I have already prepared the icon without pressing and pressing.

In the event onclick of the image, if I program it like that, there is no problem:

ImageView imagen=(ImageView)findViewById(R.id.imgcorreo);


    imagen.setImageResource(R.drawable.correo2);

After that, I want you to go back to the previous image, which is this:

R.drawable.correo2

Any suggestions to make it all?

Thanks in advance.

    
asked by Sergio Cv 02.06.2016 в 12:36
source

3 answers

1

Well, by testing the code I noticed that the ACTION_UP was not being called and I found the solution here link , with dellolver true it would be solved:

imagen.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (event.getAction() == MotionEvent.ACTION_DOWN){
                    imagen.setImageResource(R.drawable.correo2);
                }else if(event.getAction() == MotionEvent.ACTION_UP){
                    imagen.setImageResource(R.drawable.imagen_actual);
                }

                return true;
            }
        });

I tried it and it works perfectly, best regards.

    
answered by 02.06.2016 / 12:48
source
1

You can use a selector:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
   <item android:drawable="@drawable/imagen_normal"/>
   <item android:drawable="@drawable/imagen_pulsada" android:state_pressed="true"/>
   <item android:drawable="@drawable/imagen_pulsada" android:state_hovered="true"/>
</selector>

Save this file in your drawable folder and assign it to your view.

Greetings.

    
answered by 02.06.2016 в 12:43
1

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.

    
answered by 02.06.2016 в 13:30