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.actionbarsherlock.sample.styled.MainActivityICS.java

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

    // set up a listener for the refresh item
    final MenuItem refresh = (MenuItem) menu.findItem(R.id.menu_refresh);
    refresh.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        // on selecting show progress spinner for 1s
        public boolean onMenuItemClick(MenuItem item) {
            // item.setActionView(R.layout.progress_action);
            handler.postDelayed(new Runnable() {
                public void run() {
                    refresh.setActionView(null);
                }/*  ww w.  j a  v  a  2s  . c om*/
            }, 1000);
            return false;
        }
    });
    return super.onCreateOptionsMenu(menu);
}

From source file:de.damdi.fitness.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;
            }/*w ww .  ja  v a  2  s . co  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();
            } 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;
        }
    });
}

From source file:com.locution.hereyak.LoginActivity.java

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

    MenuItem item = menu.findItem(R.id.action_forgot_password);

    if (item != null) {
        item.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                lostPassword();//ww w .jav a2 s.co m
                return true;
            }
        });
    }

    return true;
}

From source file:de.damdi.fitness.activity.create_exercise.CreateExerciseActivity.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.activity_create_exercise, menu);

    // configure menu_create_exercise_info
    MenuItem menu_create_exercise_info = (MenuItem) menu.findItem(R.id.menuitem_create_exercise_info);
    menu_create_exercise_info.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(CreateExerciseActivity.this);
            builder.setTitle(getString(R.string.title_about_creating_exercise));
            builder.setMessage(getString(R.string.text_about_creating_exercise));

            builder.create().show();// w  w  w.  j a  v a 2s. com

            return true;
        }
    });

    // configure menuitem_save_exercise
    MenuItem menuitem_save_exercise = (MenuItem) menu.findItem(R.id.menuitem_save_exercise);
    menuitem_save_exercise.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            saveExercise();
            return true;
        }
    });

    return true;
}

From source file:ar.com.lapotoca.resiliencia.gallery.ui.ImageDetailActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.image_menu, menu);
    MenuItem shareItem = menu.findItem(R.id.menu_share);
    shareItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override//from w  w w  .  jav  a  2 s  . c o  m
        public boolean onMenuItemClick(MenuItem item) {
            try {

                ImageHolder img = Images.image[mPager.getCurrentItem()];
                if (img == null) {
                    return false;
                }

                AnalyticsHelper.getInstance().sendImageShareEvent(img.getUrl());

                Uri bmpUri;
                if (img.isLocal()) {
                    bmpUri = Uri.parse("content://" + AssetProvider.CONTENT_URI + "/" + img.getUrl());
                } else {
                    ImageView iv = (ImageView) findViewById(R.id.picImageView);
                    bmpUri = getLocalBitmapUri(iv);
                }
                if (bmpUri != null) {
                    Intent shareIntent = new Intent();
                    shareIntent.setAction(Intent.ACTION_SEND);
                    shareIntent.putExtra(Intent.EXTRA_STREAM, bmpUri);
                    shareIntent.setType("image/*");
                    startActivity(Intent.createChooser(shareIntent, getString(R.string.share_item)));

                    AnalyticsHelper.getInstance().sendImageShareCompleted();
                    return true;
                } else {

                    AnalyticsHelper.getInstance().sendImageShareCanceled();
                    return false;
                }
            } catch (Exception e) {
                AnalyticsHelper.getInstance().sendImageShareFailed(e.getMessage());
                return false;
            }
        }
    });

    MenuItem downloadItem = menu.findItem(R.id.download_asset);
    downloadItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {

            Context context = ImageDetailActivity.this;

            String appDirectoryName = context.getString(R.string.app_name);
            File imageRoot = new File(
                    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                    appDirectoryName);

            ImageHolder img = Images.image[mPager.getCurrentItem()];
            if (img == null) {
                return false;
            }

            AssetManager assetManager = context.getAssets();
            try {
                InputStream is = assetManager.open(img.getUrl());
                String fileName = img.getUrl().split("/")[1];

                imageRoot.mkdirs();
                File image = new File(imageRoot, fileName);

                byte[] buffer = new byte[BUFFER_LENGHT];
                FileOutputStream fos = new FileOutputStream(image);
                int read = 0;

                while ((read = is.read(buffer, 0, 1024)) >= 0) {
                    fos.write(buffer, 0, read);
                }

                fos.flush();
                fos.close();
                is.close();

                String[] paths = { image.getAbsolutePath() };

                MediaScannerConnection.scanFile(context, paths, null, null);
                NotificationHelper.showNotification(context,
                        context.getString(R.string.download_image_succesfull));
                AnalyticsHelper.getInstance().sendDownloadImage(fileName);

            } catch (Exception e) {
                NotificationHelper.showNotification(context,
                        context.getString(R.string.download_no_permissions));
                AnalyticsHelper.getInstance().sendImageDownloadFailed(e.getMessage());
            }

            return true;
        }
    });

    return true;
}

From source file:de.skubware.opentraining.activity.create_exercise.CreateExerciseActivity.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.activity_create_exercise, menu);

    // configure menu_create_exercise_info
    MenuItem menu_create_exercise_info = (MenuItem) menu.findItem(R.id.menuitem_create_exercise_info);
    menu_create_exercise_info.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            AlertDialog.Builder builder = new AlertDialog.Builder(CreateExerciseActivity.this);
            builder.setTitle(getString(R.string.title_about_creating_exercise));
            builder.setMessage(getString(R.string.text_about_creating_exercise));

            builder.create().show();/* w w  w . j ava 2  s  . c  om*/

            return true;
        }
    });

    return true;
}

