Android spinner set default text
Asked Answered
F

3

9

I've implemented a spinner into my app. I'm showing a list of schools but I want a default value/prompt so the user knows to select a school. I also want to do this without having to have the prompt as a list item. Any ideas?

Here is my spinner code so far:

selectSchoolSpinner = (Spinner) findViewById(R.id.select_school_spinner);
spinnerSchoolList = new ArrayList<String>();

spinnerSchoolList.add("Please select School"); 
//I don't want to have a prompt like the latter     

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_item, spinnerSchoolList);

spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

selectSchoolSpinner.setAdapter(spinnerAdapter);
Finnougric answered 8/12, 2013 at 8:50 Comment(0)
S
21

You should create custom ArrayAdapter with a String for your desired text in its constructor. Then, you should switch the first element of your array with your default text, and after the user click on the spinner it should switch back to the original array.

Example:

Activity class:

    public class SpinnerActivity extends Activity {

    Spinner spinner;
    String defaultTextForSpinner = "Your deafult text here";
    String[] arrayForSpinner = {"One", "Two", "Three"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_spinner);

        spinner = (Spinner) findViewById(R.id.your_spinner);
        spinner.setAdapter(new CustomSpinnerAdapter(this, R.layout.spinner_row, arrayForSpinner, defaultTextForSpinner));
    }
    }

CustomSpinnerAdapter class:

    public class CustomSpinnerAdapter extends ArrayAdapter<String>{

    Context context;
    String[] objects;
    String firstElement;
    boolean isFirstTime;

    public CustomSpinnerAdapter(Context context, int textViewResourceId, String[] objects, String defaultText) {
        super(context, textViewResourceId, objects);
        this.context = context;
        this.objects = objects;
        this.isFirstTime = true;
        setDefaultText(defaultText);
    }

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent) {
        if(isFirstTime) {
            objects[0] = firstElement;
            isFirstTime = false;
        }
        return getCustomView(position, convertView, parent);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) { 
        notifyDataSetChanged();
        return getCustomView(position, convertView, parent);
    }

    public void setDefaultText(String defaultText) {
        this.firstElement = objects[0];
        objects[0] = defaultText;
    }

    public View getCustomView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.spinner_row, parent, false);
        TextView label = (TextView) row.findViewById(R.id.spinner_text);
        label.setText(objects[position]);

        return row;
    }

    }
Spirant answered 12/2, 2014 at 17:13 Comment(5)
I get error on getCustomView method. what is R.id.tremps_spinner_row and R.id.tremps_spinner_textDropforge
Check my answer again, I've edited it. spinner_row is the layout for a spinner item. spinner_text is the TextView inside spinner_row layoutSpirant
I would have to add the whole custom adapter code separately?Olaolaf
IMHO it would be cleaner to keep a reference to textViewResourceId when you pass it to the constructor, instead of referencing it again in the getCustomView method.Elizabethelizabethan
I am having issue in this, I am unable to select my first option from dropdown, even when its visible, onItemSelected gets activated, after I click 2nd optionSluff
P
13

Try setSelection method to set default text:

spinner.setSelection(adapter.getPosition(DEFAULT_CURRENCY_TYPE))

And look this link: How to make an Android Spinner with initial text "Select One". It is very useful (@aaronvargas answer is what you need).

Potheen answered 8/12, 2013 at 8:53 Comment(0)
K
2
       @Override
    protected void onPostExecute(PositionBean[] result) {
        String[] spinnerArray = new String[result.length];

        if(result!=null) {

            for (int i = 0; i < result.length; i++) {

                String positionName = result[i].getPositionName();
                spinnerArray[i] = result[i].getPositionName();

            }
            ArrayList<String> positionList = new ArrayList<String>(Arrays.asList(spinnerArray));
            positionList.add(0,"select");
            String [] positions = positionList.toArray(new String[positionList.size()]);

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(EngineActivity.this, android.R.layout.simple_spinner_item,
                    positions);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            positionSpinner.setAdapter(adapter);
        }else{
            Log.d("NULL", "This is null");
        }

    }
Kunlun answered 8/2, 2016 at 13:11 Comment(1)
A little play around with the array fixed the problem, i hope this will help someoneKunlun

© 2022 - 2024 — McMap. All rights reserved.