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 …

creating options menu for recyclerview item

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.

[sociallocker id=1372] Options Menu for RecyclerView Source Code [/sociallocker]

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 🙂

Hi, my name is Belal Khan and I am a Google Developers Expert (GDE) for Android. The passion of teaching made me create this blog. If you are an Android Developer, or you are learning about Android Development, then I can help you a lot with Simplified Coding.

Expand Your Knowledge: Next Tutorial Picks

0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x