Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 980951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T04:27:57+00:00 2026-05-16T04:27:57+00:00

I want to use a Spinner that initially (when the user has not made

  • 0

I want to use a Spinner that initially (when the user has not made a selection yet) displays the text “Select One”. When the user clicks the spinner, the list of items is displayed and the user selects one of the options. After the user has made a selection, the selected item is displayed in the Spinner instead of “Select One”.

I have the following code to create a Spinner:

String[] items = new String[] {"One", "Two", "Three"};
Spinner spinner = (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

With this code, initially the item “One” is displayed. I could just add a new item “Select One” to the items, but then “Select One” would also be displayed in the dropdown list as first item, which is not what I want.

How can I fix this problem?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T04:27:57+00:00Added an answer on May 16, 2026 at 4:27 am

    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 less than 0.

    This has been tested on Android 1.5 through 4.2, but buyer beware! Because this solution relies on reflection to call the private AdapterView.setNextSelectedPositionInt() and AdapterView.setSelectedPositionInt(), it’s not guaranteed to work in future OS updates. It seems likely that it will, but it is by no means guaranteed.

    Normally I wouldn’t condone something like this, but this question has been asked enough times and it seems like a reasonable enough request that I thought I would post my solution.

    /**
     * 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 < 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) && 
                           (Integer)(args[0])<0 ? 
                             getView((Integer)args[0],(View)args[1],(ViewGroup)args[2]) : 
                             m.invoke(obj, args);
                } 
                catch (InvocationTargetException e) {
                    throw e.getTargetException();
                } 
                catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
    
            protected View getView(int position, View convertView, ViewGroup parent) 
              throws IllegalAccessException {
    
                if( position<0 ) {
                    final TextView v = 
                      (TextView) ((LayoutInflater)getContext().getSystemService(
                        Context.LAYOUT_INFLATER_SERVICE)).inflate(
                          android.R.layout.simple_spinner_item,parent,false);
                    v.setText(getPrompt());
                    return v;
                }
                return obj.getView(position,convertView,parent);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Below is my stored procedure. I want use stored procedure select all row of
I want to use the MultipleLookupField control in a web page that will run
I've got a common use-case wherein a user selects one item out of many
I want to use the mouse scrollwheel in my OpenGL GLUT program to zoom
I want to use Powershell to write some utilities, leveraging our own .NET components
I want to use the functions exposed under the OpenGL extensions. I'm on Windows,
I want to use the Publish.GacRemove function to remove an assembly from GAC. However,
I want to use the Web Browser control within an mono application, but when
I want to use CodeDOM to both declare and initialize my static field in
I want to use SQL Profiler to trace the queries executed agains my database,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.