Example usage for android.view MenuItem setOnMenuItemClickListener

List of usage examples for android.view MenuItem setOnMenuItemClickListener

Introduction

In this page you can find the example usage for android.view MenuItem setOnMenuItemClickListener.

Prototype

public MenuItem setOnMenuItemClickListener(MenuItem.OnMenuItemClickListener menuItemClickListener);

Source Link

Document

Set a custom listener for invocation of this menu item.

Usage

From source file:com.example.office.ui.Office365DemoActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    try {/*  w w w.j a v  a  2 s.  c om*/
        getMenuInflater().inflate(R.menu.menu_inbox, menu);

        // search
        MenuItem searchItem = menu.findItem(R.id.inbox_menu_search);
        mSearchView = (SearchView) searchItem.getActionView();
        mSearchView.setOnQueryTextListener(this);

        searchItem.setOnActionExpandListener(new OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                return true; // Return true to collapse action view
            }

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true; // Return true to expand action view
            }
        });

        // logout
        MenuItem logoutItem = menu.findItem(R.id.menu_log_out);
        logoutItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @SuppressWarnings({ "unchecked", "rawtypes" })
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                case R.id.menu_log_out:
                    // remove stored credentials
                    AuthPreferences.storeCredentials(null);
                    // remove stored mails
                    MailConfigPreferences.saveConfiguration(null);
                    // clear authentication context
                    resetToken();
                    clearCookies();
                    // reset authenticator
                    setAuthenticator(null);
                    // clear currently displayed items
                    ((ItemsFragment) getCurrentFragment()).updateList(Collections.emptyList());
                    // clear default screen
                    ((ItemsFragment) getFragmentManager()
                            .findFragmentByTag(DEFAULT_SCREEN.getName(Office365DemoActivity.this)))
                                    .updateList(Collections.emptyList());
                    // notify current fragment that user logged out
                    ((ItemsFragment) getCurrentFragment()).notifyUserLoggedOut();

                    // open authentication activity
                    tryAuthenticate();

                default:
                    return false;
                }
            }

        });

        return true;
    } catch (Exception e) {
        Logger.logApplicationException(e, getClass().getSimpleName() + ".onCreateOptionsMenu(): Error.");
        return false;
    }
}

From source file:com.guipenedo.pokeradar.activities.MapsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem settings = menu.add(0, 0, 0, R.string.settings).setIcon(R.drawable.cog);
    settings.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override/*from   w w w  . j  a v a 2  s  .c  om*/
        public boolean onMenuItemClick(MenuItem menuItem) {
            Intent i = new Intent(getBaseContext(), MainSettingsActivity.class);
            startActivity(i);
            return true;
        }
    });
    MenuItem info = menu.add(0, 0, 0, R.string.about).setIcon(R.drawable.info_circle);
    info.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            Intent i = new Intent(getBaseContext(), AboutActivity.class);
            startActivity(i);
            return true;
        }
    });
    info.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    settings.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    MenuItem logOut = menu.add(0, 0, 0, R.string.log_out).setIcon(R.drawable.sign_out);
    logOut.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            Utils.relog(MapsActivity.this);
            return true;
        }
    });
    logOut.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

From source file:com.jecelyin.editor.v2.ui.MainActivity.java

private void initToolbar() {

    Resources res = getResources();

    mToolbar.setNavigationIcon(R.drawable.ic_drawer_raw);
    mToolbar.setNavigationContentDescription(R.string.tab);

    Menu menu = mToolbar.getMenu();
    List<MenuItemInfo> menuItemInfos = MenuFactory.getInstance(this).getToolbarIcon();
    for (MenuItemInfo item : menuItemInfos) {
        MenuItem menuItem = menu.add(MenuDef.GROUP_TOOLBAR, item.getItemId(), Menu.NONE, item.getTitleResId());
        menuItem.setIcon(MenuManager.makeToolbarNormalIcon(res, item.getIconResId()));

        //menuItem.setShortcut()
        menuItem.setOnMenuItemClickListener(this);
        menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    }/*w w w.j av a  2 s.c o  m*/
    MenuItem menuItem = menu.add(MenuDef.GROUP_TOOLBAR, R.id.m_menu, Menu.NONE, getString(R.string.more_menu));
    menuItem.setIcon(R.drawable.ic_right_menu);
    menuItem.setOnMenuItemClickListener(this);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

    tabManager = new TabManager(this);
}

From source file:com.googlecode.android_scripting.facade.ui.UiFacade.java

