Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Android Spinner with icons
final Item[] items = {
new Item("Email", android.R.drawable.ic_menu_add),
new Item("Facebook", android.R.drawable.ic_menu_delete),
new Item("...", 0),//no icon for this one
ListAdapter adapter = new ArrayAdapter<Item>(
public View getView(int position, View convertView, ViewGroup parent) {
//User super class to create the View
View v = super.getView(position, convertView, parent);
TextView tv = (TextView)v.findViewById(;
//Put the image on the TextView
tv.setCompoundDrawablesWithIntrinsicBounds(items[position].icon, 0, 0, 0);
//Add margin between image and text (support various screen densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
return v;
new AlertDialog.Builder(this)
.setTitle("Share Appliction")
.setAdapter(adapter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
public static class Item{
public final String text;
public final int icon;
public Item(String text, Integer icon) {
this.text = text;
this.icon = icon;
public String toString() {
return text;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.