Toolbar inside CardView to create a popup menu (overflow icon)
Asked Answered
A

3

23

I have a list that looks like google play in a recyclerview with cardview, and works perfect.

I need to add a popup menu (with overflow icon), like this:

which is the best way to do this ?

I researched and found that there are 2 options:

1 - with a toolbar inside the cardview layout. is there a performanece problem with this solution ?

2 - with a imagebutton or imageview with a icon of the overflow, that when you click the menu is created.

I need a solution to be compatible with >= API 10

thanks

Amphicoelous answered 6/1, 2016 at 19:30 Comment(1)
Number 2. An entire toolbar is unecessary, just use either an ImageButton or an ImageView.Sedgewinn
A
24

It depends by your layout.

If you would like a layout like this, with a Toolbar you can achieve it more easily. enter image description here

Somenthing like

<android.support.v7.widget.CardView>

   <LinearLayout>

        <Toolbar  android:id="@+id/card_toolbar" />

        //......

   </LinearLayout>

</CardView>

toolbar.inflateMenu(R.menu.card_toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener(){..});

If you would like only a popup is more simple to use an image. Somenthing like:

PopupMenu popup = new PopupMenu(getContext(), mImageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(......);
Attic answered 7/1, 2016 at 7:52 Comment(3)
I second this solution and I think it should work wellWater
where does the image button come from?Arran
More convenient way: popupMenu.inflate(R.menu.menu_restrictions_section); :)Unnamed
R
48

Step 1 create Popup Menu xml

<?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/Not_interasted_catugury"
        android:orderInCategory="100"
        android:title="Never show stories from this category  " />

    <item
        android:id="@+id/No_interasted"
        android:orderInCategory="101"
        android:title="Not Interested"></item>


</menu>

Step 2: Make a Image button in your card

 <ImageButton
            android:id="@+id/imageButton"
            android:layout_width="20dp"
            android:layout_height="30dp"
            android:src="@drawable/ic_dots"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            android:layout_below="@+id/item_detail"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:background="@null"/>

then give a icon from drawable

Step 3: Inside your holder class

and set item click listner inside onBindViewHolder

mImageButton= (ImageButton) view.findViewById(R.id.imageButton);
holder.mImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showPopupMenu(holder.mImageButton,position);
            }
        });

Step 4: Show pop menu and inflate the xml

  private void showPopupMenu(View view,int position) {
        // inflate menu
        PopupMenu popup = new PopupMenu(view.getContext(),view );
        MenuInflater inflater = popup.getMenuInflater();
        inflater.inflate(R.menu.popup_menu, popup.getMenu());
        popup.setOnMenuItemClickListener(new MyMenuItemClickListener(position));
        popup.show();
    }

Step 5: Implement the OnMenuItemClickListener

     class MyMenuItemClickListener implements PopupMenu.OnMenuItemClickListener {

        private int position;
        public MyMenuItemClickListener(int positon) {
            this.position=positon;
        }

        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            switch (menuItem.getItemId()) {

                case R.id.Not_interasted_catugury:
                    String RemoveCategory=mDataSet.get(position).getCategory();
                    // mDataSet.remove(position);
                    //notifyItemRemoved(position);
                   // notifyItemRangeChanged(position,mDataSet.size());

                    mySharedPreferences.saveStringPrefs(Constants.REMOVE_CTAGURY,RemoveCategory,MainActivity.context);
                    Toast.makeText(MainActivity.context, "Add to favourite", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.No_interasted:
                    mDataSet.remove(position);
                    notifyItemRemoved(position);
                    notifyItemRangeChanged(position,mDataSet.size());
                    Toast.makeText(MainActivity.context, "Done for now", Toast.LENGTH_SHORT).show();
                    return true;
                case R.id.delete:
                    mySharedPreferences.deletePrefs(Constants.REMOVE_CTAGURY,MainActivity.context);
                default:
            }
            return false;
        }
    }
Rhondarhondda answered 13/8, 2016 at 4:33 Comment(2)
Thanks that helped! Great example, well assembled, saved my time!Note
I would advise to use<br>android:background="?android:attr/selectableItemBackground" or android:background="?attr/selectableItemBackground as it will show effect when clicked. Also, if you are using a vector drawable, you should use the following app:srcCompat="@drawable/ic_more_vert_black_24dp" android:tint="@color/secondary_text" to get the right look.Erivan
A
24

It depends by your layout.

If you would like a layout like this, with a Toolbar you can achieve it more easily. enter image description here

Somenthing like

<android.support.v7.widget.CardView>

   <LinearLayout>

        <Toolbar  android:id="@+id/card_toolbar" />

        //......

   </LinearLayout>

</CardView>

toolbar.inflateMenu(R.menu.card_toolbar);
toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener(){..});

If you would like only a popup is more simple to use an image. Somenthing like:

PopupMenu popup = new PopupMenu(getContext(), mImageButton);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(......);
Attic answered 7/1, 2016 at 7:52 Comment(3)
I second this solution and I think it should work wellWater
where does the image button come from?Arran
More convenient way: popupMenu.inflate(R.menu.menu_restrictions_section); :)Unnamed
L
1

This is how I did it:

list_item: ( res / layout )

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:maxLines="1"
            android:layout_toStartOf="@+id/item"
            android:layout_alignParentStart="true"/>

        <ImageButton
            android:layout_width="20dp"
            android:layout_height="30dp"
            android:id="@+id/options"
            android:src="@drawable/ic_menu"
            android:layout_alignParentEnd="true"
            android:onClick="showOptions"/>    

    </RelativeLayout>

</androidx.cardview.widget.CardView>

popup_menu: ( res / 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/option1"
        android:title="Option1"/>

    <item
        android:id="@+id/option2"
        android:title="Option2"/>
</menu>

MainActivity: (add this function anywhere in MainActivity)

public void showOptions(View view){
    PopupMenu popup = new PopupMenu(MainActivity.this, view);
    popup.getMenuInflater().inflate(R.menu.popup_menu, popup.getMenu());
    popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            CardView view = 
           (CardView) ((ViewGroup) view.getParent()).getParent();
            int position = recyclerView.getChildAdapterPosition(cView);  
            Toast.makeText(getApplicationContext(), 
                        item.toString()+" clicked at position"+position,
                        Toast.LENGTH_SHORT).show();
            return true;
        }
    });
    popup.show();
}
Limassol answered 8/8, 2020 at 11:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.