public boolean onPrepareOptionsMenu(Menu menu) {
    if (mMenuUpdated.getAndSet(false)) {
        menu.removeGroup(MENU_GROUP_ID);
        for (UiMenuItem item : mOptionsMenuItems) {
            MenuItem menuItem = menu.add(MENU_GROUP_ID, Menu.NONE, Menu.NONE, item.mmTitle);
            if (item.mmIcon != null) {
                menuItem.setIcon(mService.getResources().getIdentifier(item.mmIcon, "drawable", "android"));
            }//from www . jav  a2s .com
            menuItem.setOnMenuItemClickListener(item.mmListener);
        }
        return true;
    }
    return true;
}

From source file:com.ibuildapp.romanblack.NewsPlugin.NewsPlugin.java

/**
 * This menu contains reverse and refresh buttons
 *
 *//*from   w w w .j  a  va2  s.  c om*/
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.news_menu_main, menu);
    menu.clear();

    MenuItem menuItem = menu.add("");
    menuItem.setTitle(getString(R.string.news_reverse));
    menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            handler.sendEmptyMessage(REVERSE_LIST);
            return true;
        }
    });

    if (Statics.isRSS) {
        menuItem = menu.add("");
        menuItem.setTitle(R.string.news_refresh);
        menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if (isOnline) {
                    handler.sendEmptyMessage(REFRESH_RSS);
                } else {
                    handler.sendEmptyMessage(CANT_REFRESH_RSS);
                }
                return true;
            }
        });
    }

    return false;
}

From source file:com.glabs.homegenie.fragments.GroupsViewFragment.java

public void UpdateCurrentGroupMenu() {
    StartActivity rootactivity = (StartActivity) getActivity();
    Menu menu = rootactivity.getActionMenu();
    if (menu != null) {
        MenuItem automation = menu.findItem(R.id.menu_automation);
        if (automation != null) {
            automation.setEnabled(false);
            Menu submenu = automation.getSubMenu();
            if (submenu == null)
                return;
            ///* w  w w  .  j a va2 s  .  c o m*/
            submenu.removeGroup(Menu.NONE);
            if (mGroupPrograms.size() > 0) {
                for (Module program : mGroupPrograms) {
                    MenuItem prg = submenu.add(Menu.NONE, Menu.NONE, Menu.NONE, program.getDisplayName());
                    prg.setIcon(R.drawable.ic_action_flash_on);
                    MenuCompat.setShowAsAction(prg, SHOW_AS_ACTION_IF_ROOM | SHOW_AS_ACTION_WITH_TEXT);
                    final String paddress = program.Address;
                    String groupname = "";
                    try {
                        groupname = Uri.encode(mAdapter.getGroup(mCurrentGroup).Name, "UTF-8");
                    } catch (Exception e) {
                    }
                    final String gname = groupname;
                    prg.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                        @Override
                        public boolean onMenuItemClick(MenuItem menuItem) {
                            String apicall = "HomeAutomation.HomeGenie/Automation/Programs.Run/" + paddress
                                    + "/" + gname + "/" + new Date().getTime();
                            Control.callServiceApi(apicall, null);
                            return true;
                        }
                    });
                }
                automation.setEnabled(true);
            }
        }
        //
        //            MenuItem recordMacro = submenu.add(1, Menu.NONE, Menu.NONE, "Record macro");
        //            recordMacro.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        //                @Override
        //                public boolean onMenuItemClick(MenuItem menuItem) {
        //                    StartActivity sa = (StartActivity)getActivity();
        //                    sa.openMacroRecordMenu();
        //                    return true;
        //                }
        //            });
        //            rootactivity.supportInvalidateOptionsMenu();
    }
}

