89

I have imageview in my Android app that I am using like a button with the onClick event given, but as you might guess it is not giving imageview a clickable effect when clicked. How can I achieve that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Burak Dede
  • 3,725
  • 5
  • 40
  • 53

28 Answers28

79

You can do this with a single image using something like this:

     //get the image view
    ImageView imageView = (ImageView)findViewById(R.id.ImageView);

    //set the ontouch listener
    imageView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    ImageView view = (ImageView) v;
                    //overlay is black with transparency of 0x77 (119)
                    view.getDrawable().setColorFilter(0x77000000,PorterDuff.Mode.SRC_ATOP);
                    view.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL: {
                    ImageView view = (ImageView) v;
                    //clear the overlay
                    view.getDrawable().clearColorFilter();
                    view.invalidate();
                    break;
                }
            }

            return false;
        }
    });

I will probably be making this into a subclass of ImageView (or ImageButton as it is also a subclass of ImageView) for easier re-usability, but this should allow you to apply a "selected" look to an imageview.

Mr Zorn
  • 1,903
  • 1
  • 14
  • 25
54

You can design different images for clicked/not clicked states and set them in the onTouchListener as follows

final ImageView v = (ImageView) findViewById(R.id.button0);
        v.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                switch (arg1.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    v.setImageBitmap(res.getDrawable(R.drawable.img_down));
                    break;
                }
                case MotionEvent.ACTION_CANCEL:{
                    v.setImageBitmap(res.getDrawable(R.drawable.img_up));
                    break;
                }
                }
                return true;
            }
        });

The better choice is that you define a selector as follows

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"   
        android:drawable="@drawable/img_down" />
    <item android:state_selected="false"   
        android:drawable="@drawable/img_up" />
</selector>

and select the image in the event:

v.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View arg0, MotionEvent arg1) {
                v.setSelected(arg1.getAction()==MotionEvent.ACTION_DOWN);
                return true;
            }
        });
Martin Booka Weser
  • 3,192
  • 5
  • 28
  • 41
  • 1
    can you explain what res.getDrawable res what it does ? – VenushkaT Feb 17 '14 at 06:15
  • v.setSelected will only == true once, immediately moving to false due to MotionEvent.ACTION_MOVE. Once the finger is released, then MotionEvent.ACTION_UP occurs. If using the selector method, use separate logic to setSelected or setPressed. if (arg1.getAction() == MotionEvent.ACTION_DOWN) { v.setPressed(true); } else if (arg1.getAction() == MotionEvent.ACTION_UP) { v.setPressed(false); } – Adam Denoon Jan 20 '16 at 21:58
  • Better be `MotionEvent.ACTION_DOWN || MotionEvent.ACTION_MOVE` to keep the image showing as long as we're pressing. – Alaa M. Dec 07 '16 at 20:30
  • You don't need to `setOnTouchListener(...`. You can simply create the `` and then set the ImageView clickable in XML like `` – Pierre Mar 09 '20 at 14:16
45

EDIT: Although the original answer below works and is easy to set up, refer to this post by an Android Developer Advocate at Google if you want / need a more efficient implementation. Also note that the android:foreground attribute is coming to all Views, including ImageView, by default in Android M.


The problem with using a selector for an ImageView is that you can only set it as the view's background - as long as your image is opaque, you will not see the selector's effect behind it.

The trick is to wrap your ImageView in a FrameLayout with the attribute android:foreground which allows us to define an overlay for its content. If we set android:foregroundto a selector (e.g.?android:attr/selectableItemBackground for API level 11+) and attach the OnClickListener to the FrameLayout instead of the ImageView, the image will be overlaid with our selector's drawable - the click effect we desire!

Behold:

<FrameLayout
    android:id="@+id/imageButton"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:foreground="?android:attr/selectableItemBackground" >

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/yourImageFile" />

</FrameLayout>

(Note this should be placed within your parent layout.)

