/* * Copyright (c) 2014. The Trustees of Indiana University. * * This version of the code is licensed under the MPL 2.0 Open Source license with additional * healthcare disclaimer. If the user is an entity intending to commercialize any application * that uses this code in a for-profit venture, please contact the copyright holder. */ package com.muzima.view; import android.annotation.SuppressLint; import android.content.Context; import android.util.AttributeSet; import android.view.View; import android.widget.Checkable; import android.widget.ImageView; import android.widget.RelativeLayout; public class CheckedRelativeLayout extends RelativeLayout implements Checkable { // TODO remove duplication with CheckedLinearLayOut private static final int[] CHECKED_STATE_SET = { android.R.attr.state_checked }; private boolean checked = false; @SuppressLint("NewApi") public CheckedRelativeLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CheckedRelativeLayout(Context context, AttributeSet attrs) { super(context, attrs); } public CheckedRelativeLayout(Context context) { super(context); } @Override public boolean isChecked() { return checked; } @Override public void setChecked(boolean checked) { this.checked = checked; refreshDrawableState(); //Propagate to childs final int count = getChildCount(); for (int i = 0; i < count; i++) { final View child = getChildAt(i); if(child instanceof Checkable) { ((Checkable)child).setChecked(checked); }else if(child instanceof ImageView){ child.setSelected(checked); } } } @Override protected int[] onCreateDrawableState(int extraSpace) { final int[] drawableState = super.onCreateDrawableState(extraSpace + 1); if (isChecked()) { mergeDrawableStates(drawableState, CHECKED_STATE_SET); } return drawableState; } @Override public void toggle() { this.checked = !this.checked; } }