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:cf.obsessiveorange.rhcareerfairlayout.ui.fragments.VPFiltersFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MenuItem searchBtn = menu.findItem(R.id.action_search);
    searchBtn.setVisible(false);//from ww  w .j  a va 2s  .  co  m

    ((MainActivity) getActivity()).closeSearch();

    MenuItem clearFiltersItem = menu.add(getResources().getString(R.string.btn_clear_filters));
    clearFiltersItem.setIcon(new IconDrawable(this.getActivity(), Iconify.IconValue.fa_times)
            .colorRes(R.color.accentNoTransparency).actionBarSize());
    clearFiltersItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    clearFiltersItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            Toast.makeText(getActivity(), "Filters cleared", Toast.LENGTH_SHORT).show();

            try {
                DBManager.setAllCategoriesSelected(false);
            } catch (SQLException e) {
                e.printStackTrace();
            }

            ((FiltersCellAdapter) recyclerView.getAdapter()).refreshData();

            synchronized (RHCareerFairLayout.refreshCompaniesNotifier) {
                RHCareerFairLayout.refreshCompaniesNotifier.notifyChanged();
            }

            getActivity().runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    recyclerView.getAdapter().notifyDataSetChanged();
                }
            });

            return true;
        }
    });
}

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.bitants.wally.fragments.SavedImagesFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.images_saved, menu);

    MenuItem menuItemFilter = menu.findItem(R.id.action_licenses);

    if (menuItemFilter != null) {
        menuItemFilter.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override/*from ww w .  ja va  2  s  .c o m*/
            public boolean onMenuItemClick(MenuItem item) {
                final LicensesDialogFragment fragment = LicensesDialogFragment.newInstance(R.raw.notices,
                        false);
                fragment.show(getFragmentManager(), null);
                return false;
            }
        });
    }

    if (!BuildConfig.BUILD_TYPE.equalsIgnoreCase("release")) {
        menu.add("Wally " + BuildConfig.VERSION_NAME);
    }

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:com.kciray.android.filemanager.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);

    MenuItem addNewFolderItem = menu.findItem(R.id.add_new_folder);
    addNewFolderItem.setOnMenuItemClickListener(item -> {
        activeDirView.addNewFolder();//w  ww. ja va 2s.c  o  m
        return false;
    });

    addMenuAction(menu, R.id.settings, () -> {
        Intent intent = new Intent(MainActivity.this, FMPreferenceActivity.class);
        startActivity(intent);
    });

    addMenuAction(menu, R.id.about, () -> {
        ActivityUtils.start(AboutActivity.class);
        overridePendingTransition(R.anim.rotate_in, 0);
    });

    return super.onCreateOptionsMenu(menu);
}

From source file:com.kciray.android.filemanager.MainActivity.java

private void addMenuAction(Menu menu, int itemId, final Runnable runnable) {
    MenuItem settingsItem = menu.findItem(itemId);
    if (settingsItem != null)
        settingsItem.setOnMenuItemClickListener(item -> {
            runnable.run();/* w  w w.j a  v  a 2  s .co m*/
            return false;
        });
}

