I have to create a set of View representing week's days and that are checkable, to select and deselect the availability in that day. Days buttons layout
I create a custom view that extends Button and implements Checkable. I assigned a selector drawable to it in order to change color when checked.
The problem is that it doesn't change drawable when clicked. Do I need to specify in the listener that it has to change drawable? It's not yet a Checkable property?
I can't use any other Checkable View like Toggle, ChechedTextView, cause I need this particular style for the view.
CheckableButton
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.Checkable;
public class CheckableButton extends Button implements Checkable {
private static final int[] CHECKED_STATE_SET = {android.R.attr.state_checked};
private boolean mChecked = false;
private OnCheckedChangeListener mOnCheckedChangeListener;
public CheckableButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
public boolean isChecked() {
return mChecked;
}
public void setChecked(boolean b) {
if (b != mChecked) {
mChecked = b;
refreshDrawableState();
if (mOnCheckedChangeListener != null) {
mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
}
}
}
public void toggle() {
setChecked(!mChecked);
}
@Override
public int[] onCreateDrawableState(int extraSpace) {
final int[] drawableState = super.onCreateDrawableState(extraSpace + 1);
if (isChecked()) {
mergeDrawableStates(drawableState, CHECKED_STATE_SET);
}
return drawableState;
}
public void setOnCheckedChangeListener(OnCheckedChangeListener listener) {
mOnCheckedChangeListener = listener;
}
public static interface OnCheckedChangeListener {
void onCheckedChanged(View checkableView, boolean isChecked);
}
}
layout (part of)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<eu.ecomind.pitagora.utils.CheckableButton
android:id="@+id/mon"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/day_selection"
android:gravity="center"
android:text="L"
android:textColor="@color/text_background_dark_customer" />
<eu.ecomind.pitagora.utils.CheckableButton
android:id="@+id/tue"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/day_selection"
android:gravity="center"
android:text="M"
android:textColor="@color/text_background_dark_customer" />
<eu.ecomind.pitagora.utils.CheckableButton
android:id="@+id/wed"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/day_selection"
android:gravity="center"
android:text="M"
android:textColor="@color/text_background_dark_customer" />
<eu.ecomind.pitagora.utils.CheckableButton
android:id="@+id/thu"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/day_selection"
android:gravity="center"
android:text="G"
android:textColor="@color/text_background_dark_customer" />
<eu.ecomind.pitagora.utils.CheckableButton
android:id="@+id/fri"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/day_selection"
android:gravity="center"
android:text="V"
android:textColor="@color/text_background_dark_customer" />
<eu.ecomind.pitagora.utils.CheckableButton
android:id="@+id/sat"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/day_selection"
android:gravity="center"
android:text="S"
android:textColor="@color/text_background_dark_customer" />
<eu.ecomind.pitagora.utils.CheckableButton
android:id="@+id/sun"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="@drawable/day_selection"
android:gravity="center"
android:text="D"
android:textColor="@color/text_background_dark_customer" />
</LinearLayout>
selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://ift.tt/nIICcg">
<item android:drawable="@drawable/day_selected"
android:state_checked="true"/>
<item android:drawable="@drawable/day_not_selected"/>
</selector>
Aucun commentaire:
Enregistrer un commentaire