final View imageButton = findViewById(R.id.imageButton);
imageButton.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View view) {
        // do whatever we wish!
    }
});
justasm
  • 739
  • 8
  • 11
  • 2
    Great ! This is a very good solution. Same solution used in contacts application for SMS or Call actions. – Jerry Apr 26 '14 at 08:50
  • Wont work in API < 11. ?android:attr/selectableItemBackground requires API level 11 (current min is 8) – MSaudi Aug 10 '14 at 21:15
  • The `selectableItemBackground` attribute was only added in API level 11, so you must use another selector if you want to use this solution for older API levels. For example, for one of my applications that supports API level 7, I use the `@drawable/list_selector_holo_light` resource generated using the [Android Holo Colors Generator tool](http://android-holo-colors.com/). – justasm Aug 10 '14 at 21:24
  • You can achieve the same behavior using only 1 `` with selectableItemBackground ! – Yohan Dahmani May 03 '17 at 14:14
30

Use style="?android:borderlessButtonStyle" in the XML file. It will show the Android default click effect.

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher" 
    style="?android:borderlessButtonStyle"
/>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Anjula
  • 1,690
  • 23
  • 29
21

Simply just use an ImageButton.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 3
    I personally cannot make it look like ImageView. No border, stretch the image to the size of ImageButton etc. If you can give this border and stretch issue, and update your post. Mine some reputation goes to you as bounty =) – totten Feb 05 '15 at 07:45
15

Here is my simple way to solve that:

ImageView iv = (ImageView) findViewById(R.id.imageView);

iv.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub
        //Agrega porcentajes de cada fraccion de grafica pastel

        Animation animFadein = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.fade_in);

        iv.startAnimation(animFadein);
    });

In file res/anim/fade_in.xml:

<?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
         android:fillAfter="true" >

<alpha
    android:duration="100"
    android:fromAlpha="0.0"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1.0" />
 </set>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • Great solution - thanks! I scrolled through this post trying all the concise ones - and no success. Finally I got here and it worked for me. One observation - if you have 2+ buttons you want to apply the animation toward .. for my code, I found I need to create a unique instance of Animation object for each button I wanted to apply the effect for. Reusing the same instance made all the buttons flicker when 1 was clicked. – Gene Bo Mar 24 '15 at 22:56
11

If you want ripple when tapped, it can be given by this code :

<ImageView
    ...
    android:background="?attr/selectableItemBackgroundBorderless"
    android:clickable="true"
    ...
</ImageView>

Similarly, you can implement click effect for TextView

<TextView
    ...
    android:background="?attr/selectableItemBackgroundBorderless"
    android:clickable="true"
    ...
</TextView>
npk
  • 1,512
  • 1
  • 13
  • 26
9

Set the selectable background to the ImageView and add some padding. Then attach the OnClickListener.

<ImageView
    android:id="@+id/your_image_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/your_image"
    android:padding="10dp"
    android:background="?android:attr/selectableItemBackground"/>
Oliver Kranz
  • 3,741
  • 2
  • 26
  • 31
8

For defining the selector drawable choice

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="true"   
        android:drawable="@drawable/img_down" />
    <item android:state_selected="false"   
        android:drawable="@drawable/img_up" />
</selector>

I have to use android:state_pressed instead of android:state_selected

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed ="true"   
        android:drawable="@drawable/img_down" />
    <item android:state_pressed ="false"   
        android:drawable="@drawable/img_up" />
</selector>
worawee.s
  • 598
  • 5
  • 11
5

This worked for me:

img.setOnTouchListener(new OnTouchListener(){

            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction())
                {
                    case MotionEvent.ACTION_DOWN:
                    {
                        ((ImageView)v).setImageAlpha(200);
                        break;
                    }
                    case MotionEvent.ACTION_MOVE:
                    {
                        // if inside bounds
                        if(event.getX() > 0 && event.getX() < v.getWidth() && event.getY() > 0 && event.getY() < v.getHeight())
                        {
                            ((ImageView)v).setImageAlpha(200);
                        }
                        else
                        {
                            ((ImageView)v).setImageAlpha(255);
                        }

                        break;
                    }
                    case MotionEvent.ACTION_UP:
                    {
                        ((ImageView)v).setImageAlpha(255);
                    }
                }
                return true;
            }

        });

@Edit: As Gunhan said there will be backward compatibility problem with setImageAlpha method. I used this method:

public static void setImageAlpha(ImageView img, int alpha)
    {
        if(Build.VERSION.SDK_INT > 15)
        {
            img.setImageAlpha(alpha);
        }
        else
        {
            img.setAlpha(alpha);
        }
    }
Altynbek Usenbekov
  • 688
  • 1
  • 8
  • 14
  • 1
    setImageAlpha requires API level 16. Therefore, for backward compatible apps its not possible to use it – Gunhan Dec 04 '12 at 17:19
  • 1
    @Gunhan actually, you can use "nineOldAndroids" library which allows using alpha even on older APIs. just use: ViewHelper.setAlpha(view,alpha); – android developer Mar 17 '14 at 12:58
5

You could try with android:background="@android:drawable/list_selector_background" to get the same effect as the "Add alarm" in the default "Alarm Clock" (now Desk Clock).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Marco Bettiol
  • 531
  • 3
  • 12
4

