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(); } |