Create Options Menu for RecyclerView Item Tutorial

Hello guys, today we will learn about creating options menu for RecyclerView Item in Android. You may have seen this in many apps. Sometimes you need to open an Options Menu for every item in the List. For creating the list we will use RecyclerView. I already posted some RecyclerView Tutorials. This time we will add an option menu to the items. So lets start.

Creating an Android Studio Project

  • As we always do create a new Android Studio Project.
  • Select Empty Activity and move ahead.
  • As usual after project is loaded in Android Studio we will get MainActivity.java and activity_main.xml files (If you haven’t changed the names while creating projects).

Learn Everything About RecyclerView in this Video

  • If you are not familiar with RecyclerView then you can learn it from Scratch in this playlist. The playlist covers everything from beginning to advanced level.

  • If you don’t want to learn with video then leave it and lets move ahead on this tutorial.

Adding RecyclerView and CardView

  • For this List we will use RecyclerView and for the ListItems we will use CardView. Both the components are not available by default. So first we need to add these.
  • Go to File -> Project Structure -> app -> Dependencies, then click on the + icon from below and select Library Dependency.

adding recyclerview

  • Now find ReyclerView and CardView from the list and add them both.

recyclerview

Creating RecyclerView

  • Now we will create a RecyclerView, so come inside activity_main.xml and create a RecyclerView with the below given code.

Designing List Items

  • Now its time to design our List Items. So create a new layout resource file inside layout folder. I have created list_items.xml.  Copy the following code inside this file, we are using CardView here to design the list item.

  • The above code will generate the following ListItem.

options for recyclerview item

Creating Menu Items

  • Now we need the menu item so first create a new directory inside res and name it menu.
  • Inside this menu directory create a menu resource file named options_menu.xml.  And create the menus you want inside this file. See the following code.

Creating List Model and Adapter

  • Now lets come to the coding part. First we will create a Model for our RecyclerView. So create a java file named MyList.java. 

  • Now its time to create our RecyclerView Adapter. So create a java file named CustomAdapter and write the following code.

Adding Items to RecyclerView

  • Now lets add some items to our RecyclerView. So come inside MainActivity.java and write the following code.

  • Now run the application and you should see the following output.

options menu for recyclerview

  • So by now we have our nice looking RecyclerView. Now we need to create Options Menu for RecyclerView Items. You can see the three vertical dots in every item. We will option the option on the click over this three vertical dot.

Creating Options Menu for RecyclerView Item

  • Now the last phase of this tutorial is creating Options Menu for RecyclerView Item. So again come inside CustomAdapter.java. And inside onClick() method where we written a comment that will show popup menu here, write the following code.

  • So the final code of the CustomAdapter.java after adding the Options Menu for RecyclerView Item will be.

  • Now run the application.

options menu for recyclerview item

  • Bingo! Its working absolutely fine.
  • Still facing problems? Don’t worry you can get my source code from github repository, just go to the link given below.

So thats all for this Options Menu For RecyclerView Tutorial friends. If you are still facing troubles or have any query please don’t hesitate to leave your comments. Thank You 🙂

19 thoughts on “Create Options Menu for RecyclerView Item Tutorial”

  1. VERY NICE TUTORIAL. HELPED ME ALOT TO UNDERSTAND RECYCLER VIEW..EASY AND VERY WELL DESCRIBED TUTORIAL…LOVED IT..HATS OFF TO U GUYS..KEEP GOING..

    Reply
  2. How do I update the content of the cardview once in 24 hours? Need just one cardview on the main activity then I want to update the contents once daily.

    Reply
  3. Hi there, i have a problem with my listview. it is not appearing one after the other. i have to scroll down to get the other one. Can you tell me where i have gone wrong?

    Reply
  4. TypedValue{t=0x3d=0x3ee “res/drawable/ic_menu more overflow material.xml” a=1 r=0x10803d6}
    This error i am getting in my project when i am used pop menu code in adapter. When i run the app it showing unfortunatly stop becoz of the adove error.

    Reply
  5. android:layout_width=”wrap_content” not give perfect click for me
    i use this insted
    android:layout_width=”40dp”
    android:gravity=”center”

    Reply
  6. Hello,

    I wish to create Recyclerview with Floating button.

    In detail, recyclerview’s each cell will contain an imageview & 1 floating action button. On click floating action button, circular floating action menu will appear.
    I tried this using a library of github ie. https://github.com/oguzbilgener/CircularFloatingActionMenu library.
    I stuck,where, when I click on 1st item floating button, circular floating menu get visible of 1st cell which is correct . But at the same time when I scroll down, last cell’s circular floating menu is also opened.
    I request your guidance, regarding Recyclerview with floating action button & particularly how to handle clicked cell position of resp floating button inside recyclerview.
    Thanks in advance.

    Reply

Leave a Comment