I do some similar things See suitable for you or not

View Press Effect Helper:

  • usage : do some simple press effect like iOS

    Simple Usage:

  • ImageView img = (ImageView) findViewById(R.id.img);

  • ViewPressEffectHelper.attach(img)

https://gist.github.com/extralam/7489370

Ah Lam
  • 167
  • 1
  • 2
  • 8
4

In combination with all the answers above, I wanted the ImageView to be pressed and changed state but if the user moved then "cancel" and not perform an onClickListener.

I ended up making a Point object within the class and setting its coordinates according to when the user pushed down on the ImageView. On the MotionEvent.ACTION_UP I recording a new point and compared the points.

I can only explain it so well, but here is what I did.

// set the ontouch listener
weatherView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        // Determine what action with a switch statement
        switch (event.getAction()) {

        // User presses down on the ImageView, record the original point
        // and set the color filter
        case MotionEvent.ACTION_DOWN: {
            ImageView view = (ImageView) v;

            // overlay is black with transparency of 0x77 (119)
            view.getDrawable().setColorFilter(0x77000000,
                    PorterDuff.Mode.SRC_ATOP);
            view.invalidate();

            p = new Point((int) event.getX(), (int) event.getY());
            break;
        }

        // Once the user releases, record new point then compare the
        // difference, if within a certain range perform onCLick
        // and or otherwise clear the color filter
        case MotionEvent.ACTION_UP: {
            ImageView view = (ImageView) v;
            Point f = new Point((int) event.getX(), (int) event.getY());
            if ((Math.abs(f.x - p.x) < 15)
                    && ((Math.abs(f.x - p.x) < 15))) {
                view.performClick();
            }
            // clear the overlay
            view.getDrawable().clearColorFilter();
            view.invalidate();
            break;
        }
        }
        return true;
    }
});

I have an onClickListener set on the imageView, but this can be an method.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120
  • by adding case `MotionEvent.ACTION_CANCEL` with the same functionality as `MotionEvent.ACTION_UP` then it is possible to "clear" the view if the users performs a "drag" which is not a click action. – madlymad May 12 '14 at 13:24
4

You can Override setPressed in the ImageView and do the color filtering there, instead of creating onTouchEvent listeners:

@Override
public void setPressed(boolean pressed) {
    super.setPressed(pressed);

    if(getDrawable() == null)
        return;

    if(pressed) {
        getDrawable().setColorFilter(0x44000000, PorterDuff.Mode.SRC_ATOP);
        invalidate();
    }
    else {
        getDrawable().clearColorFilter();
        invalidate();
    }
}
ono
  • 2,984
  • 9
  • 43
  • 85
4

Based on Mr Zorn's answer, I use a static method in my abstract Utility class:

public abstract class Utility {
...

    public static View.OnTouchListener imgPress(){
        return imgPress(0x77eeddff); //DEFAULT color
    }

    public static View.OnTouchListener imgPress(final int color){
        return new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                switch(event.getAction()) {

                    case MotionEvent.ACTION_DOWN: {
                        ImageView view = (ImageView) v;
                        view.getDrawable().setColorFilter(color, PorterDuff.Mode.SRC_ATOP);
                        view.invalidate();
                        break;
                    }

                    case MotionEvent.ACTION_UP:
                        v.performClick();

                    case MotionEvent.ACTION_CANCEL: {
                        ImageView view = (ImageView) v;

                        //Clear the overlay
                        view.getDrawable().clearColorFilter();
                        view.invalidate();
                        break;
                    }
                }

                return true;
            }
        };
    }

    ...
}

Then I use it with onTouchListener:

ImageView img=(ImageView) view.findViewById(R.id.image);
img.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) { /* Your click action */ }
});
img_zc.setOnTouchListener(Utility.imgPress()); //Or Utility.imgPress(int_color_with_alpha)

It is very simple if you have a lot of images, and you want a simple onTouch effect, without any XML drawable and only one image.

Community
  • 1
  • 1
neoteknic
  • 1,930
  • 16
  • 32
3

I create sample here, just change ImageView into ClickableImageView from your layout. Hope it help.

enter image description here

thanhbinh84
  • 17,876
  • 6
  • 62
  • 69
3

Use an android.widget.Button, and set its background property to an android.graphics.drawable.StateListDrawable. This can all be done in XML, or programmatically. See the Custom Button section of the Form Stuff tutorial.

Ozone
  • 579
  • 2
  • 5
3

I think ImageButton is a better solution

<ImageButton
    android:layout_width="96dp"
    android:layout_height="56dp"
    android:src="@mipmap/ic_launcher"
    android:adjustViewBounds="true"
    android:background="@android:color/transparent"
    android:foreground="@drawable/selector" />
