Android Spinner -- how to size to currently selected item?

android, android-spinner

Solution

I achieved this now by subclassing Spinner:

public class SpinnerWrapContent extends IcsSpinner {
    private boolean inOnMeasure;

    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {       
         inOnMeasure = true;
         super.onMeasure(widthMeasureSpec, heightMeasureSpec);
         inOnMeasure = false;
    }

    public boolean isInOnMeasure() {
        return inOnMeasure;
    }
}

Then in my SpinnerAdapter's getView(), I used the currently selected position if I am called from onMeasure():

    public View getView(int position, View convertView, ViewGroup parent) {
        View view;

        if (convertView != null)
            view = convertView;
        else {
            int fixedPosition = (spinner.isInOnMeasure() ? spinner.getSelectedItemPosition() : position);

            // Here create view for fixedPosition
        }

        return view;
    }

Problem

It appears that a spinner is sized to the longest item given in its adapter. This is a good behavior for the majority of cases, but it is undesirable in my particular case. Is this possible to turn this off? My guess is no, by looking at Spinner.onMeasure() in the source, but figured I'd ask.

Original source

Related problems