This came as odd to me. From within an Activity class, you can create a context menu through the onCreateContextMenu() and capture those events and process code in the onOptionsItemSelected() method.

Since I was extending an existing View class, it does have its own onCreateContextMenu() but with only one parameter needed with is ContextMenu.

Here is the problem then. There is no onOptionsItemSelected() for the View class but only for Activity.

How then can events of the context menu items be captured? The answer is none that you can just override some method directly.

What I did though, was traversing each MenuItem in the Menu and adding an OnMenuItemClickListener to each one. That did the trick.

Here is a sample code.

By the way, make sure you register your view that will show the context menu by doing this:

I thought that creating a custom method in my Activity class where I can change the visibility of a MenuItem and accessing it from the Fragment would work. Sadly, it did not.

It seems that the Fragment class has a method called setHasOptionsMenu(boolean) and you need to call that from within the onCreateView() method in order to be able to manipulate MenuItems from within onCreateOptionsMenu().

So this is how a sample Fragment class will look like.

This code looks for the MenuItem and hides it.

The answer to that is there is no way. However, an open source library created by Niek Haarman proved quite useful and a good alternative to be able to display tooltips when you click on a MenuItem in the ActionBar.

Take a look at this screenshot.

actionbar_menuItem_tooltip
And this is how I did it.

The tooltipView variable and my custom made addTooltip() method contain the same code that is found in the sample source provided by Niek’s library. That’s it! Simple, right?

However, we need to do a few tweaks because the current library does not provide margins on both left and right sides so if your text is pretty long, your tooltip will take the whole screen width and that would make it look unappealing, right?

The workaround to that is to modify the tooltip.xml of the library and add the attribute android:layout_marginRight=”10dp” in the main layout container which is RelativeLayout.

The next thing to do is to modify the ToolTipView class to set certain width when the app is viewed in a large screen device. Even if we have margins on both left and right, a very long horizontal tooltip still does not look nice, right? There should at least be an estimated maximum width limit and here is how I did it.

in the ToolTipView class there is a method called init(). Change that and use this instead.

Looking at the code, you will notice I added checking to see if the device has a large screen or not and if its orientation is in portrait or landscape mode. This is equally important because I do not want my tooltips’ width to fill the horizontal screen area, only limiting it to a certain width.

In this case, if it is a tablet under landscape orientation, I make sure the maximum size will be 1/3 or the horizontal screen area.

If it is only a smart phone, I only take into account its landscape orientation and set a maximum width limit of half the size of the horizontal screen area.

If your app takes into account orientation changes, you need to make sure your tooltip points to the correct MenuItem. This is how I did it.

Also, a very important thing to note is that since we are dealing with the ActionBar’s MenuItem, you could be using a DrawerLayout as the main container. Do not place the tooltip layout there. Based on the sample XML layout code of the supertooltips library, place your DrawerLayout in the area where the comment line “Rest of Layout” is located.

If you place the tooltip tag in your DrawerLayout, chances are your left menu will not work because the DrawerLayout only accepts 2 children, the left menu and the content area.

There! Now you got yourself a tooltip when an ActionBar MenuItem is clicked.

Related Posts Plugin for WordPress, Blogger...