2

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?

Community
  • 1
  • 1
mpellegr
  • 3,072
  • 3
  • 22
  • 36

1 Answers1

1

I think this isn't advisable and that's why it isn't supported. In my case, I tried one state that took numerical values with idle=0, walk=1, and run=2 for the button. I changed that to be three separate states as a boolean. In my example, one state could be true, so if there's a combination of them that are true, one is randomly chosen to be the state. I wish could I find a way to do this without allowing more than one true state though, like the numerical approach.

mpellegr
  • 3,072
  • 3
  • 22
  • 36