I am using an AlertDialog (see the below code) and would like to put an image before each text.
For example, email icon then text "Email", Facebook icon then text "Facebook", etc.
Using the following code, how to add an icon before each text value?
final CharSequence[] items = { "Email", "Facebook", "Twitter", "LinkedIn" };
AlertDialog.Builder builder = new AlertDialog.Builder(More.this);
builder.setTitle("Share Appliction");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (item == 0) {
} else if (item == 1) {
} else if (item == 2) {
} else if(item == 3) {
}
}
});
AlertDialog alert = builder.create();
alert.show();
I like Tom Esterez's solution but recommend using the relative function instead for RTL support.
So use this:
instead of this:
You need custom
ListAdapter
to add your image. One way is to subclass theArrayAdapter
(used by default by theAlertDialog
). Here is an example:Here is the Item class
Do something like this:
Total idea is to create layout/viewgroup and add icon+text+whatever you want into viewgroup
To scale images: