56

Is there anyway of making a hint for a spinner similar to hint that is provided for edit text fields. I know you can use a prompt that gives you a title bar but still leaves the initial spinner field blank until you click into the spinner. I currently have a crude way of setting a dummy field as the first part of the spinner array which is the question and then have a check at the end to make sure the spinner doesn't equal the question string. Is there any cleaner / better way of doing this?

Thanks!

Samet ÖZTOPRAK
  • 3,112
  • 3
  • 32
  • 33
Nick
  • 9,285
  • 33
  • 104
  • 147

3 Answers3

151

Here's a solution which is probably a bit simpler than Ravi Vyas code (thanks for the inspiration!):

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item) {

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        View v = super.getView(position, convertView, parent);
        if (position == getCount()) {
            ((TextView)v.findViewById(android.R.id.text1)).setText("");
            ((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
        }

        return v;
    }       

    @Override
    public int getCount() {
        return super.getCount()-1; // you dont display last item. It is used as hint.
    }

};

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("Item 1");
adapter.add("Item 2");
adapter.add("Hint to be displayed");

spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //display hint
Boni2k
  • 3,255
  • 3
  • 23
  • 27
  • 7
    i love your solution Boni, however, were you able to make it working on orientation change? whenever I rotate the device the last value from adapter is selected instead of hint – AndroidGecko Nov 20 '12 at 11:03
  • 1
    @matej.smitala: You have to store the selected spinner position inside onSaveInstanceState: http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please – Boni2k Nov 23 '12 at 17:02
  • What is 'android.R.id.text1' referring too? Im not sure what text1 is. – Scalahansolo Aug 19 '13 at 19:44
  • 1
    It points to the TextView that is used in a spinner item – Boni2k Aug 20 '13 at 20:07
  • 4
    When this is done, the spinner starts at the bottom of the list. Any good way to remedy this? – loeschg Apr 15 '14 at 21:46
  • 2
    @loeschg maybe calling setSelection(0) before the spinner is opened (e.g. by setting onTouchListener and looking for the ACTION_DOWN event)? – Boni2k Apr 16 '14 at 13:07
  • On saving instance is not helping. It is still selecting last value before hinttext item – NinjaCoder Jul 28 '14 at 21:06
  • After notifyDataSetChanged the setSelection command would not work. – Mbt925 Dec 28 '14 at 20:37
  • @ITurchenko - I wish i had read your comment at first! :/ – Jignesh Shah Dec 02 '16 at 07:56
  • If i set `@Override public int getCount() { return super.getCount(); }` then it display hint and also in drop down list it includes hint. and if i set `@Override public int getCount() { return super.getCount()-1; } ` then list is proper but hint is not set . why ? i dont know how to do it proper. – Dharmishtha Sep 11 '17 at 07:11
  • @AndroidGecko Do you manage to resolve the problem, when orientation change? – K.Os Nov 01 '17 at 19:20
  • There is no hint displayed when Spinner has just `adapter.add("Hint to be displayed");` value – CGR Dec 18 '17 at 16:41
23

You can setup your own spinner adapter and overide the getView method to show the hint instead of an item . I have created a sample project on github , check it out here

Ravi Vyas
  • 12,212
  • 6
  • 31
  • 47
15

An even easier way than setting up your own spinner adapter is to just use a button and style it like a spinner object with

android:background="@android:drawable/btn_dropdown"

Then setup the button's onClick event to open a single-item-select dialog. You can then do whatever you want with the text of the button.

This has been my preferred way of handling this for a while. Hope it helps someone.

EDIT: I've been playing around with this again recently (and someone asked me to post an example a while ago). This strategy will look a bit different if you're using the Holo theme. However, if you're using other themes such as Theme.Black, this will look identical.

To demonstrate this, I made a simple app that has both a regular Spinner along with my custom button-spinner. I threw this up in a GitHub repo, but here's what the Activity looks like:

package com.stevebergamini.spinnerbutton;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Spinner;

public class MainActivity extends Activity {

    Spinner spinner1;
    Button button1;
    AlertDialog ad;
    String[] countries;

    int selected = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinner1 = (Spinner) findViewById(R.id.spinner1);
        button1 = (Button) findViewById(R.id.button1);

        countries = getResources().getStringArray(R.array.country_names);

        //  You can also use an adapter for the allert dialog if you'd like
        //  ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, countries);        

        ad = new AlertDialog.Builder(MainActivity.this).setSingleChoiceItems(countries, selected,  
                new  DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            button1.setText(countries[which]);
                            selected = which;
                            ad.dismiss();

                        }}).setTitle(R.string.select_country).create(); 


        button1.setOnClickListener( new OnClickListener(){

            @Override
            public void onClick(View v) {
                ad.getListView().setSelection(selected);
                ad.show();              
            }});

    }
}
SBerg413
  • 14,515
  • 6
  • 62
  • 88
  • Could you also post onClick code to invoke such single item select dialog? – hendrix Nov 26 '12 at 11:33
  • 1
    for the spinner button style you can use this to make it use a spinners drop down .. style="?android:attr/spinnerStyle" – j2emanue May 20 '14 at 16:58