Example usage for android.view Menu findItem

List of usage examples for android.view Menu findItem

Introduction

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

Prototype

public MenuItem findItem(int id);

Source Link

Document

Return the menu item with a particular identifier.

Usage

From source file:net.sourceforge.kalimbaradio.androidapp.activity.DownloadActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    MenuItem savePlaylist = menu.findItem(R.id.menu_save_playlist);
    boolean savePlaylistEnabled = !Util.isOffline(this);
    savePlaylist.setEnabled(savePlaylistEnabled);
    savePlaylist.setVisible(savePlaylistEnabled);
    MenuItem screenOption = menu.findItem(R.id.menu_screen_on_off);
    if (getDownloadService().getKeepScreenOn()) {
        screenOption.setTitle(R.string.download_menu_screen_off);
    } else {//from  w  w  w.  j a v a2 s.co m
        screenOption.setTitle(R.string.download_menu_screen_on);
    }
    MenuItem searchOption = menu.findItem(R.id.menu_search);
    searchOption.setVisible(!Util.isOffline(this));

    return super.onPrepareOptionsMenu(menu);
}

From source file:com.slim.turboeditor.activity.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    if (fileOpened && searchResult != null) {
        MenuItem imReplace = menu.findItem(R.id.im_replace);
        MenuItem imReplaceAll = menu.findItem(R.id.im_replace_all);
        MenuItem imPrev = menu.findItem(R.id.im_previous_item);
        MenuItem imNext = menu.findItem(R.id.im_next_item);

        if (imReplace != null)
            imReplace.setVisible(searchResult.canReplaceSomething());

        if (imReplaceAll != null)
            imReplaceAll.setVisible(searchResult.canReplaceSomething());

        if (imPrev != null)
            imPrev.setVisible(searchResult.hasPrevious());

        if (imNext != null)
            imNext.setVisible(searchResult.hasNext());

    } else if (fileOpened) {
        MenuItem imSave = menu.findItem(R.id.im_save);
        MenuItem imUndo = menu.findItem(R.id.im_undo);
        MenuItem imRedo = menu.findItem(R.id.im_redo);

        if (mEditor != null) {
            if (imSave != null)
                imSave.setVisible(mEditor.canSaveFile());
            if (imUndo != null)
                imUndo.setVisible(mEditor.getCanUndo());
            if (imRedo != null)
                imRedo.setVisible(mEditor.getCanRedo());
        } else {/*from  ww  w . j av  a2  s . c  o m*/
            imSave.setVisible(false);
            imUndo.setVisible(false);
            imRedo.setVisible(false);
        }
    }

    return true;
}

From source file:com.example.alvarpao.popularmovies.MovieGridFragment.java

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

    // Get the array of values for the sort options since the onItemSelected method only
    // returns the string representation of the item, not the value associated
    Resources resources = getResources();
    final TypedArray sortArrayValues = resources.obtainTypedArray(R.array.sort_options_values);

    MenuItem sortMenuItem = menu.findItem(R.id.sort_spinner);
    mSortSpinner = (Spinner) MenuItemCompat.getActionView(sortMenuItem);
    mSortSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override//w w w. ja v  a 2s.c o  m
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            if (Utility.getPreferredSortOptionPosition(getActivity()) != position) {
                // Update the shared preferences with the new sort option election
                SharedPreferences sharedPreferences = PreferenceManager
                        .getDefaultSharedPreferences(getActivity());
                SharedPreferences.Editor editor = sharedPreferences.edit();
                //Save the sort_by value to query the movie database
                editor.putString(getString(R.string.sort_preference_key), sortArrayValues.getString(position));
                // Save the current position of the selected sort option
                editor.putInt(getString(R.string.sort_position_preference_key), position);
                editor.commit();

                // Update movie grid to reflect new sort option selected by user
                resetMovieGrid();
                getMovies(PAGE_1);
            }

            else {
                // If the sort option didn't change, the event most likely was triggered by
                // either a rotation or the activity being created. In case of the rotation
                // the state has already been restored in the onCreate() method, after onCreate()
                // the onCreateOptionsMenu() is called. If the activity has been created, the
                // favorite movies need to be fetched
                if (Utility.getPreferredSortOption(getActivity())
                        .equals(getString(R.string.sort_favorites_value))) {
                    if (!mMovies.isEmpty() && ((MainActivity) getActivity()).inTwoPaneLayout())
                        updateDetailsFragment();
                    else if (mMovies.isEmpty())
                        getMovies(Integer.valueOf(mPageToFetch).toString());
                }

                else {
                    // For the other sort options since pagination is involved the default
                    // movie to display in the details fragment when the user hasn't selected
                    // any movie is either the first one in the returned list when it is
                    // first loaded or the first visible movie before rotation. The first one
                    // when is first loaded is instantiated in the onScroll method
                    if (((MainActivity) getActivity()).inTwoPaneLayout()) {
                        if ((mSelectedMovie != -1) && (mMovieAdapter.getCount() != 0)) {
                            Movie selectedMovie = mMovieAdapter.getItem(mSelectedMovie);
                            ((Callback) getActivity()).onItemSelected(selectedMovie);
                        }
                    }
                    // Determine if another page needs to be retrieved by making an API call
                    getMovies(Integer.valueOf(mPageToFetch).toString());
                }

                // Restoring the scroll position
                mMoviesGridView.smoothScrollToPosition(mCurrentScrollPosition);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> parent) {
            //Do nothing
        }
    });

    // This statement is needed to restore the state of the sort selection in case a screen
    // rotation has occurred. The spinner is restored to the position it was retrieved
    // from the SharedPreferences;
    mSortSpinner.setSelection(Utility.getPreferredSortOptionPosition(getActivity()));
}