From source file:nz.ac.auckland.lablet.ExperimentRunViewManager.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();//from w w w.  j  a  v a  2  s  .  c  o  m
    getMenuInflater().inflate(R.menu.perform_experiment_activity_actions, menu);

    // back item
    MenuItem backItem = menu.findItem(R.id.action_done);
    assert backItem != null;
    backItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            onBackPressed();
            return false;
        }
    });

    // analyse item
    MenuItem analyseItem = menu.findItem(R.id.action_analyse);
    assert analyseItem != null;
    analyseMenuItem.setMenuItem(analyseItem);
    if (!experiment.getCurrentExperimentRun().dataTaken())
        analyseMenuItem.setEnabled(false);
    analyseItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            finishExperiment(true);
            return true;
        }
    });

    // settings item
    MenuItem settingsMenu = menu.findItem(R.id.action_settings);
    assert settingsMenu != null;
    settingsMenuItem.setMenuItem(settingsMenu);

    // sensor view item
    MenuItem viewMenu = menu.findItem(R.id.action_view);
    assert viewMenu != null;
    viewMenu.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            showViewMenu();
            return true;
        }
    });

    // activate sensorDataList item
    MenuItem sensorMenu = menu.findItem(R.id.action_sensors);
    assert sensorMenu != null;
    sensorMenuItem.setMenuItem(sensorMenu);
    sensorMenu.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem menuItem) {
            showSensorMenu();
            return true;
        }
    });

    // config the menu
    Bundle options = ExperimentHelper.unpackStartExperimentOptions(getIntent());
    if (options != null) {
        boolean showAnalyseMenu = options.getBoolean("show_analyse_menu", true);
        analyseMenuItem.setVisible(showAnalyseMenu);

        boolean sensorsEditable = options.getBoolean("sensors_editable", true);
        sensorMenu.setVisible(sensorsEditable);
        if (!sensorsEditable) {
            if (experiment.getCurrentExperimentRun().getExperimentSensors().size() == 1)
                viewMenu.setVisible(false);
        }
    }

    return true;
}

From source file:com.shollmann.igcparser.ui.activity.IGCFilesActivity.java

@Override
public boolean onCreateOptionsMenu(final Menu menu) {
    getMenuInflater().inflate(R.menu.files_menu, menu);
    MenuItem menuSearchEntireSdCard = menu.findItem(R.id.menu_search_sdcard);
    MenuItem menuRefresh = menu.findItem(R.id.menu_refresh);
    MenuItem menuAbout = menu.findItem(R.id.menu_about);
    MenuItem menuShare = menu.findItem(R.id.menu_share);
    MenuItem menuSettings = menu.findItem(R.id.menu_settings);

    ImageView viewAttach = (ImageView) menu.findItem(R.id.menu_sort).getActionView();
    viewAttach.setBackgroundResource(R.drawable.drawable_sort_icon);
    viewAttach.setOnClickListener(new View.OnClickListener() {
        @Override// w ww  .j a v  a2  s  .  c o m
        public void onClick(View v) {
            showSortDialog(v);
        }

    });

    menuSearchEntireSdCard.setOnMenuItemClickListener(this);
    menuRefresh.setOnMenuItemClickListener(this);
    menuAbout.setOnMenuItemClickListener(this);
    menuShare.setOnMenuItemClickListener(this);
    menuSettings.setOnMenuItemClickListener(this);
    return super.onCreateOptionsMenu(menu);
}

From source file:org.geometerplus.android.fbreader.FBReader.java

private void addMenuItem(Menu menu, String actionId, Integer iconId, String name) {
    if (name == null) {
        name = ZLResource.resource("menu").getResource(actionId).getValue();
    }/*w  w w.  java 2s  .  c  o  m*/
    final MenuItem menuItem = menu.add(name);
    if (iconId != null) {
        menuItem.setIcon(iconId);
    }
    menuItem.setOnMenuItemClickListener(myMenuListener);
    myMenuItemMap.put(menuItem, actionId);
}

From source file:com.ibuildapp.romanblack.WebPlugin.WebPlugin.java

/**
 * This menu contains back, forward and refresh buttons.
 *
 * @param menu/*from w  w  w . j a va  2  s  .c  o  m*/
 * @return true if there are no an exception while preparing, false otherwise
 */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    try {
        menu.clear();
        MenuItem itemBack = menu.add("");
        itemBack.setIcon(R.drawable.romanblack_html_menu_back);
        itemBack.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem mi) {
                webView.goBack();
                return true;
            }
        });
        if (!webView.canGoBack()) {
            itemBack.setEnabled(false);
        }

        MenuItem itemRefresh = menu.add("");
        itemRefresh.setIcon(R.drawable.romanblack_html_menu_refresh);
        itemRefresh.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem mi) {
                webView.reload();
                return true;
            }
        });
        if ("".equals(currentUrl) || "about:blank".equals(currentUrl)) {
            itemRefresh.setEnabled(false);
        }

        MenuItem itemForward = menu.add("");
        itemForward.setIcon(R.drawable.romanblack_html_menu_forward);
        itemForward.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem mi) {
                webView.goForward();
                return true;
            }
        });
        if (!webView.canGoForward()) {
            itemForward.setEnabled(false);
        }

        return true;

    } catch (Exception ex) {
        return false;
    }
}