Skip to content Skip to sidebar Skip to footer

Custom Spinner: Setselection Scrolling Down

I have a custom spinner I have a Hint label that is in the last position of my array(spinner), so to display it I set selection to the last position, like this: ArrayAdapter myAdap

Solution 1:

You can achieve this by extending Spinner and overriding methods that are responsible for setup and showing the list of values in the drop down:

public class CustomSpinner extends Spinner {

    private boolean mToggleFlag = true;

    public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode) {
        super(context, attrs, defStyle, mode);
    }

    public CustomSpinner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public CustomSpinner(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomSpinner(Context context, int mode) {
        super(context, mode);
    }

    public CustomSpinner(Context context) {
        super(context);
    }

    @Override
    public int getSelectedItemPosition() {
        if (!mToggleFlag) {
            return 0; // Gets to the first element
        }
        return super.getSelectedItemPosition();
    }

    @Override
    public boolean performClick() {
        mToggleFlag = false;
        boolean result = super.performClick();
        mToggleFlag = true;
        return result;
    }

}

Post a Comment for "Custom Spinner: Setselection Scrolling Down"