5

I'm using adapter.add for adding a title in my spinner, but how to hide first item in an android spinner dropdown? Here is My Code:

var spinner1 = FindViewById<Spinner>(Resource.Id.spinner1);
var adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleSpinnerItem);
spinner1.Adapter = adapter;
adapter.Add("Select one...");
adapter.Add("Name");
adapter.Add("Mobile");
adapter.Add("Age");

See images below:

enter image description here enter image description here

DiH
  • 331
  • 2
  • 7
  • 17

1 Answers1

14

Apply This code This code is Working Fine On my Device

List<String> list = new ArrayList<String>();
list.add("string1");
list.add("string2");
list.add("string3");
list.add("[Select one]");
final int listsize = list.size() - 1;

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list) {
    @Override
    public int getCount() {
        return(listsize); // Truncate the list
    }
};

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(dataAdapter);
mySpinner.setSelection(listsize); 
Riyaz Parasara
  • 1,296
  • 12
  • 24
  • This also works with xamarin?Is there any other way for adding title in my spinner? Easiest from my way? – DiH Aug 12 '16 at 14:30
  • You can use any native code in Xamarin too, just need to use the corresponding c# API. Which is usually the camel case version of native method. – Rohit Vipin Mathews Aug 12 '16 at 18:38
  • Works great for short lists. For long lists scrolls to bottom. – Krzysztof Dziuba Nov 22 '17 at 08:52
  • It is working BUUT I have one issue that if we use this technique then dropdown list will be shown from bottom not from top. I want to use this method but dropdown list from top. can you help ? – F_Z Dec 02 '20 at 13:07