Skip to content

Instantly share code, notes, and snippets.

@blazsolar
Created February 19, 2014 16:55
Show Gist options
  • Save blazsolar/9096254 to your computer and use it in GitHub Desktop.
Save blazsolar/9096254 to your computer and use it in GitHub Desktop.
Android RelativeLayout with Checkable so it can be used in ListView CHOICE_MODE_SINGLE or CHOICE_MODE_MULTIPLE
package com.wefika.pro.widgets;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.Checkable;
import android.widget.RelativeLayout;
/**
* Created by Blaž Šolar on 7/17/13.
*/
public class CheckableRelativeLayout extends RelativeLayout implements Checkable {
private static final int[] CHECKED_STATE_SET = {
android.R.attr.state_checked,
};
private boolean mChecked;
public CheckableRelativeLayout(Context context) {
super(context);
}
public CheckableRelativeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public CheckableRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public void setChecked(boolean checked) {
if(mChecked != checked) {
mChecked = checked;
refreshDrawableState();
}
}
@Override
public boolean isChecked() {
return mChecked;
}
@Override
public void toggle() {
setChecked(!mChecked);
}
@Override
protected int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment