Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possibile to set hint Spinner in Android [duplicate]

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!

like image 364
Nick Avatar asked Jul 06 '11 20:07

Nick


People also ask

How can I use more than one spinner in Android?

Clearly each of your adapters need to adapt a different set of String s, so you need to create an ArrayAdapter for each Spinner . A single OnItemSelectedListener will work for the 3 Spinners (as long as you set them). You can call getId() on the AdapterView<?>

How can I tell if spinner is clicked or not in android?

Let the Spinner's first value be something like "-please select-". when the user clicks on the next button perform validation and check whether the value of the selectedItem in the spinner is "-please select-" and if yes, then display a toast and ask the the user to select something from the spinner.

What is spinner in Android with example?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.


2 Answers

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 
like image 85
Boni2k Avatar answered Oct 12 '22 05:10

Boni2k


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

like image 23
Ravi Vyas Avatar answered Oct 12 '22 06:10

Ravi Vyas