0

Please tell me how can I hide the item at index 0 from the dropdown in the spinner in Android Studio? I am using this code, it works, but when I open the list, it shows at the bottom. That is, it is focused on the elements below. what do i need to change?

SpinnerName = (Spinner) v.findViewById(R.id.spinner1);

        ArrayList<String> names = new ArrayList<>();

        names.add(0, "SELECT");
        names.add(1, "Name1");
        names.add(2, "Name2");
        

       final int listsize = names.size()-1;

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_spinner_item, names){
            @Override
            public int getCount() {
                return(listsize); 
            }
        };

        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        SpinnerName.setAdapter(adapter);
        adapter.setDropDownViewResource(R.layout.spinner_list);
        SpinnerName.setSelection(listsize);
       
       
        SpinnerName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int pos,
                                       long id) {

                ....

            }

            @Override
            public void onNothingSelected(AdapterView<?> arg0) {
            }
        });
Syed Rafaqat Hussain
  • 1,009
  • 1
  • 9
  • 33
luska
  • 23
  • 6
  • Why don't you just use a list of the correct elements? – Henry Twist Apr 21 '21 at 11:46
  • @HenryTwist I need to display "Select.." sentence, if spinner is closed. – luska Apr 21 '21 at 11:53
  • this is already answered here https://stackoverflow.com/questions/37019941/how-to-add-a-hint-in-spinner-in-xml and https://stackoverflow.com/questions/49508512/how-to-add-a-hint-in-spinner/49508686 – CSmith Apr 21 '21 at 12:03

1 Answers1

0
SpinnerName.setSelection(listsize);

your problem is here! you are passing the total list size number and that where you are doing wrong, because setSelection method use to show default index of spinner.

you can simply do that SpinnerName.setSelection(1); that would give you the fist spinner item unless showing the names.add(0, "SELECT"); item