Dynamically adding activities to a menu using addIntentOptions in Android

July 27, 2012
By

We know the very famous quote “Do not reinvent the wheel”. This is really apt for Android applications. Because Android facilitates an application to integrate existing activities available in the system effortlessly. There are numerous methods to integrate system activities to an application. One method is to dynamically add required activities to the application’s menu using “addIntentOptions” method of the menu object. Here the required activities are defined using an intent . Then based on this intent, the method “addIntentOptions” fetches the satisfying activities from the system and add each activity as a separate menu item into our application. In this article we will see how to add all the existing web browsers in the system to our application so that, the articles listed in the listview can be opened by a browser.

This application is developed in Eclipse 3.7.2 with ADT plugin ( 20.0.2 ) and Android SDK ( R20.0.1 )


1. Create a new Android Application Project namely “OpenActivityFromMenuItem”

Create new Android Application Project

Figure 1 : Create new Android Application Project


2. Design application launcher

Design the application launcher

Figure 2 : Design the application launcher


3. Create a blank activity

Create a blank activity

Figure 3 : Create a blank activity


4. Enter activity details

Enter MainActivity Details

Figure 4: Enter MainActivity Details


5. Update the contents of the file res/menu/activity_main.xml


<menu xmlns:android="http://schemas.android.com/apk/res/android">
</menu>

Menu items will be added at the runtime.


6. Update the contents of the file src/in/wptrafficanalyzer/openactivityfrommenuitem/MainActivity.java


package in.wptrafficanalyzer.openactivityfrommenuitem;

import android.app.ListActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;

public class MainActivity extends ListActivity {

    String[] articles = new String[] {
        "Floating contextual menu",
        "Contextual Action Bar",
        "Dropdown Navigation in Action Bar",
        "Navigation Tabs in Action Bar"
    };

    String[] article_urls = new String[] {
        "http://wptrafficanalyzer.in/blog/creating-a-floating-contextual-menu-in-android",
        "http://wptrafficanalyzer.in/blog/creating-a-contextual-menu-bar-contextual-action-mode-for-a-listview-in-android",
        "http://wptrafficanalyzer.in/blog/adding-drop-down-navigation-to-action-bar-in-android",
        "http://wptrafficanalyzer.in/blog/adding-navigation-tabs-containing-listview-to-action-bar-in-android"
    };

    @Override
    protected void onStart() {
        super.onStart();
        /** Defining an array adapter to host list of articles */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_list_item_1, articles );

        /** Setting array adapter to the the listview */
        getListView().setAdapter(adapter);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        /** Registering the listview for the contextual menu */
        registerForContextMenu(getListView());
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);

        /** Inflating the menu from activity_main.xml */
        getMenuInflater().inflate(R.menu.activity_main, menu);

        AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;

        /** Creating an intent, based on which the menu items will be populated */
        Intent intent = new Intent(Intent.ACTION_VIEW);

        /** Creating a url object which points to the selected article's web page */
        Uri uri = Uri.parse(article_urls[info.position]);

        intent.setData(uri);

        /** Adding menu items satisfying the intent properties */
        menu.addIntentOptions(
            Menu.NONE,  // Menu group to which new items will be added
            Menu.NONE,          // Unique item ID (none)
            Menu.NONE,          // Order for the items (none)
            this.getComponentName(),   // The current activity name
            null,       // Specific items to place first (none)
            intent,     // Intent created above that describes our requirements
            Menu.FLAG_APPEND_TO_GROUP,          // Additional flags to control items
            null);      // Array of MenuItems that correlate to specific items (none)
        }
}


7. Application in execution

MainActivity, which lists a set of articles

Figure 5 : A set of articles are listed in the MainActivity

Contextual menu appears on long pressing an article

Figure 6 : Contextual menu appears on long pressing an article

The article is opened in a browser

Figure 7 : The article is opened in a browser


8. Download


9. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags: , , , ,

One Response to Dynamically adding activities to a menu using addIntentOptions in Android

  1. someone on June 17, 2016 at 3:54 pm

    Thank you!

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner