Show default value in Spinner in android

rahul picture rahul · Jun 12, 2013 · Viewed 115.8k times · Source

I want to show a dropdown for gender selection. I passed a string array as

String arr[]=new String[]{"male","female"};

but the problem is that is shows default selection with the value of "male" and I want to show "Gender" as default value. If I pass "Gender" in array at position 0, then it is visible in dropdown also. I just want "Gender" as hint but it must not be shown in dropdown.

Can anybody tell me how I can do this. Thanks in advance.

Answer

Dhaval Parmar picture Dhaval Parmar · Jun 12, 2013
Spinner sp = (Spinner)findViewById(R.id.spinner); 
sp.setSelection(pos);

here pos is integer (your array item position)

array is like below then pos = 0;

String str[] = new String{"Select Gender","male", "female" };

then in onItemSelected

@Override
    public void onItemSelected(AdapterView<?> main, View view, int position,
            long Id) {

        if(position > 0){
          // get spinner value
        }else{
          // show toast select gender
        }

    }