Faheem Ahmad
Faheem Ahmad

Reputation: 9

First item of spinner to set to select one

ArrayAdapter<StringWithTag> spinnerAdapter = new ArrayAdapter<StringWithTag>(RegisterActivity.this, android.R.layout.simple_spinner_item, countryList);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinnerCountry.setAdapter(spinnerAdapter);

Upvotes: 0

Views: 1414

Answers (3)

Tara
Tara

Reputation: 2648

Add first item of the spinner as "Select One" and in onclick listener set the condition as mentioned below.

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if (position == 0)
            {
               Toast.makeText(getApplicationContext(),"Please Select any one option",Toast.LENGTH_SHORT).show();
            }
            else if (position == 1)
            {
                // add you stuff
            }
            else {
                // add your stuff
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {

        }
    });

Upvotes: 1

Sathish Kumar J
Sathish Kumar J

Reputation: 4345

Create your countryList array with Select One element. like,

String[] countryList = new String[]{"Select One" ,"India" , "China" , "Japan"};

This may helps you

Upvotes: 2

Viral Patel
Viral Patel

Reputation: 33438

Add "Select One" as the first item in your list i.e. countryList

Upvotes: 0

Related Questions