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?
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 .
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.
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.
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.
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(); } }
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With