1

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) {
            }
        });
    }

}
Jumana Alhaddad
  • 285
  • 2
  • 6
  • 17

3 Answers3

2

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.
        }
Jignesh Jain
  • 1,518
  • 12
  • 28
0

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."

Hammad Akram
  • 573
  • 3
  • 9
  • if you can do this way spinner has one extra value display like "select city." also when you show toast like "Please select city". it will display every time by default at initial level. – Jignesh Jain Apr 25 '17 at 04:54
  • No. You can use setOnItemClickListener after setting adapter. And secondly you don't need set on item click listener, just get spinner position by getSlectedItemPosition before doing some action. – Hammad Akram Apr 25 '17 at 05:11
  • ya i know that but i have concern regarding that first value select city always there in spinner list.that's the wrong way to show users. – Jignesh Jain Apr 25 '17 at 05:22
  • This is the easiest way. Can be done by custom adapter – Hammad Akram Apr 25 '17 at 05:24
0

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"

Community
  • 1
  • 1
Sagar Giri
  • 2,221
  • 1
  • 10
  • 20