Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show default value in Spinner in android

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.

like image 827
rahul Avatar asked Jun 12 '13 10:06

rahul


People also ask

How to set default values spinner android?

String myString = "some value"; //the value you want the position for ArrayAdapter myAdap = (ArrayAdapter) mySpinner. getAdapter(); //cast to an ArrayAdapter int spinnerPosition = myAdap. getPosition(myString); //set the default according to value spinner. setSelection(spinnerPosition);

What is spinner in Android with example?

Android Spinner is a view similar to the dropdown list which is used to select one option from the list of options. It provides an easy way to select one item from the list of items and it shows a dropdown list of all values when we click on it.

How do you clear a spinner?

when you select one of your first spinner Items you can use code below in first spinner onItemSelected() method to clear your second spinner. Show activity on this post. You have called spinner2List. clear(); but haven't notified the adapter by using notifyDataSetChanged(); method.


1 Answers

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         }      } 
like image 161
Dhaval Parmar Avatar answered Sep 29 '22 21:09

Dhaval Parmar