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:com.example.sqlbrite.todo.ui.ItemsFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);

    MenuItem item = menu.add(R.string.new_item)
            .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override//from  w  w w. j a va2  s  .  c  o m
                public boolean onMenuItemClick(MenuItem item) {
                    listener.onNewItemClicked(getListId());
                    return true;
                }
            });
    MenuItemCompat.setShowAsAction(item, SHOW_AS_ACTION_IF_ROOM | SHOW_AS_ACTION_WITH_TEXT);
}

From source file:com.github.longkai.zhihu.ui.HotItemsFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem search = menu.add(android.R.string.search_go);
    search.setIcon(R.drawable.action_search_light);
    // earn some room in action bar
    MenuItemCompat.setShowAsAction(search, MenuItemCompat.SHOW_AS_ACTION_ALWAYS);
    final View searchView = SearchViewCompat.newSearchView(getActivity());
    if (searchView != null) {
        SearchViewCompat.setOnQueryTextListener(searchView, new SearchViewCompat.OnQueryTextListenerCompat() {
            @Override//from w  w  w  . j  av a2  s  .c om
            public boolean onQueryTextChange(String newText) {
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;
                if (keywords == null && newFilter == null) {
                    return true;
                }
                if (keywords != null && keywords.equals(newFilter)) {
                    return true;
                }
                keywords = newFilter;
                getLoaderManager().restartLoader(0, null, HotItemsFragment.this);
                return true;
            }
        });
        SearchViewCompat.setOnCloseListener(searchView, new SearchViewCompat.OnCloseListenerCompat() {
            @Override
            public boolean onClose() {
                if (!TextUtils.isEmpty(SearchViewCompat.getQuery(searchView))) {
                    SearchViewCompat.setQuery(searchView, null, true);
                    return true;
                }
                return false;
            }

        });
        MenuItemCompat.setActionView(search, searchView);
    }
}

From source file:com.github.michalbednarski.intentslab.StartActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_start, menu);
    // TODO: move to menu resource
    menu.add("Proxy log (experimental)").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override/*from  www  . ja  va 2  s.  c  o  m*/
        public boolean onMenuItemClick(MenuItem item) {
            startActivity(new Intent(StartActivity.this, LogViewerActivity.class));
            return true;
        }
    });
    menu.add("RunAs local (experimental)").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            (new Thread() {
                @Override
                public void run() {
                    RemoteEntryPoint
                            .main(new String[] { new ComponentName(StartActivity.this, RunAsInitReceiver.class)
                                    .flattenToShortString() });
                }
            }).start();
            return true;
        }
    });
    menu.add("Activity Monitor (experimental)")
            .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    startActivity(new Intent(StartActivity.this, ActivityMonitorActivity.class));
                    return true;
                }
            });
    menu.add("Current objects (experimental)")
            .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    startActivity(new Intent(StartActivity.this, ClipboardActivity.class));
                    return true;
                }
            });
    menu.add("Another instance (experimental)")
            .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    startActivity(new Intent(StartActivity.this, StartActivityMultitask.class)
                            .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
                            .setAction("mi" + new Random().nextLong()));
                    return true;
                }
            });
    return true;
}

From source file:im.afterclass.android.activity.InvitePickActivity.java

/**
 * members/*from www . ja  v a2  s. c  o m*/
 * 
 * @param v
 */

public boolean onCreateOptionsMenu(Menu menu) {

    MenuItem saveItem = menu.add("saveButton");
    MenuItemCompat.setShowAsAction(saveItem, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);
    saveItem.setIcon(R.drawable.ic_action_save);
    saveItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem arg0) {

            Toast.makeText(InvitePickActivity.this, "???", Toast.LENGTH_SHORT).show();
            //Intent intent = new Intent(InvitePickActivity.this,LaunchActivity.class);
            //startActivity(intent);
            //overridePendingTransition(R.anim.head_in,0);
            return true;
        }
    });
    return true;
}

From source file:com.example.snapcacheexample.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    // only add the menu when the selection fragment is showing
    if (fragments[SELECTION].isVisible()) {
        if (menu.size() == 0) {
            settings = menu.add(R.string.settings);
        }//from  w w w.j  av  a 2  s  .  c  o  m
        return true;
    } else {
        menu.clear();
        settings = null;
    }
    return false;
}

From source file:com.examples.abelanav2.ui.UploadFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    MenuItem uploadMenu = menu.add(R.string.upload_menu_action);
    uploadMenu.setIcon(R.drawable.ic_done_white_24dp);
    uploadMenu.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    uploadMenu.setOnMenuItemClickListener(this);
    super.onCreateOptionsMenu(menu, inflater);
}

From source file:com.example.google.location.LocationActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuItem menuItem = menu.add(R.string.clear_map);
    menuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
    menuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override//from   w  w w  .j av  a 2  s.  c o  m
        public boolean onMenuItemClick(MenuItem item) {
            clearMap();
            return true;
        }
    });
    return true;
}

From source file:com.reallynourl.nourl.fmpfoldermusicplayer.ui.activity.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.add("Clear Current List").setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override//  w  w w  . ja  v  a  2  s .  c  om
        public boolean onMenuItemClick(MenuItem item) {
            DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch (which) {
                    case DialogInterface.BUTTON_POSITIVE:
                        MediaManager.getInstance().stop();
                        MediaManager.getInstance().getPlaylist().clear();
                        Snackbar.make(findViewById(R.id.content_panel), "Cleared the current list.",
                                Snackbar.LENGTH_SHORT).show();
                        break;
                    case DialogInterface.BUTTON_NEGATIVE:
                        //ignore since nothing should be done
                        break;
                    }
                }
            };
            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            builder.setMessage("Are you sure you want to clear the list?\nThis will stop playback!")
                    .setPositiveButton("Yes", dialogClickListener).setNegativeButton("No", dialogClickListener)
                    .show();
            return true;
        }
    });
    return true;
}

From source file:com.keithcassidy.finishline.FinishLineMapFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    autoUpdateMenuItem = menu.add(R.string.menu_autozoom);
    autoUpdateMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    setAutoZoomIcon();//ww  w .j  av  a2 s  . c  o m
}

From source file:eu.chainfire.geolog.ui.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    int tab = getActionBar().getSelectedNavigationIndex();

    if (tab == 0) {
        menu.add(R.string.menu_add).setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override//from  w  w  w.j a  v  a  2 s. co m
            public boolean onMenuItemClick(MenuItem item) {
                addProfile();
                return true;
            }
        }).setIcon(R.drawable.ic_action_add).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    } else if (tab == 1) {
        menu.add(R.string.menu_export).setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                ExportActivity.launchActivity(MainActivity.this, 0);
                return true;
            }
        }).setIcon(R.drawable.ic_action_export).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
        menu.add(R.string.menu_clear).setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                (new AlertDialog.Builder(MainActivity.this)).setTitle(R.string.generic_clear)
                        .setMessage(Html.fromHtml(getString(R.string.logs_clear_confirm)))
                        .setPositiveButton(getString(R.string.generic_clear),
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        (new ClearLogsAsync()).execute();
                                    }
                                })
                        .setNegativeButton(getString(R.string.generic_cancel), null).show();

                return true;
            }
        }).setIcon(R.drawable.ic_action_delete).setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);

    } else if (tab == 2) {

    }

    return true;
}