19

I have spinner but in it only selectable items are shown as default title..

is there any way i can set title which doesn't appear when spinner unfolds...

ngesh
  • 13,398
  • 4
  • 44
  • 60
  • please be more specific or an image will be for better for understanding . – Sujit Jun 17 '11 at 12:51
  • well to precise i need a title to spinner.. which is not selectable but is always visible before selecting anything in spinner... and it should not appear in the list of options selectable,.... – ngesh Jun 17 '11 at 12:54
  • 1
    look at this post http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one – Kartik Domadiya Jun 17 '11 at 12:56
  • Take a look here: http://developer.android.com/resources/tutorials/views/hello-spinner.html Also, you can create a custom dialog that looks identical to a spinner. Then you can do whatever you want with it. – SBerg413 Jun 17 '11 at 12:55

3 Answers3

58

You will have to set the prompt attribute of spinner.

<Spinner   android:id="@+id/discuss_about_spinner"  
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:prompt="@string/discuss_about_header"
           android:layout_below="@+id/what_time_layout">
</Spinner>

Programmatically set title to Spinner

spinner.setPrompt("Title");

I don't know what the reason is, prompt don't support direct substitution of a string value. Instead you must have your string in strings.xml.

Pradeep
  • 2,530
  • 26
  • 37
Andro Selva
  • 53,910
  • 52
  • 193
  • 240
  • 5
    This one should be selected as answer, as setting a title in a `Spinner` is already available. Otherwise use `spinner.setPrompt("Title")` method. – krishh Oct 11 '12 at 13:09
  • 5
    This should not be the selected answer, this isn't what the question is asking. The question is asking to have a spinner hold [1,2,3], but before an option is selected, the spinner itself does not have one of the [1,2,3] values shown. It shows a title such as "Select One" – JuiCe Jan 25 '13 at 15:42
4

I just realized what you were trying to do. You want the Spinner "button" to have a title. Something like "- Select One -", correct?

If that is indeed the case, here is how I accomplished this. Don't use a spinner. Instead, use a button and style with the title to look like a spinner (will have to find how I did this). When the user clicks the button, open a "select one" dialog and style it to look like a spinner's dialog. I'll try to put a code sample up shortly.

SBerg413
  • 14,515
  • 6
  • 62
  • 88
2

I'm afraid this is not available out-of-the-box. You'll need to implement this yourself.

inazaruk
  • 74,247
  • 24
  • 188
  • 156
  • i would ve done it.. but don't want to take unnecessary risk if its readily available..:) – ngesh Jun 17 '11 at 12:58