7

Hi I am trying to create a layout like this using support library https://developer.android.com/reference/android/support/v7/widget/CardView.html but i didn't found a way to add overflow icon in a card.

I don't want to use this library https://github.com/gabrielemariotti/cardslib/blob/master/doc/CARDGRID.md. I want to do same using support library that Google introduced recently.

Isn't there is way to achieve using support library or I have to use gabrielemariotti library to add overflow items in a card view.

enter image description here

Update

Guys I have edited question now it is more clear what i want.

N Sharma
  • 33,489
  • 95
  • 256
  • 444
  • "i didn't found a way to do so" -- what did you try? What problems did you encounter? – CommonsWare Oct 29 '14 at 11:05
  • @CommonsWare I tried to see post on internet but didn't find anything to do using support lib – N Sharma Oct 29 '14 at 11:09
  • Did you use a `CardView` as the container around your cell content in your `Adapter` for your `GridView`? If not, why are you asking here before even trying it yourself? – CommonsWare Oct 29 '14 at 11:11
  • @CommonsWare no you didn't get my question. My Question was not to use card view using grid. I wanted to ask how to make views such like there is arrow at the bottom, on click on it user can install that app – N Sharma Oct 29 '14 at 11:13
  • "My Question was not to use card view using grid" -- the title of your question is "How to create card grid view layout". "how to make views such like there is arrow at the bottom" -- there is no arrow in the screenshot. – CommonsWare Oct 29 '14 at 11:16
  • @CommonsWare see at the bottom in the red rectangle there is overflow icon. – N Sharma Oct 29 '14 at 11:20

2 Answers2

17

First of all you shouldn't use this lib only to achieve an overflow menu inside a card.

Said that,the CardView in support Library is a FrameLayout with any model behind. The best way to achieve it, now is to use the new Toolbar inside the CardView layout.

Also you can add a ImageView inside the CardView layout and do something like this:

PopupMenu popup = new PopupMenu(getContext(), mImageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.your_menu, popup.getMenu());

Finally (but it is not so important) if it isn't enough, the new cardslib (coming soon) will use the CardView in support library.

Aphex
  • 7,390
  • 5
  • 33
  • 54
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
  • 1
    The parameters for the `inflater.inflate()` function is `R.menu.your_menu_res` which is nothing but the id of the xml menu you want to inflate, and the you need to pass `popup.getMenu()` as the 2nd paramter – Bhargav Jan 21 '16 at 10:46
4

Here's how to do it correctly in an android.support.v7.widget.CardView. In my example, it's a cardview inside of a RecyclerView, so I set this up in the adapter, inside the onBindViewHolder method:

public class DashboardItemAdapter extends RecyclerView.Adapter<DashboardItemAdapter.ViewHolder> {
    ArrayList<Item> mItems = new ArrayList<>();
    private Context mContext;

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        mContext = parent.getContext();
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_dashboard, parent, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder vh, int position) {
        final Item item = mItems.get(position);

        ...

        PopupMenu popup = new PopupMenu(mContext, vh.btnMenu);
        popup.inflate(R.menu.dashboard_card_waiting);
        popup.setOnMenuItemClickListener(item -> {
            switch (item.getItemId()) {
                case R.id.action_cancel:
                    cancelItem();
                    return true;
                default:
                    return true;
            }
        });
        vh.btnMenu.setOnClickListener(btn -> popup.show());
    }

    ...
}
Aphex
  • 7,390
  • 5
  • 33
  • 54