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.
You can set prompt to the spinner ... that can be set in the xml with android:prompt="Gender"
Spinner don't support Hint, i recommend you to make a custom spinner adapter.
check this link : https://stackoverflow.com/a/13878692/1725748
I found a solution by extending
ArrayAdapter
and Overriding thegetView
method.What Android does when initialising the Spinner, is calling getView for the selected item before calling getView for all items in
T[] objects
. TheSpinnerAdapterWithInitialText
returns aTextView
with theinitialText
, the first time it is called. All the other times it callssuper.getView
which is thegetView
method ofArrayAdapter
which is called if you are using the Spinner normally.To find out whether the user has selected a spinner item, or if the spinner still displays the
initialText
, callselectionMade
and hand over the spinner the adapter is assigned to.here pos is integer (your array item position)
array is like below then
pos = 0;
then in onItemSelected
Try below: