I need that when you deploy Spinner 1st item "Select ..." is hidden or removed from the array tied to Spinner .
I tried to set the visability of GONE to the 1st site - it is not completely hidden, if I return null from the getDropDownView() method, it will getDropDownView() with an error. I tried to track the Spinner open event using View.OnClickListener - also View.OnClickListener with an error:

 java.lang.RuntimeException: Don't call setOnClickListener for an AdapterView. You probably want setOnItemClickListener instead at android.widget.AdapterView.setOnClickListener(AdapterView.java:774) 

    2 answers 2

    The solution is as follows. In the data we create the last item with the inscription, which should be in a closed spinner (in the example, this is select city .. ). Position in the spinner translate this point. Now in our closed state everything is displayed as it should.

    MainActivity ^

      public class MainActivity extends AppCompatActivity { Spinner spinner; private String[] city = { "Moscow", "New York", "Berlin", "Select city .." }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); spinner = (Spinner)findViewById(R.id.spinner); CustomAdapter adapter = new CustomAdapter(this, android.R.layout.simple_spinner_item, city); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setSelection(city.length - 1); } } 

    Now, to hide this item when expanding the list, simply reduce the item count in the adapter by one by overriding the getCount() method

    CustomAdapter:

     public class CustomAdapter extends ArrayAdapter { String [] city; public CustomAdapter(Context context, int resourceId, String[] city) { super(context, resourceId, city); this.city = city; } @Override public int getCount() { return city.length - 1; } } 

    You can also hide the first item, but this does not have to be done, since such a decision will complicate the logic, which is due to the fact that there will be a discrepancy between the list positions and the positions in the spinner, because although the item is hidden, it remains an index.
    That is, it turns out that for the position of the city[1] data, the spinner will return position = 0 . When placing the starting point at the end of the list, we avoid this problem. In addition, the solution is much simpler and requires much less code.

    • When you rotate the screen, the element "Select .." changes to the last but one ((( - Real KEK
    • @ Falcons should not be like this. - pavlofff

    The working solution is similar to this answer , but instead of overriding getCount() , we simply hide the starting item with setVisibility() and setHeight() . Starting point is placed at the end of the list.

     class CustomAdapter<Str> extends ArrayAdapter<Str> { private final Context cntxt; private final Str[] objects; CustomAdapter(Context cntxt, int resource, @NonNull Str[] objects) { super(cntxt, resource, objects); this.cntxt = cntxt; this.objects = objects; } @Override public View getView(int pos, View convertView, ViewGroup p) { TextView tv = (TextView) super.getView(pos, convertView, p); tv.setTextColor(pos == objects.length - 1 ? ContextCompat.getColor(cntxt, R.color.spinnerTxtColor) : Color.BLACK); return tv; } public View getDropDownView(int pos, View convertView, ViewGroup p) { if (pos == objects.length - 1) { TextView tv = new TextView(getContext()); tv.setVisibility(GONE); tv.setHeight(0); return tv; } else return super.getDropDownView(pos, null, p); } } 

    When initializing Spinner, install the last item:

     spinner.setSelection(city.length - 1);