From source file:de.damdi.fitness.activity.start_training.FExDetailFragment.java

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

    // configure menu_item_add_entry
    MenuItem menu_item_add_entry = (MenuItem) menu.findItem(R.id.menu_item_add_entry);
    menu_item_add_entry.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            showDialog();//from  www .j  a  v  a  2  s.  com
            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();
            } else {
                license = getString(R.string.no_license_available);
            }

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

            return true;
        }
    });

    // configure menu_item_license_info
    MenuItem menu_item_history = (MenuItem) menu.findItem(R.id.menu_item_history);
    menu_item_history.setOnMenuItemClickListener(new OnMenuItemClickListener() {
        public boolean onMenuItemClick(MenuItem item) {
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            Fragment prev = getFragmentManager().findFragmentByTag("dialog");
            if (prev != null) {
                ft.remove(prev);
            }
            ft.addToBackStack(null);

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

            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;
        }
    });
}

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 a  va2s  . c om
        public boolean onMenuItemClick(MenuItem item) {
            clearMap();
            return true;
        }
    });
    return true;
}

From source file:com.repkap11.repcast.activities.BaseActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.browse, menu);
    mediaRouteMenuItem = mCastManager.addMediaRouterButton(menu, R.id.media_route_menu_item);
    mediaQueueItem = menu.findItem(R.id.action_show_queue);
    MenuItem updateApplicationMenuItem = menu.findItem(R.id.update_app_menu_button);
    updateApplicationMenuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override/*  ww  w.  j a  v a2s  . c  om*/
        public boolean onMenuItemClick(MenuItem item) {
            UpdateAppTask task = new UpdateAppTask(getApplicationContext(), true);
            task.execute();
            return true;
        }
    });
    mSearchItem = menu.findItem(R.id.action_search);
    if (mSearchItem != null) {
        mSearchItem.setVisible(mIncludeSearch);
        MenuItemCompat.setOnActionExpandListener(mSearchItem, new MenuItemCompat.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                Log.d(TAG, "onMenuItemActionExpand() called with: " + "item = [" + item + "]");
                mIsSearchExpanded = true;
                if (!mSearchView.isIconified()) {
                    //return false;
                }
                Log.e(TAG, "Setting InitialValue:" + mInitialSearchString);
                new Handler(getMainLooper()).post(new Runnable() {
                    @Override
                    public void run() {
                        mSearchView.setQuery(mInitialSearchString, false);
                    }
                });
                mediaRouteMenuItem.setVisible(false);
                mediaQueueItem.setVisible(false);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                Log.d(TAG, "onMenuItemActionCollapse() called with: " + "item = [" + item + "]");
                mIsSearchExpanded = false;
                mediaRouteMenuItem.setVisible(mCastManager.isAnyRouteAvailable());
                mediaQueueItem.setVisible(mCastManager.isConnected());
                return true;
            }
        });

        mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchItem);
        mSearchView.setImeOptions(EditorInfo.IME_FLAG_NO_EXTRACT_UI);
        if (mIsSearchExpanded) {
            Log.e(TAG, "Expanding Search 1");
            MenuItemCompat.expandActionView(mSearchItem);
            mSearchView.setQuery(mInitialSearchString, false);
        }

        mSearchView.setInputType(InputType.TYPE_TEXT_FLAG_CAP_WORDS);

        mSearchView.setOnQueryTextListener(this);
    }
    return true;
}

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

/**
 * members//from w w w  . j a v a 2s . 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;
}