16

I have a problem with android:prompt for a spinner. I used this code in the XML file but it doesn't work:

<Spinner 
    android:id="@+id/spinner" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="120dp"
    android:prompt="@string/club_type">
</Spinner>

I also tried to use this code in my main activity but this doesn't work either:

spinner.setPrompt("Select club");

While I was using the second case I didn't use android:prompt; in other words, I tried them individually. Could someone help me?

Ahmed Salman Tahir
  • 1,783
  • 1
  • 17
  • 26
atapi19
  • 227
  • 1
  • 3
  • 14
  • Dublicate question[WATCH THIS][1] Try it, hope it will helpet you [1]: http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one/12221309#12221309 – Kostya Khuta Oct 24 '14 at 07:39
  • 1
    prompt only showed up on dialog mode and spinner's default mode is dropdown in order to change the mode use android:spinnerMode="dialog" – Reflection Jul 29 '20 at 07:04

2 Answers2

20

There are two ways you can deal with that:

Static way:

add one line code in XML's Spinner tag

android:spinnerMode="dialog"

and then set:

android:prompt="PROMPT"

In dynamic way:

use

Spinner spinner = (Spinner)findViewById(R.id.spnner); 
String[] myItems= getResources().getStringArray(R.array.spinner1);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(this, 
android.R.layout.select_dialog_item, myItems);

spinner.setPrompt("PROMPT");

when you set and initialize your adapter

hope that can help you! :)

Ali Behzadian Nejad
  • 8,804
  • 8
  • 56
  • 106
RobotCharlie
  • 1,180
  • 15
  • 19
9

Working perfectly on mine.

You are mistaking prompt with first element. Tap on the spinner and you will see Select club as the heading which is the prompt.

Hope this helps.

Nabin
  • 11,216
  • 8
  • 63
  • 98
  • 4
    You're right! My code show prompt in the Dialog Mode, I saw only drop down. So, how can I show "select club" like first element but it can't be selected? – atapi19 Oct 24 '14 at 07:54
  • I think you will need to put "select club" as the first element. If you can't do it, you can always ask new questions. Best of luck Breaking Bad – Nabin Oct 24 '14 at 07:56
  • 1
    Ok, I'll try some code and then I'll ask. Thank you very much! ahaha ;) – atapi19 Oct 24 '14 at 07:59