Dan Alboteanu
  • 9,404
  • 1
  • 52
  • 40
2

I have a more beauty solution if you use background images :)

public static void blackButton(View button){
    button.setOnTouchListener(new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    v.getBackground().setColorFilter(0xf0f47521,PorterDuff.Mode.SRC_ATOP);
                    v.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    v.getBackground().clearColorFilter();
                    v.invalidate();
                    break;
                }
            }
            return false;
        }
    });
}
András
  • 3,395
  • 1
  • 21
  • 27
2

Here's my solution, which, using "nineOldAndroids" library, supports old APIs too:

rootView.setOnTouchListener(new OnTouchListener() {

    @Override
    public boolean onTouch(final View v, final MotionEvent event) {

        switch (event.getAction()) {

            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL:
                v.setBackgroundResource(R.drawable.listview_normal);
                ViewHelper.setAlpha(imageView, 1);
                break;

            case MotionEvent.ACTION_DOWN:
                v.setBackgroundResource(0);
                v.setBackgroundColor(getResources().getColor(R.color.listview_pressed));
                ViewHelper.setAlpha(imageView, 0.75f);
                break;
        }
        return false;
    }
});

It assumes the rootView is the cell itself (the layout), and that it has a single imageView that you wish to be affected by the color that you wish to apply to the whole cell.


EDIT: if you wish, you can also extend ImageView to handle foreground, and set it to "?android:attr/selectableItemBackground". There is a library for this here and a tutorial on how to do it for any view you wish, here.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
  • @madlymad thank you for fixing the code formatting, though I think something went wrong with indentation. Anyway, it's good enough to be able to read it... – android developer May 12 '14 at 12:45
2

OR:

You can use this form, with Image Button.

Create file res/drawable/btn_video.xml:

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

And res/layout/activity_main.xml:

<ImageButton
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/imageButton"
    android:layout_gravity="center_horizontal"
    android:onClick="eventImageBtn"
    android:background="@drawable/btn_video"
    android:adjustViewBounds="true"
    android:scaleType="fitXY"
/>

Your image change with a click, and you can adjust with a linear layout:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:background="@color/menu_item_background">

        <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"
                      android:paddingLeft="@dimen/main_screen_side_padding" android:paddingRight="@dimen/main_screen_side_padding" android:paddingTop="@dimen/main_screen_side_padding" android:paddingBottom="@dimen/main_screen_side_padding"
                      android:background="#ffb3ff13" android:weightSum="10.00">


            <LinearLayout android:layout_weight="2.50" android:background="#ff56cfcd" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" >

                <ImageButton
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/imageButton"
                    android:layout_gravity="center_horizontal"
                    android:onClick="eventImageBtn"
                    android:background="@drawable/btn_video"
                    android:adjustViewBounds="true"
                    android:scaleType="fitXY"
                />
            </LinearLayout>

            <LinearLayout android:layout_weight="0.50" android:layout_height="0dp" android:background="#ffffffff" android:orientation="vertical" android:layout_width="fill_parent" >
            </LinearLayout>

            <LinearLayout android:layout_weight="4.50" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ff8aa5ff">
            </LinearLayout>

            <LinearLayout android:layout_weight="0.50" android:layout_height="0dp" android:background="#ffffffff" android:orientation="vertical" android:layout_width="fill_parent" >
            </LinearLayout>

            <LinearLayout android:layout_weight="2.00" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="0dp" android:background="#ffff7d1a" >
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>
</ScrollView>
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Alex Zaraos
  • 6,443
  • 2
  • 26
  • 21
1

Thanks for the help on this thread. However, you missed one thing...you need to handle the ACTION_CANCEL as well. If you don't then you might not properly restore the alpha value of the ImageView in the event that a parent view in the view hierarchy intercepts a touch event (think a ScrollView wrapping you ImageView).

Here is a complete class that is based off the above class but takes care of the ACTION_CANCEL as well. It uses an ImageViewCompat helper class to abstract the differences in the pre-post JellyBean API.

public class ChangeAlphaOnPressedTouchListener implements OnTouchListener {

    private final float pressedAlpha;

