Example usage for android.view Menu add

List of usage examples for android.view Menu add

Introduction

In this page you can find the example usage for android.view Menu add.

Prototype

public MenuItem add(@StringRes int titleRes);

Source Link

Document

Add a new item to the menu.

Usage

From source file:at.jclehner.rxdroid.BackupFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    if (!getActivity().getIntent().getBooleanExtra(BackupActivity.EXTRA_NO_BACKUP_CREATION, false)) {
        final MenuItem item = menu.add(getString(R.string._title_create_backup))
                .setIcon(R.drawable.ic_action_add_box_white)
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override//from  www .  j  a  v  a  2s.c o  m
                    public boolean onMenuItemClick(MenuItem menuItem) {
                        final String storageState = Backup.getStorageState();
                        if (Backup.StorageStateListener.isWritable(storageState)) {
                            try {
                                Backup.createBackup(null, null);
                                getLoaderManager().restartLoader(0, null, BackupFragment.this);
                            } catch (ZipException e) {
                                showExceptionDialog(e);
                            }
                        } else {
                            if (mShowDialogIfNotWriteable) {
                                final AlertDialog.Builder ab = new AlertDialog.Builder(getActivity());
                                ab.setPositiveButton(android.R.string.ok, null);
                                ab.setMessage(R.string._msg_external_storage_not_writeable);
                                ab.setTitle(R.string._title_error);
                                ab.setIcon(android.R.drawable.ic_dialog_alert);

                                ab.show();
                                mShowDialogIfNotWriteable = false;
                            } else {
                                Toast.makeText(getActivity(), R.string._msg_external_storage_not_writeable,
                                        Toast.LENGTH_LONG);
                            }
                        }

                        return true;
                    }
                });

        MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
    }
    super.onCreateOptionsMenu(menu, inflater);
}

From source file:at.jclehner.appopsxposed.SettingsActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (Util.isSystemApp(this)) {
        menu.add(R.string.uninstall).setIcon(android.R.drawable.ic_menu_delete)
                .setOnMenuItemClickListener(new OnMenuItemClickListener() {

                    @Override// w w  w  . j a va  2 s  . c  om
                    public boolean onMenuItemClick(MenuItem item) {
                        final AlertDialog.Builder ab = new AlertDialog.Builder(SettingsActivity.this);
                        ab.setMessage(getString(R.string.uninstall) + "? " + getString(R.string.will_reboot));
                        ab.setNegativeButton(android.R.string.cancel, null);
                        ab.setPositiveButton(android.R.string.ok, new OnClickListener() {

                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                if (!SU.available()) {
                                    Toast.makeText(SettingsActivity.this, R.string.toast_needs_root,
                                            Toast.LENGTH_SHORT).show();
                                } else {
                                    final String[] commands = { "mount -o remount,rw /system",
                                            "rm " + LauncherActivity.SYSTEM_APK, "mount -o remount,ro /system",
                                            "sync", "reboot", };

                                    Toast.makeText(SettingsActivity.this, R.string.will_reboot,
                                            Toast.LENGTH_LONG).show();
                                    Util.runAsSu(commands);
                                }
                            }
                        });

                        ab.show();

                        return true;
                    }
                }).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    }

    return true;
}

From source file:com.irsa.OnTheGoActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add("Personal Information").setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override/*from   www.  ja  va  2 s.  c o m*/
        public boolean onMenuItemClick(MenuItem item) {
            // TODO Auto-generated method stub
            startActivity(new Intent(OnTheGoActivity.this, Account.class));
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
    //return super.onCreateOptionsMenu(menu);
}

From source file:de.appetites.android.menuItemSearchAction.MenuItemSearchAction.java

