I'm trying to figure out how to create and use a custom integer button state for my Android application. I found this link for creating boolean (only two possible value) button states, and that seems to be working fine. But I have a case now where I would like to have the possibility of multiple states, and I'd like to designate them by an integer.
Here's what I'm doing now:
import android.content.Context;
import android.util.Log;
import android.widget.ImageView;
public class MultiStateImageView extends ImageView{
private final static String LOG_TAG = MultiStateImageView.class.getSimpleName();
private static final int[] MULTI_STATE = {R.attr.multi_state};
private final int numStates;
private int state;
public MultiStateImageView(Context context, int numStates){
super(context);
this.numStates = numStates;
state = 0;
}
public void setState(int state){
if(state >= numStates){
Log.e(LOG_TAG, "can't set a state higher than the number available!");
}
this.state = state;
refreshDrawableState();
}
@Override
public int[] onCreateDrawableState(int extraSpace){
final int[] drawableState = super.onCreateDrawableState(extraSpace + 2);
mergeDrawableStates(drawableState, MULTI_STATE);
return drawableState;
}
The problem is I'm obviously not using the state field at all. How am I supposed to do this for an integer state?