Advertisement
Tech

How to create Advanced Menus in our Google Android Application

In this article we will see how to create a more advanced menu within our applications. To do this we will learn how to create sub-menus and context menus.

By Jbeerdev
Desk Tech
Reading time 3 min read
Word count 535
Google android Mobile Guides
How to create Advanced Menus in our Google Android Application
Advertisement
Quick Take

In this article we will see how to create a more advanced menu within our applications. To do this we will learn how to create sub-menus and context menus.

On this page

Context Menu

Context menus are similar to right click menus on desktop operating systems. These menus are related with a view in the layout-tree. So imagine that you have an image on the screen (an image with ImageView), you then long press the image (long-press → about 2 or 3 seconds) and then a menu appears with different options: “Edit”, “Delete”, “Change Name”… basically whatever you want to output, of course this is just an example so lets develop the idea a bit further.

Let’s start by creating a View in our onCreate() Activity method.

Advertisement

ImageView mImage = new ImageView(this);

We can create a view in this way, or we can create it using this:

Advertisement

mImage = (ImageView) findViewById(R.id.imageName);

(Once we have set the ContentView to the Activity with setContentView(R.layout_.mainView__)_; )

Advertisement

Now, we are going to add a content menu to this ImageView, for that we use the following method.

registerForContextMenu(mImage);

Advertisement

or we can directly call the view using:

registerForContextMenu(findViewById(R.id.imageName));

Advertisement

Luckily If we use this last line, we don`t need to create an ImageView object.

Once we have done this. We need to override a pair of methods in the Activity.

Advertisement

One of them is:

onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo){

Advertisement

super.onCreateContextMenu(menu, v, menuInfo);

menu.add(0, EDIT_IMAGE, 0, “Edit Image”);

Advertisement

menu.add(0, CHANGE_NAME, 0, “Change name”);

}

Advertisement

With the .add method, we add options to the menu. In this case, we only have a context menu, but if we would like to add more of these menus to others views, we have to be careful with the onCreateContextMenu method, because we will need to filter by view:

An example in pseudo-code:

Advertisement

if(view) is “TextView”:

do A

Advertisement

else if (view) is “ImageView”

do B

Advertisement

….

Remember that we can identify every view with an unique ID.

Advertisement

Now we have to add functionality to every menu option we have created. This is done with the “onContextItemSelected” method.

public boolean onContextItemSelected (MenuItem item) {

switch (item.getItemId()){

case EDIT_IMAGE:

editImage();

return true;

case CHANGE_NAME:

changeName();

return true;

default:

return super.onContextItemSelected(Item);

}

}

Sometimes we have too much to add in the menus or we just want to order our options-menus in topics. To do this, we just need to add the following structure in the onCreateOptionsMenu method.

SubMenu toolsMenu = menu.addSubMenu(“Tools”);

SubMenu helpMenu = menu.addSubMenu(“Help”);

toolsMenu.add(“languaje”);

toolsMenu.add(“Settings”);

toolsMenu.add(“Format”);

toolsMenu.add(“Table”);

toolsMenu.add(“Font”);

helpMenu.add(“Help”);

helpMenu.add(“Update”);

helpMenu.add(“Update”);

Creating sub-menus in XML is very simple too, just we have to add the tag “menu” inside of the “item” tag we want to put as submenu as its shown in this piece of code.

<menu xmlns: android:“https://schemas.android.com/apk/res/android">

<item android:title:“Option 1” />

<item android:title:“Option 2” />

Follow up

If you want to know when new articles are released, subscribe yourself to the Google Android RSS Otherwise, you can follow my research, articles and work in my professional twitter: jbeerdev

This post is part of the series: How to- Develop Applications on Android: More Functionality

This time we will learn more advanced functionality to add to our Android Applications.

  1. Guide to Creating Android App Menus
  2. How to create Advanced Menus Via Our Google Android Applications
  3. Guide to Using Basic Content Providers in Android Development
  4. Dev Guide to Advanced Content Providers on Android
  5. How to interact with Android Events
Keep Exploring

More from Tech

Filed under
Google android Mobile
More topics
Guides
Advertisement