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.
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);
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.
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.
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 } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With