Is it possibile to set hint Spinner in Android

JavaAndroidSpinner

Java Problem Overview


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!

Java Solutions


Solution 1 - Java

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

Solution 2 - Java

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

Solution 3 - Java

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();				
			}});
		
	}
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionNickView Question on Stackoverflow
Solution 1 - JavaBoni2kView Answer on Stackoverflow
Solution 2 - JavaRavi VyasView Answer on Stackoverflow
Solution 3 - JavaSBerg413View Answer on Stackoverflow