private void createMenuItem(Menu menu, Drawable drawable, final LinearLayout list) {
    ActionBar.LayoutParams layoutParams = new ActionBar.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT);
    setLayoutParams(layoutParams);// www. j  a v  a 2  s.c  o m

    setHint(R.string.menu_item_search_action_hint);

    searchItem = menu.add(R.string.menu_item_search_action_menu_text).setIcon(drawable);
    searchItem.setActionView(this).setTitle("Search");
    searchItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    searchItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // open keyboard
            list.setVisibility(View.VISIBLE);
            MenuItemSearchAction.this.setText("");
            MenuItemSearchAction.this.requestFocus();

            MenuItemSearchAction.this.postDelayed(new Runnable() {
                @Override
                public void run() {
                    InputMethodManager imm = (InputMethodManager) context
                            .getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
                }
            }, 200);
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // close keyboard
            list.setVisibility(View.GONE);
            InputMethodManager imm = (InputMethodManager) context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(MenuItemSearchAction.this.getWindowToken(), 0);

            return true;
        }
    });

    setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // perform Search
                searchPerformListener.performSearch(MenuItemSearchAction.this.getText().toString());

                searchItem.collapseActionView();
                return true;
            }
            return false;
        }
    });
}

From source file:no.ntnu.idi.socialhitchhiking.Main.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Login as a different user").setIcon(R.drawable.fb_icon)
            .setOnMenuItemClickListener(new OnMenuItemClickListener() {
                @Override//from   w  w w .ja v a 2  s  . c om
                public boolean onMenuItemClick(MenuItem item) {
                    loginAsNewClicked(true);
                    if (getApp().getUser() != null) {
                        user = getApp().getUser();
                    }
                    return false;
                }
            });
    return super.onCreateOptionsMenu(menu);
}

From source file:com.pepperonas.showcase.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("About");
    menu.add(mSharedPreferences.getBoolean("SHOW_TOASTS", true) ? Const.HIDE_TOASTS : Const.SHOW_TOASTS);
    return super.onCreateOptionsMenu(menu);
}

From source file:com.orpheusdroid.screenrecorder.VideosListFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MenuItem refresh = menu.add("Refresh");
    refresh.setIcon(R.drawable.ic_refresh_white_24dp);
    refresh.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
    refresh.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override//  ww w  .  java2  s. c  o m
        public boolean onMenuItemClick(MenuItem menuItem) {
            // Prevent repeated refresh requests
            if (swipeRefreshLayout.isRefreshing())
                return false;
            videosList.clear();
            checkPermission();
            Log.d(Const.TAG, "Refreshing");
            return false;
        }
    });
}

From source file:com.bill.metrobus.FriendsList.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Place an action bar item for searching.
    MenuItem item = menu.add("Search");
    item.setIcon(android.R.drawable.ic_menu_search);
    MenuItemCompat.setShowAsAction(item,
            MenuItemCompat.SHOW_AS_ACTION_ALWAYS | MenuItemCompat.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);
    final View searchView = SearchViewCompat.newSearchView(getActivity());
    if (searchView != null) {
        SearchViewCompat.setOnQueryTextListener(searchView, new OnQueryTextListenerCompat() {
            @Override/*w  w  w . j a v  a  2s  .  c o m*/
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has
                // changed. Update
                // the search filter, and restart the loader to do a
                // new query
                // with this filter.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
                // Don't do anything if the filter hasn't actually
                // changed.
                // Prevents restarting the loader when restoring
                // state.
                if (mCurFilter == null && newFilter == null) {
                    return true;
                }
                if (mCurFilter != null && mCurFilter.equals(newFilter)) {
                    return true;
                }
                mCurFilter = newFilter;
                // getLoaderManager().restartLoader(0, null,
                // FriendsList.this);
                return true;
            }
        });
        SearchViewCompat.setOnCloseListener(searchView, new OnCloseListenerCompat() {
            @Override
            public boolean onClose() {
                if (!TextUtils.isEmpty(SearchViewCompat.getQuery(searchView))) {
                    SearchViewCompat.setQuery(searchView, null, true);
                }
                return true;
            }
        });
        MenuItemCompat.setActionView(item, searchView);
    }
}

From source file:com.ogunwale.android.app.yaps.ui.PhotosActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    if (mGettingRemoteData)
        menu.removeItem(R.id.menu_item_refresh);
    else if (menu.findItem(R.id.menu_item_refresh) == null)
        menu.add(R.id.menu_item_refresh);

    return super.onPrepareOptionsMenu(menu);
}