spinner android example
Example 1: spinner android
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List categories = new ArrayList();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
// Creating adapter for spinner
ArrayAdapter dataAdapter = new ArrayAdapter(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
Example 2: how to set list data to android spinner
// setting branch data in manually and load to spBranchName
List branchList = new ArrayList<>();
branchList.add("CHITTAGONG");
branchList.add("DHAKA");
ArrayAdapter branchListAdapter = new ArrayAdapter<>(getActivity(),
android.R.layout.simple_spinner_item, branchList);
branchListAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spBranchName.setAdapter(branchListAdapter);
Example 3: how to set current item for spinner android
val b: List = a.mapIndexed { i, b -> if (b) i else null }.filterNotNull().toList()
spinnerObject.setSelection(b.get(0))