Example usage for android.view MenuItem.OnMenuItemClickListener MenuItem.OnMenuItemClickListener

List of usage examples for android.view MenuItem.OnMenuItemClickListener MenuItem.OnMenuItemClickListener

Introduction

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

Prototype

MenuItem.OnMenuItemClickListener

Source Link

Usage

From source file:org.tunesremote.ArtistsListFragment.java

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

    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;

    // create context menu to play entire artist
    try {//  w w  w  . j ava2  s.  c o  m
        Response resp = (Response) adapter.getItem(info.position);
        final String artist = resp.getString("mlit");
        menu.setHeaderTitle(artist);

        MenuItem play = menu.add(R.string.artists_menu_play);
        play.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                host.session.controlPlayArtist(artist, 0);
                host.setResult(Activity.RESULT_OK, new Intent());
                host.finish();
                return true;
            }
        });

        MenuItem queue = menu.add(R.string.artists_menu_queue);
        queue.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                host.session.controlQueueArtist(artist);
                host.setResult(Activity.RESULT_OK, new Intent());
                host.finish();
                return true;
            }
        });

        MenuItem browse = menu.add(R.string.artists_menu_browse);
        browse.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                Intent intent = new Intent(host, AlbumsActivity.class);
                intent.putExtra(Intent.EXTRA_TITLE, artist);
                host.startActivityForResult(intent, 1);
                return true;
            }
        });
    } catch (Exception e) {
        Log.w(TAG, "onCreateContextMenu:" + e.getMessage());
    }

}

From source file:com.dattasmoon.pebble.plugin.IgnorePreference.java

@Override
protected void onBindDialogView(View view) {

    btnAdd = (Button) view.findViewById(R.id.btnAdd);
    etMatch = (EditText) view.findViewById(R.id.etMatch);
    chkRawRegex = (CheckBox) view.findViewById(R.id.chkRawRegex);
    chkCaseInsensitive = (CheckBox) view.findViewById(R.id.chkCaseInsensitive);
    actvApplications = (AutoCompleteTextView) view.findViewById(R.id.actvApplications);

    spnApplications = (Spinner) view.findViewById(R.id.spnApplications);
    spnMode = (Spinner) view.findViewById(R.id.spnMode);
    lvIgnore = (ListView) view.findViewById(R.id.lvIgnore);
    lvIgnore.setAdapter(arrayAdapter);// w  ww  .j a v a 2 s  .c  o m
    lvIgnore.setEmptyView(view.findViewById(android.R.id.empty));
    new LoadAppsTask().execute();

    lvIgnore.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
        @Override
        public void onCreateContextMenu(ContextMenu menu, View view, ContextMenu.ContextMenuInfo menuInfo) {
            AdapterView.AdapterContextMenuInfo contextInfo = (AdapterView.AdapterContextMenuInfo) menuInfo;
            int position = contextInfo.position;
            long id = contextInfo.id;
            // the child view who's info we're viewing (should be equal to v)
            final View v = contextInfo.targetView;
            MenuInflater inflater = new MenuInflater(getContext());
            inflater.inflate(R.menu.preference_ignore_context, menu);

            //we have to do this mess because DialogPreference doesn't allow for onMenuItemSelected or onOptionsItemSelected. Bleh
            menu.findItem(R.id.btnEdit).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    final int arrayPosition = (Integer) v.getTag();
                    final String text = ((TextView) v.findViewById(R.id.tvItem)).getText().toString();
                    JSONArray temp = new JSONArray();
                    for (int i = 0; i < arrayAdapter.getJSONArray().length(); i++) {
                        try {
                            JSONObject ignore = arrayAdapter.getJSONArray().getJSONObject(i);
                            if (i == arrayPosition) {
                                etMatch.setText(ignore.getString("match"));
                                chkRawRegex.setChecked(ignore.getBoolean("raw"));
                                chkCaseInsensitive.setChecked(ignore.optBoolean("insensitive", true));
                                String app = ignore.getString("app");
                                if (app == "-1") {
                                    actvApplications.setText(getContext().getString(R.string.ignore_any));
                                } else {
                                    actvApplications.setText(app);
                                }
                                boolean exclude = ignore.optBoolean("exclude", true);
                                if (exclude) {
                                    spnMode.setSelection(Constants.IgnoreMode.EXCLUDE.ordinal());
                                } else {
                                    spnMode.setSelection(Constants.IgnoreMode.INCLUDE.ordinal());
                                }
                                continue;
                            }

                            temp.put(ignore);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    arrayAdapter.setJSONArray(temp);

                    arrayAdapter.notifyDataSetChanged();
                    return true;
                }
            });
            menu.findItem(R.id.btnDelete).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {
                    AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

                    final int arrayPosition = (Integer) v.getTag();
                    final String text = ((TextView) v.findViewById(R.id.tvItem)).getText().toString();
                    builder.setMessage(getContext().getResources().getString(R.string.confirm_delete) + " '"
                            + text + "' ?")
                            .setPositiveButton(R.string.confirm, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    JSONArray temp = new JSONArray();
                                    for (int i = 0; i < arrayAdapter.getJSONArray().length(); i++) {
                                        if (i == arrayPosition) {
                                            continue;
                                        }
                                        try {
                                            temp.put(arrayAdapter.getJSONArray().getJSONObject(i));
                                        } catch (JSONException e) {
                                            e.printStackTrace();
                                        }
                                    }
                                    arrayAdapter.setJSONArray(temp);

                                    arrayAdapter.notifyDataSetChanged();
                                }
                            }).setNegativeButton(R.string.decline, new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    // User cancelled the dialog
                                }
                            });
                    builder.create().show();
                    return true;
                }
            });
        }
    });
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JSONObject item = new JSONObject();
            try {
                item.put("match", etMatch.getText().toString());
                item.put("raw", chkRawRegex.isChecked());
                item.put("insensitive", chkCaseInsensitive.isChecked());
                if (actvApplications.getText().toString()
                        .equalsIgnoreCase(getContext().getString(R.string.ignore_any))) {
                    item.put("app", "-1");
                } else {
                    item.put("app", actvApplications.getText().toString());
                }
                if (spnMode.getSelectedItemPosition() == Constants.IgnoreMode.INCLUDE.ordinal()) {
                    item.put("exclude", false);
                } else {
                    item.put("exclude", true);
                }
                if (Constants.IS_LOGGABLE) {
                    Log.i(Constants.LOG_TAG, "Item is: " + item.toString());
                }
                arrayAdapter.getJSONArray().put(item);
                etMatch.setText("");
                arrayAdapter.notifyDataSetChanged();
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    });
    actvApplications.setText(getContext().getString(R.string.ignore_any));
    actvApplications.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            actvApplications.showDropDown();
        }
    });
    actvApplications.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ApplicationInfo pkg = (ApplicationInfo) parent.getItemAtPosition(position);
            if (pkg == null) {
                actvApplications.setText(getContext().getString(R.string.ignore_any));
            } else {
                actvApplications.setText(pkg.packageName);
            }
        }
    });
    actvApplications.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus) {
                actvApplications.showDropDown();
            } else {
                if (actvApplications.getText().length() == 0) {
                    actvApplications.setText(getContext().getString(R.string.ignore_any));
                }
            }
        }
    });
    super.onBindDialogView(view);

}

