-3

In Recycler View I click on a button add to cart a dialog is open. In Recycler view adapter

@Override
public void onBindViewHolder(final ViewHolder holder, int position) {

    holder.addtocart.setOnClickListener((Mp3HindiLandingActivity) context);
}

In My Activity(Mp3HindiLandingActivity)

          @Override
         public void onClick(final View v) {
    switch (v.getId()) { case R.id.addtocart:
            PopupMenu popup = new PopupMenu(ctx, v);
            popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    switch (item.getItemId()) {
                        case R.id.menu1:

                            Toast.makeText(v.getContext(), "FriendRequest", Toast.LENGTH_LONG).show();
                            return true;
                        case R.id.menu2:
                            Toast.makeText(v.getContext(), "Block | Hide ", Toast.LENGTH_LONG).show();
                            return true;
                        default:
                            return false;
                    }
                }
            });
            popup.getMenuInflater().inflate(R.menu.menu, popup.getMenu());
            popup.show();

            break;`
           }}

It Shows the the menu below addtocart icon but I need the menu items having different background as per the image attached?

Please let me know how this will be implemented Please Refer Image

Yash Bhardwaj
  • 57
  • 2
  • 6
  • Give us your soucecode and we can find a solution. But just posting a problem and assuming, that anyone will code for you is not the way we will do it here –  Jul 11 '16 at 10:41

2 Answers2

7

you can try this.

paletteViewHolder.btncart.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View v) {
                    PopupMenu popup = new PopupMenu(paletteViewHolder.imgpopup.getContext(), v);
                    // This activity implements OnMenuItemClickListener .
                    //popup.setOnMenuItemClickListener ((OnMenuItemClickListener) this);
                    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem item) {
                            switch (item.getItemId()) {
                                case R.id.friendrequest:
                                    Toast.makeText(v.getContext(), "FriendRequest", Toast.LENGTH_LONG).show();
                                    return true;
                                case R.id.blockhide:
                                    Toast.makeText(v.getContext(), "Block | Hide ", Toast.LENGTH_LONG).show();
                                    return true;
                                case R.id.followunfollow:
                                    Toast.makeText(v.getContext(), "Follow/UnFollow", Toast.LENGTH_LONG).show();
                                    return true;
                                default:
                                    return false;
                            }
                            //return false;
                        }
                    });
                    popup.inflate(R.menu.menu);
                    popup.show();
                }
            });

//create below menu.xml file on menu

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/menu1"
        android:title="menu1"/>
    <item
        android:id="@+id/menu2"
        android:title="menu2"/>
    <item
        android:id="@+id/menu3"
        android:title="menu3"/>
</menu>

its helpfull for you.

Atul Mavani
  • 1,271
  • 1
  • 7
  • 11
2

First create an inner class ViewHolder likes below:

private class MyViewHolder extends RecyclerView.ViewHolder {
    public Button btnCart;

    public MyViewHolder(View view) {
        super(view);
        btnCart = (Button) view.findViewById(R.id.btnCart);
    }
}

Then override onBindViewHolder in your adapter (your adapter extends RecyclerView.Adapter).

@Override
    public void onBindViewHolder(final MyViewHolder holder, int position) {

        holder.btnCart.setOnClickListener(new View.OnClickListener() {
           public void onClick(View v) {
               // call the opening of your dialog here
           }
        });

    }
JackLametta
  • 390
  • 1
  • 2
  • 18
Dang Nguyen
  • 324
  • 2
  • 8
  • 1
    i m implementing the same thing here .. i know how to make click event,, what i want to know is how to make that custom dialog with image. like in image shown above.. – Sagar Chavada Jul 11 '16 at 10:49
  • Dang Nguyen I tried it but dialog is shown top of the screen, but according to functionality the dialog should be below of cart icon. It should be for each and every row – Yash Bhardwaj Jul 11 '16 at 11:30
  • Create a popup menu as @Atul Mavani's help will work. You can get more details about how to make popup menu with icons here: http://stackoverflow.com/questions/15454995/popupmenu-with-icons – Dang Nguyen Jul 12 '16 at 02:23