-2

Spinner item initially text value should be empty so i added empty vaue in spinner item but it shows the first spinner item is empty .please rectify my mistake which i have done

Required

Initially spinner should be empty

when selected spinner it should show only two items

spinner.java

LoginStatus_List = new ArrayList<String>();
        LoginStatus_List.add("");
        LoginStatus_List.add("Approve");
        LoginStatus_List.add("Reject");

        ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
        dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        spnrLognStatus.setAdapter(dataAdapter1);
        spnrLognStatus.setSelection(0);
swapna
  • 31
  • 9

2 Answers2

0

You have to create a costum SpinnerAdapter to achieve this.

Try this

You can set the first spinneritem empty: " ". But in this simple solution the user is still able to select the displayed empty spinneritem.

Community
  • 1
  • 1
TheOnlyJakobob
  • 541
  • 3
  • 14
  • if i add empty in the spinner it is showing the first spinner item empty but i want the empty spinner couldnt show in the spinner item list – swapna Jun 11 '14 at 07:18
  • then you have to create a costum spinner adapter. Follow this post for more help. Its pretty simple to understand: http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one – TheOnlyJakobob Jun 11 '14 at 07:19
0

You can add a kind of "title" into your spinner and ignore it if the user select it. It's not exactly an empty line but the title will act as an hint to guide the user.

private static final String TITLE ="Select the action";
...

ArrayList<String> LoginStatus_List = new ArrayList<String>();
LoginStatus_List.add(TITLE);
LoginStatus_List.add("Approve");
LoginStatus_List.add("Reject");

ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item,LoginStatus_List);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spnrLognStatus.setAdapter(dataAdapter1);
spnrLognStatus.setSelection(0);

spnrLognStatus.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        String selection = (String)spnrLognStatus.getSelectedItem();
        if(TITLE.equalsIgnoreCase(selection)){
            // Ignore the selected title
        }else{
            // Do want you want here
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }
});
Guillaume Barré
  • 4,168
  • 2
  • 27
  • 50