Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternative to HIGHLY FLAWED SPINNER CLASS in Android [closed]

Tags:

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?

like image 364
Mert Serimer Avatar asked Dec 12 '14 09:12

Mert Serimer


People also ask

How can we check whether a spinner is open or not in android?

We can user onClickListner for spinner. Since spinner open when we click(tap) on it. And to chcek where the spinner is open or not we can use onItemSelectedListner callbacks. Tell me if you want further code example.

What are the spinners in Android user interface?

Spinners provide a quick way to select one value from a set. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.


2 Answers

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. :)

like image 78
Biswajit Avatar answered Oct 16 '22 17:10

Biswajit


Putting a simplified kotlin version of the accepted answer here, which may help. At first make a ListPopupWindow member in your Activity or other class-

private val listPopupView by lazy { ListPopupWindow(this) } 

Then initialize it in the onCreate() method-

val dataList = arrayOf("item1", "item2", "item3", "item4") listPopupView.setAdapter(ArrayAdapter(this, android.R.layout.simple_list_item_1, dataList)) listPopupView.setOnItemClickListener { _, _, position, _ ->     selectionTextView.text = dataList[position]     listPopupView.dismiss()     // do other things on selection } listPopupView.anchorView = selectionTextView selectionTextView.setOnClickListener { listPopupView.show() } 

And you are done!

like image 37
Gulshan Avatar answered Oct 16 '22 17:10

Gulshan