Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checkable ImageView

Tags:

Does anybody know if it possible to make ImageView checkable. I try to use State-list drawable resource in my project where I define pictures for my ImageView check states, but there is no property to make ImageView checkable, only clickable.

Maybe anybody knows a way to solve this problem?

like image 310
teoREtik Avatar asked Mar 25 '11 08:03

teoREtik


People also ask

Can ImageView be clickable?

You can make a view clickable, as a button, by adding the android:onClick attribute in the XML layout. For example, you can make an image act like a button by adding android:onClick to the ImageView .

What is ImageView?

ImageView class is used to display any kind of image resource in the android application either it can be android. graphics. Bitmap or android. graphics. drawable.

Can ImageView display video?

ImageView is a control which display image on android and support RGB image, so setting the data of ImageView every period can generate dynamic video.

Which attribute is used to set an image in ImageView?

2. src: src is an attribute used to set a source file or you can say image in your imageview to make your layout attractive.


2 Answers

Use this custom ImageView implementation (taken from here) :

public class CheckableImageView extends ImageView implements Checkable {     private boolean mChecked;      private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked };      public CheckableImageView(final Context context, final AttributeSet attrs) {         super(context, attrs);     }      @Override     public int[] onCreateDrawableState(final int extraSpace) {         final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);         if (isChecked())             mergeDrawableStates(drawableState, CHECKED_STATE_SET);         return drawableState;     }      @Override     public void toggle() {         setChecked(!mChecked);     }      @Override     public boolean isChecked() {         return mChecked;     }      @Override     public void setChecked(final boolean checked) {         if (mChecked == checked)             return;         mChecked = checked;         refreshDrawableState();     } } 
like image 121
android developer Avatar answered Nov 09 '22 06:11

android developer


Instead of making an ImageView checkable you can set a state list drawable as background for a checkbox which will automatically flip the images.

<?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_checkable="true" android:drawable="@drawable/icon_pressed" /> <!-- pressed -->     <item android:state_checked="true" android:drawable="@drawable/icon_focused" /> <!-- focused -->     <item android:drawable="@drawable/icon_default" /> <!-- default --> </selector> 
like image 21
Abhinav Avatar answered Nov 09 '22 07:11

Abhinav