In this tutorial, I’m going to teach you how to use Android Context Menu and GridView. We’re going to creating a simple wallpaper application that will allow the users to choose and apply a wallpaper in our gallery.

What are Context Menus?

Context menus are associated with views and are triggered by performing a long click in a view for a certain period of time.


How to use Context Menus?

  1. Declare and initialize a view component. (Buttons, ImageView, TextView  and etc..)
  2. Register the view for context menu by using registerForContextMenu(view).
  3. Override onCreateContextMenu() inside the activity.
  4. Call the method add() of class ContextMenu.


What are GridViews?

Gridview is a group of view that displays items in a 2-dimensional, scroll-able grid. The grid items are inserted to the layout using an adapter.


Now let’s start creating our app by creating a new blank project in Android Studio. First we need to add a permission that will allow our app to change the device’s current wallpaper. Open AndroidManifest.xml and add this permission.


Download the images that we will use in our galery HERE and then copy and paste them in your drawable folder.


Create a new activity that will handle the full image preview. Then create a new class that we’ll use as our adapter. After this, you should now have three classes in your java folder.


Now open and add the codes provided below. We need to extend BaseAdapter so we can override the methods used in an adapter.

This class will serve as the adapter of our gridview. The getView method will be called every time we’re going to insert an item in our GridView.


Now open activity_image_review.xml and add a full size ImageView. This activity will show the selected image in full screen.


Then open and add the codes provided below.

In this activity, we’re just simply getting the resource id inside the intent pass by the previous activity. Then we will use that resource id to set the selected image in our ImageView.


Now open activity_main.xml and add a full size GridView.


Then open and add the codes provided below.


As I said earlier, there are steps in order to use the context menu. First we declare and initialize our GridView then we register it by using registerForContextMenu(view). Next we override onCreateContextMenu() and add the items to be shown in our context menu. Lastly we need to override onContextItemSelected() in order to add a function that will be triggered when a user choose an item inside the context menu.

Now you can run and test you app if it is working. The final output of this tutorial should be like this.








You need to perform a long click on an item to show the context menu.

If you’re having trouble reaching up to this point, you can download the full source code here: Download Link