Alternative to HIGHLY FLAWED SPINNER CLASS in Android
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. :)
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!