From source file:at.software2014.trackme.MainActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLinear);
    int[] menuItemIds = { R.id.action_map_refresh, R.id.action_friends_refresh, R.id.action_contact_delete,
            R.id.action_contact_add, R.id.action_contact_invite, R.id.action_zoom_to_friends,
            R.id.action_zoom_to_me };//  w  w  w .  jav a 2 s . co  m

    for (int i = 0; i < menuItemIds.length; i++) {
        MenuItem menuItem = menu.findItem(menuItemIds[i]);
        if (menuItem != null) {
            menuItem.setVisible(!drawerOpen);
        }
    }

    return super.onPrepareOptionsMenu(menu);
}

From source file:br.liveo.navigationliveo.NavigationLiveoAWizard.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.menu, menu);
    menu.findItem(R.id.menu_add).setVisible(true);
    return true;// w w w . j  a v a 2  s .co m
}

From source file:com.android.calculator2.Calculator.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    // Show the leading option when displaying a result.
    menu.findItem(R.id.menu_leading).setVisible(mCurrentState == CalculatorState.RESULT);

    // Show the fraction option when displaying a rational result.
    menu.findItem(R.id.menu_fraction)/*from w  ww.j  a  v  a2 s.  co m*/
            .setVisible(mCurrentState == CalculatorState.RESULT && mEvaluator.getRational() != null);

    return true;
}

From source file:gr.scify.newsum.ui.SearchViewActivity.java

public boolean onCreateOptionsMenu(Menu menu) {
    SharedPreferences userlang = getSharedPreferences("lang", 0);
    String newlang = userlang.getString("lang", Locale.getDefault().getLanguage());
    Setlanguage.updateLanguage(getApplicationContext(), newlang);

    MenuInflater inflater = getMenuInflater();

    inflater.inflate(R.menu.viewactivity, menu);

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) {
        menu.findItem(R.id.share).setVisible(false);
    }/*from   w w  w . ja va2s.com*/
    return true;

}

From source file:co.uk.socialticker.ticker.TickerActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    Boolean toggle = true;//www  .jav  a 2  s .com

    MenuItem update = menu.findItem(R.id.menu_twitter_update);
    MenuItem logout = menu.findItem(R.id.menu_twitter_logout);

    if (!isTwitterLoggedInAlready()) {
        toggle = false;
    }
    update.setEnabled(toggle);
    logout.setEnabled(toggle);

    super.onPrepareOptionsMenu(menu);
    return true;
}

From source file:com.fsm.storybook.launcher.WebViewActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    mOptionsMenu = menu;//from   w w  w.  ja v a2  s . c  o  m

    getMenuInflater().inflate(R.menu.web_view, menu);

    menu.findItem(R.id.settings).setIcon(R.drawable.setting);
    menu.findItem(R.id.settings).setVisible(false);

    menu.findItem(R.id.mo_previous).setIcon(R.drawable.mo_previous);
    menu.findItem(R.id.mo_next).setIcon(R.drawable.mo_next);
    menu.findItem(R.id.mo_play).setIcon(R.drawable.mo_play);
    menu.findItem(R.id.mo_pause).setIcon(R.drawable.mo_pause);

    MenuItem mo_previous = menu.findItem(R.id.mo_previous);
    MenuItem mo_next = menu.findItem(R.id.mo_next);
    MenuItem mo_play = menu.findItem(R.id.mo_play);
    MenuItem mo_pause = menu.findItem(R.id.mo_pause);

    mo_previous.setVisible(mIsMoAvailable);
    mo_next.setVisible(mIsMoAvailable);
    if (mIsMoAvailable) {
        mo_play.setVisible(!mIsMoPlaying);
        mo_pause.setVisible(mIsMoPlaying);
    }

    //force buttons to show up
    /*
    mo_previous.setVisible(true);
    mo_next.setVisible(true);
    mo_play.setVisible(!mIsMoPlaying);
    mo_pause.setVisible(mIsMoPlaying);
    */

    return true;
}

From source file:edu.cens.loci.ui.PlaceViewActivity.java

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);

    if (mPlace.state == Places.STATE_SUGGESTED) {
        menu.findItem(R.id.menu_delete).setVisible(false);
        menu.findItem(R.id.menu_edit).setVisible(false);
        menu.findItem(R.id.menu_block).setVisible(false);
        menu.findItem(R.id.menu_unblock).setVisible(false);
    } else if (mPlace.state == Places.STATE_BLOCKED) {
        menu.findItem(R.id.menu_block).setVisible(false);
        menu.findItem(R.id.menu_unblock).setVisible(true);
    } else if (mPlace.state == Places.STATE_REGISTERED) {
        menu.findItem(R.id.menu_block).setVisible(true);
        menu.findItem(R.id.menu_unblock).setVisible(false);
    }//from   w w w . jav a  2  s  .co m

    return true;
}