g123k
g123k

Reputation: 3874

android:prompt is ignored

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 ?

Upvotes: 0

Views: 2035

Answers (1)

Barak
Barak

Reputation: 16393

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!

Upvotes: 3

Related Questions