From source file:com.slidinglayersample.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_menu, menu);

    menu.findItem(R.id.action_sign_out).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            User.signOutUser();/*from w  w w  .ja  v  a 2  s  .com*/
            MainActivity.this.finish();
            startActivity(new Intent(MainActivity.this, RegisterOrSignInActivity.class));
            return true;
        }
    });

    menu.findItem(R.id.action_new_company).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
            final ArrayList<Integer> mSelectedItems = new ArrayList<Integer>();

            LayoutInflater inflater = MainActivity.this.getLayoutInflater();
            final View v = inflater.inflate(R.layout.dialog_create_company, null, false);
            builder.setView(v);

            builder.setTitle("Add company");

            builder.setPositiveButton("Add", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                    Company c = new Company();

                    EditText name = (EditText) v.findViewById(R.id.reg_company_name);
                    EditText desc = (EditText) v.findViewById(R.id.reg_company_description);

                    c.setName(name.getText().toString());
                    c.setDescription(desc.getText().toString());
                    c.setCreatorId(User.getCurrentUser().getId());

                    //                        mCompanyArrayList.add(c);
                    //                        mCompanyAdapter.notifyDataSetChanged();

                    dlg = new ProgressDialog(MainActivity.this);
                    dlg.setTitle("Please wait.");
                    dlg.setMessage("Company registration.  Please wait.");
                    dlg.show();

                    new RegisterCompany().execute(c);

                    //                        showToast("Added company : " + c.getName(), SuperToast.Background.BLUE);
                    //                        Toast.makeText(getApplicationContext(), mSelectedItems.size(), Toast.LENGTH_LONG).show();
                }
            }).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {

                }
            });

            AlertDialog dialog = builder.create();
            dialog.show();

            return true;
        }
    });

    return true;
}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    menu.clear();/*  www.j av a 2s.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.vuze.android.remote.fragment.TorrentListFragment.java

private void setupActionModeCallback() {
    mActionModeCallbackV7 = new Callback() {

        // Called when the action mode is created; startActionMode() was called
        @Override/*from   ww  w .  ja v  a  2  s  .c  o m*/
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {

            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "onCreateActionMode");
            }

            if (mode == null && torrentListAdapter.getCheckedItemCount() == 0
                    && torrentListAdapter.getSelectedPosition() < 0) {
                return false;
            }

            Menu origMenu = menu;
            if (tb != null) {
                menu = tb.getMenu();
            }
            if (mode != null) {
                mActionMode = (mode instanceof ActionModeWrapperV7) ? mode
                        : new ActionModeWrapperV7(mode, tb, getActivity());

                mActionMode.setTitle(R.string.context_torrent_title);
            }
            ActionBarToolbarSplitter.buildActionBar(getActivity(), this, R.menu.menu_context_torrent_details,
                    menu, tb);

            TorrentDetailsFragment frag = (TorrentDetailsFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.frag_torrent_details);
            if (frag != null) {
                frag.onCreateActionMode(mode, menu);
            }

            if (sideListArea == null) {
                SubMenu subMenu = origMenu.addSubMenu(R.string.menu_global_actions);
                subMenu.setIcon(R.drawable.ic_menu_white_24dp);
                MenuItemCompat.setShowAsAction(subMenu.getItem(), MenuItemCompat.SHOW_AS_ACTION_NEVER);

                try {
                    // Place "Global" actions on top bar in collapsed menu
                    MenuInflater mi = mode == null ? getActivity().getMenuInflater() : mode.getMenuInflater();
                    mi.inflate(R.menu.menu_torrent_list, subMenu);
                    onPrepareOptionsMenu(subMenu);
                } catch (UnsupportedOperationException e) {
                    Log.e(TAG, e.getMessage());
                    menu.removeItem(subMenu.getItem().getItemId());
                }
            }

            if (AndroidUtils.usesNavigationControl()) {
                MenuItem add = origMenu.add(R.string.select_multiple_items);
                add.setCheckable(true);
                add.setChecked(torrentListAdapter.isMultiCheckMode());
                add.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        boolean turnOn = !torrentListAdapter.isMultiCheckModeAllowed();

                        torrentListAdapter.setMultiCheckModeAllowed(turnOn);
                        if (turnOn) {
                            torrentListAdapter.setMultiCheckMode(turnOn);
                            torrentListAdapter.setItemChecked(torrentListAdapter.getSelectedPosition(), true);
                        }
                        return true;
                    }
                });
            }

            return true;
        }

        // Called each time the action mode is shown. Always called after
        // onCreateActionMode, but
        // may be called multiple times if the mode is invalidated.
        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {

            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "MULTI:onPrepareActionMode " + mode);
            }
            if (tb != null) {
                menu = tb.getMenu();
            }

            // Must be called first, because our drawer sets all menu items
            // visible.. :(
            getActivity().onPrepareOptionsMenu(menu);

            prepareContextMenu(menu);

            TorrentDetailsFragment frag = (TorrentDetailsFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.frag_torrent_details);
            if (frag != null) {
                frag.onPrepareActionMode(mode, menu);
            }

            AndroidUtils.fixupMenuAlpha(menu);

            return true;
        }

        // Called when the user selects a contextual menu item
        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "onActionItemClicked " + item.getTitle());
            }

            if (TorrentListFragment.this.handleFragmentMenuItems(item.getItemId())) {
                return true;
            }
            if (getActivity().onOptionsItemSelected(item)) {
                return true;
            }
            TorrentDetailsFragment frag = (TorrentDetailsFragment) getActivity().getSupportFragmentManager()
                    .findFragmentById(R.id.frag_torrent_details);
            if (frag != null) {
                if (frag.onActionItemClicked(mode, item)) {
                    return true;
                }
            }
            return false;
        }

        // Called when the user exits the action mode
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            if (AndroidUtils.DEBUG_MENU) {
                Log.d(TAG, "onDestroyActionMode. BeingReplaced?" + actionModeBeingReplaced);
            }

            mActionMode = null;

            if (!actionModeBeingReplaced) {
                listview.post(new Runnable() {
                    @Override
                    public void run() {
                        torrentListAdapter.setMultiCheckMode(false);
                        torrentListAdapter.clearChecked();
                        updateCheckedIDs();
                    }
                });

                listview.post(new Runnable() {
                    @Override
                    public void run() {
                        if (mCallback != null) {
                            mCallback.actionModeBeingReplacedDone();
                        }
                    }
                });

                listview.setLongClickable(true);
                listview.requestLayout();
                AndroidUtils.invalidateOptionsMenuHC(getActivity(), mActionMode);
            }
        }
    };
}

From source file:cm.aptoide.pt.MainActivity.java

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

    switch (v.getId()) {
    case R.id.available_list:
        Integer tag = (Integer) ((AdapterContextMenuInfo) menuInfo).targetView.getTag();
        if (tag != null && tag == 1) {
            menu.add(0, 1, 0, R.string.menu_context_reparse);
        }//  ww w.  j  ava2s  . co m
        menu.add(0, 0, 0, R.string.menu_context_remove);
        break;
    case R.id.updates_list:
        Log.d("onCreateContextMenu", "onCreateContextMenu");
        menu.add(0, (int) ((AdapterContextMenuInfo) menuInfo).id, 0,
                mContext.getString(R.string.exclude_update))
                .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        System.out.println(item.getItemId());
                        db.addToExcludeUpdate(item.getItemId());
                        updatesLoader.forceLoad();
                        return false;
                    }
                });
        break;
    }

}