From source file:com.kaliturin.blacklist.fragments.SMSConversationsListFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.main, menu);

    MenuItem menuItem = menu.findItem(R.id.write_message);
    Utils.setMenuIconTint(getContext(), menuItem, R.attr.colorAccent);
    menuItem.setVisible(true);/* w  w w  . ja va 2 s. c  om*/
    menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // open SMS sending activity
            CustomFragmentActivity.show(getContext(), getString(R.string.New_message), SMSSendFragment.class,
                    null);
            return true;
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:com.bitants.wally.fragments.LatestFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.images_list, menu);

    MenuItem menuItemFilter = menu.findItem(R.id.action_filter);

    if (menuItemFilter != null) {
        menuItemFilter.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            @Override/*from  w  w w .  j  a  va2s .c  om*/
            public boolean onMenuItemClick(MenuItem item) {

                FragmentManager fragmentManager = getFragmentManager();

                if (fragmentManager != null) {
                    final FilterDialogFragment filterDialogFragment = new FilterDialogFragment();
                    filterDialogFragment.setPrimaryColor(getAppBarColor());
                    filterDialogFragment.setPositiveButton(R.string.submit,
                            new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    if (filterDialogFragment.saveChanges()) {
                                        WallyApplication.getContext().sendBroadcast(
                                                new Intent(FiltersChangeReceiver.FILTERS_CHANGED));
                                    }
                                }
                            });
                    filterDialogFragment.setNegativeButton(R.string.cancel, null);
                    filterDialogFragment.show(fragmentManager, FilterDialogFragment.TAG);
                }

                return false;
            }
        });
    }

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:de.skubware.opentraining.activity.create_workout.ExerciseTypeDetailFragment.java

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

    // MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.exercise_detail_menu, menu);

    // configure menu_item_add_exercise
    MenuItem menu_item_add_exercise = (MenuItem) menu.findItem(R.id.menu_item_add_exercise);
    menu_item_add_exercise.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {

            // assert, that an exercise was choosen
            if (mExercise == null) {
                Log.wtf(TAG, "No exercise has been choosen. This should not happen");
                return true;
            }//from w  w w .java  2s  . c o m

            // add exercise to workout or create a new one
            if (mWorkout == null) {
                SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
                String defaultWorkoutName = settings.getString("default_workout_name", "Workout");

                mWorkout = new Workout(defaultWorkoutName, new FitnessExercise(mExercise));
            } else {

                // assert that there is not already such an exercise in the
                // workout
                for (FitnessExercise fEx : mWorkout.getFitnessExercises()) {
                    if (fEx.getExType().equals(mExercise)) {
                        Toast.makeText(getActivity(), getString(R.string.exercise_already_in_workout),
                                Toast.LENGTH_LONG).show();
                        return true;
                    }
                }

                mWorkout.addFitnessExercise(new FitnessExercise(mExercise));
            }

            // update Workout in Activity
            if (getActivity() instanceof Callbacks) {
                // was launched by ExerciseTypeListActivity
                ((Callbacks) getActivity()).onWorkoutChanged(mWorkout);
            } else {
                // was launched by ExerciseTypeDetailActivity
                Intent i = new Intent();
                i.putExtra(ExerciseTypeListActivity.ARG_WORKOUT, mWorkout);
                getActivity().setResult(Activity.RESULT_OK, i);
                getActivity().finish();
            }

            Toast.makeText(getActivity(), getString(R.string.exercise) + " " + mExercise.getLocalizedName()
                    + " " + getString(R.string.has_been_added), Toast.LENGTH_SHORT).show();

            return true;
        }
    });

    // configure menu_item_license_info
    MenuItem menu_item_license_info = (MenuItem) menu.findItem(R.id.menu_item_license_info);
    menu_item_license_info.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(getString(R.string.license_info));

            String license = "";

            if (mExercise.getImageLicenseMap().values().iterator().hasNext()) {
                license = mExercise.getImageLicenseMap().values().iterator().next().toString();
            } else {
                license = getString(R.string.no_license_available);
            }

            builder.setMessage(license);
            builder.create().show();

            return true;
        }
    });

    // configure menu_item_description
    MenuItem menu_item_description = (MenuItem) menu.findItem(R.id.menu_item_description);
    menu_item_description.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            if (mExercise.getDescription() == null || mExercise.getDescription().equals("")) {
                Toast.makeText(getActivity(), getString(R.string.no_description_available), Toast.LENGTH_LONG)
                        .show();
                return true;
            }

            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            builder.setTitle(getString(R.string.description));

            builder.setMessage(Html.fromHtml(mExercise.getDescription()));
            builder.create().show();

            return true;
        }
    });

    // configure menu_item_delete_exercise
    if (mExercise != null && mExercise.getExerciseSource() == ExerciseSource.CUSTOM) {
        MenuItem menu_item_delete_exercise = (MenuItem) menu.findItem(R.id.menu_item_delete_exercise);
        menu_item_delete_exercise.setVisible(true);
        menu_item_delete_exercise.setOnMenuItemClickListener(new OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                IDataProvider dataProvider = new DataProvider(getActivity());
                dataProvider.deleteCustomExercise(mExercise);

                if (getActivity() instanceof Callbacks) {
                    // was launched by ExerciseTypeListActivity
                    ((Callbacks) getActivity()).onExerciseDeleted(mExercise);
                } else {
                    // was launched by ExerciseTypeDetailActivity
                    Intent i = new Intent();
                    i.putExtra(ARG_DELETED_EXERCISE, mExercise);
                    getActivity().setResult(RESULT_EXERCISE_CHANGED, i);
                    getActivity().finish();
                }

                return false;
            }
        });
    }

    // configure menu_item_upload_exercise
    MenuItem menu_item_upload_exercise = (MenuItem) menu.findItem(R.id.menu_item_upload_exercise);
    menu_item_upload_exercise.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            Context context = ExerciseTypeDetailFragment.this.getActivity();

            //UploadExerciseAsyncTask exUpload = new UploadExerciseAsyncTask(context);
            //exUpload.execute(mExercise);

            UploadExerciseImagesAsyncTask exImageUpload = new UploadExerciseImagesAsyncTask(context);
            exImageUpload.execute(mExercise);

            return true;
        }
    });

    // configure menu_item_send_exercise_feedback
    MenuItem menu_item_delete_exercise = (MenuItem) menu.findItem(R.id.menu_item_send_exercise_feedback);
    menu_item_delete_exercise.setVisible(true);
    menu_item_delete_exercise.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            Fragment prev = getActivity().getSupportFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);

            // Create and show the dialog.
            DialogFragment newFragment = SendExerciseFeedbackDialogFragment.newInstance(mExercise);
            newFragment.show(ft, "dialog");

            return false;
        }
    });

}

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

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

    MenuItem clearListMenuItem = menu.add(R.string.menu_clear_crossing_times);
    clearListMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    clearListMenuItem.setIcon(android.R.drawable.ic_menu_delete);

    clearListMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {

        @Override//from   w  w  w.ja  v a  2  s. co  m
        public boolean onMenuItemClick(MenuItem item) {
            try {
                deleteCrossings();
            } catch (Exception e) {
                Log.e(TAG, "onMenuItemClick " + e);
            }
            return true;
        }
    });

    MenuItem shareMenuItem = menu.add(R.string.menu_share);
    shareMenuItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    shareMenuItem.setIcon(android.R.drawable.ic_menu_share);

    shareMenuItem.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            try {
                sendCurrentCrossing();
            } catch (Exception e) {
                Log.e(TAG, "onMenuItemClick " + e);
            }
            return true;
        }
    });

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:org.jsharkey.oilcan.BrowserActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    MenuItem gourl = menu.add(R.string.browse_gotourl);
    gourl.setIcon(R.drawable.ic_menu_goto);
    gourl.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            BrowserActivity.this.startSearch(webview.getUrl(), true, null, false);
            return true;
        }//from ww  w .j  a v  a2s  .c  om
    });

    MenuItem refresh = menu.add(R.string.browse_refresh);
    refresh.setIcon(R.drawable.ic_menu_refresh);
    refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            webview.reload();
            return true;
        }
    });

    MenuItem scripts = menu.add(R.string.browse_manage);
    scripts.setIcon(android.R.drawable.ic_menu_agenda);
    scripts.setIntent(new Intent(BrowserActivity.this, ScriptListActivity.class));

    MenuItem example = menu.add(R.string.browse_example);
    example.setIcon(R.drawable.ic_menu_bookmark);
    example.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            final String[] examples = BrowserActivity.this.getResources().getStringArray(R.array.list_examples);
            new AlertDialog.Builder(BrowserActivity.this).setTitle(R.string.browse_example_title)
                    .setItems(examples, new OnClickListener() {
                        public void onClick(DialogInterface dialog, int which) {
                            BrowserActivity.this.loadNewPage(examples[which]);
                        }
                    }).create().show();

            return true;
        }
    });

    return true;
}