Tip : ArrayAdapter in AlertDialog
We can always use ListView or RecyclerView for selection from list of items, but if we have small amount of choices and among those choices we want user to select one, we can use AlertDialog.Builder setAdapter.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
private void showDialog() { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Choose any item"); final List<String> lables = new ArrayList<>(); lables.add("Android Gig"); lables.add("EventBus Library In Android"); lables.add("Android’s Runtime Permission"); lables.add("Part of TextView clickable"); ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, lables); builder.setAdapter(dataAdapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { Toast.makeText(MainActivity.this,"You have selected " + lables.get(which),Toast.LENGTH_LONG).show(); } }); AlertDialog dialog = builder.create(); dialog.show(); } |
Ravi Rupareliya
He is author of Android Gig. He loves to explore new technologies and have worked on Android, React Native, Action on Google and Flutter.
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019