How to Give an Imageview Click Effect Like a Button on Android

How can I give an imageview click effect like a button on Android?

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;
}
});

Imageview on-touch/click effect without multiple effects (Android)

In a click function add

 ImageView img= (ImageView) view.findViewById(R.id.theID);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
img.startAnimation(animation);

Create an anim folder in Assets and then cleate an animation resource file named alpha

inside the file paste the following

<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_interpolator"
android:duration="50"
android:fromAlpha="0.5"
android:toAlpha="1.0" />

This type of animation just sets the Alpha tranparency to 50% and back to 100% so the image will flash.

There are many animations so have a look around on the net to find which one you like and create the anim resource file and put the name here R.anim.alpha);

in your imageView xml file you can add android:onClick="myClickFunction"

and then in the activity add

public void myClickFunction(View v) {
ImageView img = (ImageView) v.findViewById(R.id.theID);
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.alpha);
img.startAnimation(animation);
}

Button like click effect for Imageview in android

Try a selector like this

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

<item android:state_pressed="true"><shape>
<solid android:color="#151B8D" />

<stroke android:width="1dp" android:color="#151B8D" />

<corners android:bottomLeftRadius="8dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="8dp" />

<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape></item>
<item><shape>
<gradient android:angle="270" android:endColor="#151B8D" android:startColor="#151B8D" />

<stroke android:width="1px" android:color="#000000" />

<corners android:bottomLeftRadius="8dp" android:bottomRightRadius="0dp" android:topLeftRadius="0dp" android:topRightRadius="8dp" />

<padding android:bottom="10dp" android:left="10dp" android:right="10dp" android:top="10dp" />
</shape></item>

</selector>

How to set button click effect in Android?

This can be achieved by creating a drawable xml file containing a list of states for the button. So for example if you create a new xml file called "button.xml" with the following code:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/YOURIMAGE" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="@drawable/gradient" />
<item android:drawable="@drawable/YOURIMAGE" />
</selector>

To keep the background image with a darkened appearance on press, create a second xml file and call it gradient.xml with the following code:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<bitmap android:src="@drawable/YOURIMAGE"/>
</item>
<item>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient android:angle="90" android:startColor="#880f0f10" android:centerColor="#880d0d0f" android:endColor="#885d5d5e"/>
</shape>
</item>
</layer-list>

In the xml of your button set the background to be the button xml e.g.

android:background="@drawable/button"

Changed the above code to show an image (YOURIMAGE) in the button as opposed to a block colour.

How can I give a click effect to my image?

return false;

It doesn't seem to accept the changes as with the statement you've wrote.
Please change it into the following:

return true;

Any more questions, feel free to ask :)

How I can give the effect of a button to an ImageView when I click?

You can set a selector as the background, however is is much better to use an ImageButton since it was essentially design to do just that.

FYI here is the style definition of ImageButton

<style name="Widget.ImageButton">
<item name="android:focusable">true</item>
<item name="android:clickable">true</item>
<item name="android:scaleType">center</item>
<item name="android:background">@android:drawable/btn_default</item>
</style>

How to set a ripple effect on textview or imageview on Android?

Ref : http://developer.android.com/training/material/animations.html,

http://wiki.workassis.com/category/android/android-xml/

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

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

Ripple effect over imageview

Add android:background="@null" for the ImageView

How to make a circular ripple on a button when it's being clicked?

If you already have a background image, here is an example of a ripple that looks close to selectableItemBackgroundBorderless:

            <ImageButton
android:id="@+id/btn_show_filter_dialog"
android:layout_width="24dp"
android:layout_height="24dp"
android:background="@drawable/ic_filter_state"/>

ic_filter_state.xml:

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

state_pressed_ripple.xml: (opacity set to 10% on white background) 1AFFFFFF

 <?xml version="1.0" encoding="UTF-8"?>    
<ripple xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="oval">
<solid android:color="#1AFFFFFF"/>
</shape>
<color android:color="#FFF"/>
</item>
</ripple>


Related Topics



Leave a reply



Submit