0

I am using a dropdown menu and i put inside it only a strings but i want when i click on the action bar on the drop down menu widget that when the list of choices drops i will see not only strings but pictures for each choice , i tried to look it up but i dont find something relevant on how doing it. Example for what i am talking about is this http://2.bp.blogspot.com/-mVQddiSMz0I/TzL2y2Ny5uI/AAAAAAAABKo/ikNPtWiPu4w/s1600/Screenshot_2012-02-08-14-01-21.png So can you give me an example and explanation on doing it please? Thanks

user2202255
  • 21
  • 3
  • 5

5 Answers5

0

Use ActionBar, see this documentation:

Adding an Action Provider

The ShareActionProvider is an extension of ActionProvider that facilitates a “share" action by showing a list of available share targets from the action bar.

enter image description here

With ActionProvider you can add menu and you can create custom your menu item. Here have how to create menu item xml documentation:

Menu Resource

And see example: menu with photo

And here have how to add icon stream to inten for ShareIntent see: ShareActionProvider with one icon - looking as simple actionitem

Community
  • 1
  • 1
SBotirov
  • 13,872
  • 7
  • 59
  • 81
0

Your screenshot is of a ListPopupWindow as used by a ShareActionProvider.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

If you don't need to use a ShareActionProvider, but just a Spinner with pictures and text, you can add submenus to your actionbar as shown here: https://stackoverflow.com/a/8312985/626787

Or you can add a Spinner as an action and fill it with a CustomAdapter.

Community
  • 1
  • 1
mbarrben
  • 372
  • 3
  • 9
0

As @mbarrben said you do not need ShareActionProvider. You only need a Spinner and implemet a CustomAdapter. Below you can see a simple example(Full example here

On your activity you should have that:

public class SpinnerIconActivity extends SherlockActivity implements OnNavigationListener{

private SpinnerAdapter mSpinnerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_spinner_icon);

    ActionBar actionBar = getSupportActionBar();
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);


    List<SpinnerRow> options = new ArrayList<SpinnerRow>();
    options.add(new SpinnerRow("Droid 1", R.drawable.android01));
    options.add(new SpinnerRow("Droid 2", R.drawable.android02));
    options.add(new SpinnerRow("Droid 3", R.drawable.android03));
    mSpinnerAdapter  = new MySpinnerAdapter(getApplicationContext(),
            R.layout.spinner_row, R.id.text, options);

        actionBar.setListNavigationCallbacks(mSpinnerAdapter, this);
   }

   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
        getSupportMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
   }

   @Override
   public boolean onNavigationItemSelected(int itemPosition, long itemId) {
    return false;
   }

}

The most important thing is write your custom adapter. I choosed use ArrayAdapter because using it i just need to override one method.

The approach to write a customAdapter is the same to create customizable ListView with one basic difference, instead to override the getView method, you must override getDropDownView.

So, below is my example:

public class MySpinnerAdapter extends ArrayAdapter<SpinnerRow> {

private List<SpinnerRow> rows;
private int resource;

public MySpinnerAdapter(Context context, int resource,
        int textViewResourceId, List<SpinnerRow> objects) {
    super(context, resource, textViewResourceId, objects);
    rows = objects;
    this.resource = resource;
}

static class ViewHolder {

    TextView text;
    ImageView icon;
}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
    ViewHolder  holder = new ViewHolder();
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(resource,
                parent, false);

        holder.text = (TextView) convertView.findViewById(R.id.text);
        holder.icon = (ImageView) convertView.findViewById(R.id.icon);

        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    SpinnerRow currentRow = rows.get(position);
    holder.text.setText(currentRow.toString());
    holder.icon.setImageDrawable(
            getContext().getResources().getDrawable(currentRow.getIconResourceId()));


    return convertView;
}

}
Bruno Mateus
  • 1,727
  • 18
  • 25
0

Refer the link which is provided by google => http://developer.android.com/guide/topics/ui/actionbar.html#ActionProvider

Shubham Arora
  • 167
  • 13