Search code examples
androidspinnerandroid-spinneralternate

Alternative to HIGHLY FLAWED SPINNER CLASS in Android


in my current project i have dealt with spinner class which has been customized from almost every aspect possible. Thus i have gained some detailed aspects as i deal with it . So i will start with the part it shows why it is flawed.

1_There is no default listener that will be fired on pop up window/layout/dialog created-showed(layout inflated) event. There are some workarounds such as ontouch listener on spinner, then check if on touch finish happened in spinner area, then you know popup will be shown but still not reliable since you can fill popup with async task..

2_On item selected event does not fire when same index is selected again. This is really annoying since i may be updating the adapter depending on other conditions which will change current selection and list order etc... Of course there is a workaround way by creating own spinner class and adding it in xml like com.myproject.customspinner etc.....(Spinner : onItemSelected not called when selected item remains the same)

3_There is no working functional OnClickListener and OnItemLongTouchListener event for spinner.

4_Changing Spinner DropDown list divider element's attributes such as color requires more labor than changing all dropdrown and spinner' background views itself which is very absurd.

5_Spinner the name itself is very absurd =))).

So what can i use instead of Spinner? Which is best way to go?


Solution

  • You can create a custom spinner using ListPopupWindow to a TextView means when a TextView is clicked a ListPopupWindow open like spinner dropdown list and you can choose a element. If you need I will help you in that.

    ListPopupWindow numberList;
    TextView spDays;
    ArrayList<Map<String, String>>() listTrans;
    
    in oncreate() spDays.setonclicklistner(this);spDays.setText("Select");
    setNumberListSpinnerView();
    
    in onclick(){
    when spDays clicked :- numberList.show();
    }
    
    
    void setNumberListSpinnerView() {
    
    numberList= new ListPopupWindow(this);
    numberList.setAnchorView(spDays);
    
    numberList.setOnItemClickListener((new AdapterView.OnItemClickListener() {
        @Override
        getListItem();
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Map map = listTrans.get(position);
            spDays.setText(map.get("circle_name").toString());
            circle_name = map.get("circle_name") + "";
            circle_id = map.get("circle_id").toString();
            circleList.dismiss();
            Log.d("Circle id:", circle_id + "");
            getRetails();
    
        }
    }));
    }
    
    
    void getListItem(){
    String[] numbers = {"1","2","3","4","5","6"};
    listTrans = new ArrayList<Map<String, String>>();
    LinkedHashMap<String, String> tran = new LinkedHashMap<String, String>();
    for (String number : numbers) {
        tran.put("numbers", number);
        listTrans.add(tran);
    }
    SimpleAdapter adapter = new SimpleAdapter(AddRetailSurvey.this, listTrans,
            android.R.layout.simple_spinner_dropdown_item,
            new String[]{"numbers"},
            new int[]{android.R.id.text1});
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    numberList.setAdapter(adapter);
    }
    

    Check this code and modify it according to your requirement. If you found any problem I am here to help you. :)