Jumana Alhaddad
Jumana Alhaddad

Reputation: 285

Spinner prompt doesn't show up

I want to use spinner prompt like "Select City". I did set a value for select_city in string layout use in layout android:prompt="@string/select_city" but it did not work. Also tried to use sp_city.setPrompt("Select City"); also did not work.

What is my problem? How can I fix it and set the prompt?

Layout:

<Spinner
    android:id="@+id/spinner_poi_city"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/btn_dropdown"
    android:spinnerMode="dropdown"/>

Class:

public class FirstPOIPreference extends DialogPreference {

    private Spinner sp_city;
    private ArrayAdapter<String> adapter;
    private POI poiDialog = new POI();

    public FirstPOIPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        setPersistent(false);
        setDialogLayoutResource(R.layout.pref_poi_dialog);
        setPositiveButtonText(android.R.string.ok);
        setNegativeButtonText(android.R.string.cancel);
    }

    @Override
    protected void onBindDialogView(View view) {
        super.onBindDialogView(view);
        initViews(view);
    }

    private void initViews(View view) {
        sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);
        sp_city.setPrompt("City");
        String[] arrayCity = new String[]{"Erie", "Pittsburgh", "Cleveland", "Buffalo", "Niagara Falls", "Jamestown"};
        adapter = new ArrayAdapter <> (this.getContext(), android.R.layout.simple_spinner_dropdown_item, arrayCity);
        sp_city.setAdapter(adapter);

        sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView <?> parentView, View selectedItemView, int position, long id) {
                poiDialog.setCity(sp_city.getSelectedItem().toString());
            }

            @Override
            public void onNothingSelected(AdapterView <?> parentView) {
            }
        });
    }

}

Upvotes: 1

Views: 6790

Answers (3)

Sagar Giri
Sagar Giri

Reputation: 2231

You are mistaking prompt with default text. Tap on the spinner and you will see "select City" as title

Prompt is used to show title on dropdown popup not for default text.

If you want to select the default value on spinner when you have not selected any value from spinner dropdown. Then you must use

NothingSelectedSpinnerAdapter

How to make an Android Spinner with initial text "Select One"

Upvotes: 0

Jignesh Jain
Jignesh Jain

Reputation: 1568

you can do it this way, i have done it as some tricky way.it helps you

private void initViews(View view) {

sp_city = (Spinner) view.findViewById(R.id.spinner_poi_city);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, 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("Erie");
            adapter.add("Pittsburgh");
            adapter.add("Cleveland");
            adapter.add("Buffalo");
            adapter.add("Niagara Falls");
            adapter.add("Jamestown");
            adapter.add("Select City"); //This is the text that will be displayed as hint.

            sp_city.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
                    String spinnerValue = String.valueOf(sp_city.getSelectedItem().toString());
                    if (!spinnerValue.equalsIgnoreCase("Select City")) {
                        //do your code
                        Toast.makeText(this, sp_city.getSelectedItem().toString(), Toast.LENGTH_LONG).show();
                    }
                }
                @Override
                public void onNothingSelected(AdapterView<?> parentView) {
                }
            });

            sp_city.setAdapter(adapter);
            sp_city.setSelection(adapter.getCount()); //set the hint the default selection so it appears on launch.
        }

Upvotes: 2

Hammad Akram
Hammad Akram

Reputation: 573

I think better solution is to use "Select City" as a first item of the array thats you will add to spinner. So by default it will show Select city. And you can check if city is selected or not by checking spinner selected position. If position is 0 show toast or something like "Please select city."

Upvotes: 0

Related Questions