Pages Navigation Menu

Coding is much easier than you think

How to make an Android Spinner with initial text €œSelect One€

How to make an Android Spinner with initial text €œSelect One€

 
There is two methods to do this. One is through XML and another is through Programmatically.
 

Method 1 : XML

First, you might be interested in the “prompt” attribute of the Spinner class. See the picture below, “Choose a Planet” is the prompt that can be set in the xml with android:prompt=””

I was going to suggest subclassing Spinner, where you could maintain two adapters internally. One adapter that has the “Select One” option, and the other real adapter (with the actual options), then using the OnClickListener to switch the adapters before the choices dialog is shown.. however after trying implement that idea I’ve come to the conclusion you cannot receive OnClick events for the widget itself.

You could wrap the spinner in a different view, intercept the clicks on the view, and then tell your CustomSpinner to switch the adapter, but seems like an awful hack.

 
** UPDATE: Android Complete tutorial now available here.
 

Method 2: Programtically

Here’s a general solution that overrides the Spinner view. It overrides setAdapter() to set the initial position to -1, and proxies the supplied SpinnerAdapter to display the prompt string for position<0.

/**
 * A modified Spinner that doesn't automatically select the first entry in the list.
 *
 * Shows the prompt if nothing is selected.
 *
 * Limitations: does not display prompt if the entry list is empty.
 */
public class NoDefaultSpinner extends Spinner {

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

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

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

    @Override
    public void setAdapter(SpinnerAdapter orig ) {
        final SpinnerAdapter adapter = newProxy(orig);

        super.setAdapter(adapter);

        try {
            final Method m = AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt",int.class);
            m.setAccessible(true);
            m.invoke(this,-1);

            final Method n = AdapterView.class.getDeclaredMethod("setSelectedPositionInt",int.class);
            n.setAccessible(true);
            n.invoke(this,-1);

        } catch( Exception e ) {
            throw new RuntimeException(e);
        }
    }

    protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
        return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
                obj.getClass().getClassLoader(),
                new Class[]{SpinnerAdapter.class},
                new SpinnerAdapterProxy(obj));
    }    

    /**
     * Intercepts getView() to display the prompt if position &lt; 0
     */
    protected class SpinnerAdapterProxy implements InvocationHandler {

        protected SpinnerAdapter obj;
        protected Method getView;

        protected SpinnerAdapterProxy(SpinnerAdapter obj) {
            this.obj = obj;
            try {
                this.getView = SpinnerAdapter.class.getMethod("getView",int.class,View.class,ViewGroup.class);
            } catch( Exception e ) {
                throw new RuntimeException(e);
            }
        }

        public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
            try {
                return m.equals(getView) &amp;&amp; (Integer)(args[0])

About Gokul


I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.

  • http://gravatar.com/nachtien nachtien

    Why are you doing this?

    catch( Exception e ) {
    throw new RuntimeException(e);
    }