Skip to content

Instantly share code, notes, and snippets.

@gnumilanix
Last active June 2, 2017 01:53
Show Gist options
  • Save gnumilanix/7c1e1391c6150261477fe9fc4624f07c to your computer and use it in GitHub Desktop.
Save gnumilanix/7c1e1391c6150261477fe9fc4624f07c to your computer and use it in GitHub Desktop.
public class CheckableImageView extends AppCompatImageView implements Checkable {
private boolean isChecked;
private OnUpdateImageListener onUpdateImageListener;
public CheckableImageView(Context context) {
super(context, null);
}
public CheckableImageView(Context context, AttributeSet attrs) {
super(context, attrs, 0);
}
public CheckableImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/**
* Sets an implementation of {@link OnUpdateImageListener}
*
* @param onUpdateImageListener that can update image state
*/
public void setOnUpdateImageListener(OnUpdateImageListener onUpdateImageListener) {
this.onUpdateImageListener = onUpdateImageListener;
}
@Override
public boolean isChecked() {
return isChecked;
}
@Override
public void setChecked(boolean checked) {
this.isChecked = checked;
updateImage(checked);
}
@Override
public void toggle() {
setChecked(!isChecked());
}
@Override
protected void drawableStateChanged() {
updateImage(isChecked());
super.drawableStateChanged();
}
private void updateImage(boolean isChecked) {
if (null != onUpdateImageListener) {
onUpdateImageListener.onUpdateImage(isChecked);
}
}
/**
* Interface to be implemented by classes that can update image checkable state
*/
public interface OnUpdateImageListener {
/**
* Method that should update an image based on the state
*
* @param isChecked true if checked image should be loaded
*/
void onUpdateImage(boolean isChecked);
}
}

Define your CheckableImageView in your layout

<com.milanix.view.CheckableImageView
    android:id="@+id/ivLogo"
    android:layout_width="60dp"
    android:layout_height="60dp" />

After inflating your view in the code, attach an implementation

ivLogo.setOnUpdateImageListener(new CheckableImageView.OnUpdateImageListener() {
    @Override
    public void onUpdateImage(boolean isChecked) {
        Glide.with(OrderConfigurationFragment.this.getContext()).load(isChecked ? checkedImageUrl : normalImageUrl).into(ivLogo);
        }
    });

Even better with lambdas:

ivLogo.setOnUpdateImageListener(isChecked -> Glide.with(getContext()).load(isChecked ? checkedImageUrl : normalImageUrl).into(ivLogo));

Or with a Databinding?

<com.milanix.view.CheckableImageView
    android:id="@+id/ivLogo"
    android:layout_width="60dp"
    android:layout_height="60dp"
    app:checkedImageUrl="..."
    app:normalImageUrl="..."/>

And a BindingAdapter

/**
 * Sets an image based on state to the given {@link CheckableImageView}
 *
 * @param view            to set margin to
 * @param checkedImageUrl image to set when checked
 * @param normalImageUrl  image to set when normal
 */
@BindingAdapter(value = {"checkedImageUrl", "normalImageUrl"})
public static void setImage(CheckableImageView view, String checkedImageUrl, String normalImageUrl) {
    view.setOnUpdateImageListener(isChecked -> Glide.with(view.getContext()).load(isChecked ? checkedImageUrl : normalImageUrl).into(view));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment