0

I have a Spinner, but the prompt message is ignored. I can't find why.

My layout :

<Spinner
       android:id="@+id/spinner"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_marginBottom="15dp"
       android:prompt="@string/age"
       android:drawSelectorOnTop="true"
       android:background="@drawable/spinner_bg" />

My code :

spinner = (Spinner) findViewById(R.id.spinner);
    SpinnerAdapter adapter = new SpinnerAdapter(this, R.layout.spinner_item, getApplicationContext().getResources().getStringArray(R.array.spinnerArray));
    spinner.setAdapter(adapter);
    adapter.setDropDownViewResource(R.layout.spinner_item);

The SpinnerAdapter :

public class SpinnerAdapter extends ArrayAdapter<String>{

public SpinnerAdapter(Context context, int textViewResourceId,
        String[] objects) {
    super(context, textViewResourceId, objects);
}


public View getView(int position, View convertView, ViewGroup parent) {
    View v = super.getView(position, convertView, parent);

    Typeface externalFont=Typeface.createFromAsset(getContext().getAssets(), "fonts/PermanentMarker.ttf");
    ((TextView) v).setTypeface(externalFont);

    return v;
}


public View getDropDownView(int position,  View convertView,  ViewGroup parent) {
    View v =super.getDropDownView(position, convertView, parent);

    Typeface externalFont=Typeface.createFromAsset(getContext().getAssets(), "fonts/PermanentMarker.ttf");
    ((TextView) v).setTypeface(externalFont);

    return v;
}

}

And the layout of the spinner_item :

<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="fill_parent"
android:layout_height="38dp"
android:ellipsize="marquee"
android:gravity="center_vertical|left"
android:minHeight="38dp"
android:paddingLeft="5dp"
android:singleLine="true"
android:textSize="18sp" />

Do you have any idea ?

g123k
  • 3,748
  • 6
  • 42
  • 45
  • Does `age` exist in your strings.xml? – Barak May 06 '12 at 16:26
  • Yes, I've tried with several strings – g123k May 06 '12 at 17:24
  • Did you figure out the issue? Was/is it really not working, or just not where you expected it to be (as I theorized)? – Barak May 10 '12 at 06:19
  • Possible duplicate of [How to make an Android Spinner with initial text "Select One"](http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one) – blahdiblah Mar 09 '13 at 11:18

1 Answers1

3

I think you are misunderstanding what android:prompt does... it doesn't set the text in the closed spinner, it sets the header/title of the open spinner.

Closed spinner:     Spinner Item 1

Open Spinner:       Android Spinner Prompt
                    ----------------------
                    Spinner Item 1
                    Spinner Item 2

There are a couple of ways to put the prompt in the closed spinner display. One way is to insert dummy data that will load into your spinner in the first position and then have your listener ignore that if it's selected (I don't like this as it puts junk in your data store).

Another option is to create a custom spinner adapter to insert the prompt as the first entry of the spinner (I like this one as it keeps the prompt in the code and keeps your data what it should be... data).

Hope this helps! Good luck!

Barak
  • 16,318
  • 9
  • 52
  • 84