    public ChangeAlphaOnPressedTouchListener(float pressedAlpha) {
        this.pressedAlpha = pressedAlpha;
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView iv = (ImageView) v;
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            ImageViewCompat.setAlpha(iv, pressedAlpha);
            break;

        case MotionEvent.ACTION_MOVE:
            if (isInsideViewBounds(v, event)) {
                ImageViewCompat.setAlpha(iv, pressedAlpha);
            } else {
                ImageViewCompat.setAlpha(iv, 1f);
            }
            break;
        case MotionEvent.ACTION_UP:
            ImageViewCompat.setAlpha(iv, 1f);
            break;
        case MotionEvent.ACTION_CANCEL:
            ImageViewCompat.setAlpha(iv, 1f);
        }
        return false;
    }

    private static boolean isInsideViewBounds(View v, MotionEvent event) {
        return event.getX() > 0 && event.getX() < v.getWidth() && event.getY() > 0
                && event.getY() < v.getHeight();
    }
}
Matt Accola
  • 4,090
  • 4
  • 28
  • 37
1

Here is my code. The idea is that ImageView gets color filter when user touches it, and color filter is removed when user stops touching it.

Martin Booka Weser, András, Ah Lam, altosh, solution doesn't work when ImageView has also onClickEvent. worawee.s and kcoppock (with ImageButton) solution requires background, which has no sense when ImageView is not transparent.

This one is extension of AZ_ idea about color filter.

class PressedEffectStateListDrawable extends StateListDrawable {

    private int selectionColor;

    public PressedEffectStateListDrawable(Drawable drawable, int selectionColor) {
        super();
        this.selectionColor = selectionColor;
        addState(new int[] { android.R.attr.state_pressed }, drawable);
        addState(new int[] {}, drawable);
    }

    @Override
    protected boolean onStateChange(int[] states) {
        boolean isStatePressedInArray = false;
        for (int state : states) {
            if (state == android.R.attr.state_pressed) {
                isStatePressedInArray = true;
            }
        }
        if (isStatePressedInArray) {
            super.setColorFilter(selectionColor, PorterDuff.Mode.MULTIPLY);
        } else {
            super.clearColorFilter();
        }
        return super.onStateChange(states);
    }

    @Override
    public boolean isStateful() {
        return true;
    }
}

usage:

Drawable drawable = new FastBitmapDrawable(bm);
imageView.setImageDrawable(new PressedEffectStateListDrawable(drawable, 0xFF33b5e5));
Community
  • 1
  • 1
Malachiasz
  • 7,126
  • 2
  • 35
  • 49
1

I think the easiest way is creating a new XML file. In this case, let's call it "example.xml" in the drawable folder, and put in the follow code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/blue"
          android:state_pressed="true" />

</selector>

But before that you have to set the colors in the colors.xml file, in the values folder, like this:

<resources>
    <color name="blue">#0000FF</color>
</resources>

That made, you just set the Button / ImageButton to use the new layout, like this:

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/example"
/>

Then when you click that image, it will change to the color set in

<item android:drawable="@color/blue"
      android:state_pressed="true" />

giving the feedback that you want...

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Lima Neto
  • 53
  • 1
  • 9
1

I tried with:

<ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:contentDescription="@string/get_started"
        android:src="@drawable/home_started"
        style="?android:borderlessButtonStyle"
        android:adjustViewBounds="true"
        android:clickable="true"
        android:elevation="5dp"
        android:longClickable="true" />

and this worked. Please note on the line: style="?android:borderlessButtonStyle"

aviit
  • 1,957
  • 1
  • 27
  • 50
0

This is the best solution I ever seen. Its more generic.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
     android:fillAfter="true" >

    <alpha
        android:duration="100"
        android:fromAlpha="0.0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:toAlpha="1.0" />
</set>
Nipun
  • 990
  • 1
  • 16
  • 25
Nilesh
  • 237
  • 1
  • 3
  • 13
0

You can made a simple ripple effect for ImageView. This is great for icons.

In res/drawable create circular_shape.xml:

<?xml version="1.0" encoding="utf-8"?>
 <selector xmlns:android="http://schemas.android.com/apk/res/android">

  <item>
    <shape android:shape="oval">
        <solid android:color="@android:color/white"/>
    </shape>
  </item>

In res/drawable create a drawable res file ripple_effect_iv.xml:

<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="?attr/colorControlHighlight">

 <item
    android:id="@android:id/mask"
    android:drawable="@drawable/circular_shape" />

</ripple>

Set it as background for ImageView also you can consider padding for showing ripple natural:

     <ImageView
        android:background="@drawable/ripple_effect_iv"
        android:padding="10dp"/>

yes your ImageView become small but you can simply increase the android:layout_width and android:layout_height.

Mohsents
  • 691
  • 11
  • 9
-1

As for now, we should develop Material Design practice. In this case you could add a ripple effect on an ImageView.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
stuckedunderflow
  • 3,551
  • 8
  • 46
  • 63