Example usage for android.view MenuItem setVisible

List of usage examples for android.view MenuItem setVisible

Introduction

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

Prototype

public MenuItem setVisible(boolean visible);

Source Link

Document

Sets the visibility of the menu item.

Usage

From source file:learn2crack.activities.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override/*from ww  w  .ja v a 2 s  .c  o  m*/
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    //inflater.inflate(R.menu.contact_list_menu, menu);
    //inflater.inflate(R.menu.menu_main, menu);
    // Locate the search item
    //MenuItem searchItem = menu.findItem(R.id.menu_search);
    MenuItem searchItem = menu.findItem(R.id.action_search);

    // In versions prior to Android 3.0, hides the search item to prevent additional
    // searches. In Android 3.0 and later, searching is done via a SearchView in the ActionBar.
    // Since the search doesn't create a new Activity to do the searching, the menu item
    // doesn't need to be turned off.
    if (mIsSearchResultView) {
        searchItem.setVisible(false);
    }

    // In version 3.0 and later, sets up and configures the ActionBar SearchView
    if (Utils.hasHoneycomb()) {

        // Retrieves the system search manager service
        final SearchManager searchManager = (SearchManager) getActivity()
                .getSystemService(Context.SEARCH_SERVICE);

        // Retrieves the SearchView from the search menu item
        final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        //final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //toolbar.setNavigationContentDescription(new Toolbar.On);
        //searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Assign searchable info to SearchView
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));

        // Set listeners for SearchView
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String queryText) {
                // Nothing needs to happen when the user submits the search string
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                // Called when the action bar search text has changed.  Updates
                // the search filter, and restarts the loader to do a new query
                // using the new search string.
                String newFilter = !TextUtils.isEmpty(newText) ? newText : null;

                // Don't do anything if the filter is empty
                if (mSearchTerm == null && newFilter == null) {
                    return true;
                }

                // Don't do anything if the new filter is the same as the current filter
                if (mSearchTerm != null && mSearchTerm.equals(newFilter)) {
                    return true;
                }

                // Updates current filter to new filter
                mSearchTerm = newFilter;

                // Restarts the loader. This triggers onCreateLoader(), which builds the
                // necessary content Uri from mSearchTerm.
                mSearchQueryChanged = true;
                getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                return true;
            }
        });

        if (Utils.hasICS()) {
            // This listener added in ICS
            MenuItemCompat.setOnActionExpandListener(searchItem, new MenuItemCompat.OnActionExpandListener() {
                @Override
                public boolean onMenuItemActionExpand(MenuItem menuItem) {
                    // Nothing to do when the action item is expanded
                    return true;
                }

                @Override
                public boolean onMenuItemActionCollapse(MenuItem menuItem) {
                    // When the user collapses the SearchView the current search string is
                    // cleared and the loader restarted.
                    if (!TextUtils.isEmpty(mSearchTerm)) {
                        onSelectionCleared();
                    }
                    mSearchTerm = null;
                    getLoaderManager().restartLoader(ContactsQuery.QUERY_ID, null, ContactsListFragment.this);
                    return true;
                }
            });
        }

        if (mSearchTerm != null) {
            // If search term is already set here then this fragment is
            // being restored from a saved state and the search menu item
            // needs to be expanded and populated again.

            // Stores the search term (as it will be wiped out by
            // onQueryTextChange() when the menu item is expanded).
            final String savedSearchTerm = mSearchTerm;

            // Expands the search menu item
            if (Utils.hasICS()) {
                searchItem.expandActionView();
            }

            // Sets the SearchView to the previous search string
            searchView.setQuery(savedSearchTerm, false);
        }
    }
}

From source file:com.geecko.QuickLyric.fragment.LyricsViewFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    MainActivity mainActivity = (MainActivity) getActivity();
    if (mainActivity == null)
        return;//from  w w w . j a  va2 s  .  c  o m
    CollapsingToolbarLayout toolbarLayout = (CollapsingToolbarLayout) mainActivity
            .findViewById(R.id.toolbar_layout);
    toolbarLayout.setTitle(getString(R.string.app_name));

    if (((DrawerLayout) mainActivity.drawer) // drawer is locked
            .getDrawerLockMode(mainActivity.drawerView) == DrawerLayout.LOCK_MODE_LOCKED_CLOSED)
        return;

    inflater.inflate(lyrics, menu);
    // Get the SearchView and set the searchable configuration
    final MaterialSuggestionsSearchView materialSearchView = (MaterialSuggestionsSearchView) mainActivity
            .findViewById(R.id.material_search_view);
    materialSearchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(final String query) {
            materialSearchView.setSuggestions(null);
            materialSearchView.requestFocus();
            materialSearchView.post(new Runnable() {
                @Override
                public void run() {
                    ((InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE))
                            .hideSoftInputFromWindow(materialSearchView.getWindowToken(), 0);
                }
            });
            materialSearchView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    ((MainActivity) getActivity()).search(query);
                    materialSearchView.setSuggestions(null);
                }
            }, 90);
            mExpandedSearchView = false;
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            if (!materialSearchView.hasSuggestions())
                materialSearchView.setSuggestions(materialSearchView.getHistory());
            return true;
        }
    });

    materialSearchView.setOnSearchViewListener(new MaterialSearchView.SearchViewListener() {
        @Override
        public void onSearchViewShown() {
            if (getActivity() == null)
                return;
            ((ControllableAppBarLayout) getActivity().findViewById(R.id.appbar)).expandToolbar(true);
            mExpandedSearchView = true;
        }

        @Override
        public void onSearchViewClosed() {
            mExpandedSearchView = false;
        }
    });

    final Resources resources = getResources();
    final int resourceId = resources.getIdentifier("status_bar_height", "dimen", "android");
    int statusBarHeight;
    if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP)
        statusBarHeight = 0;
    else if (resourceId > 0)
        statusBarHeight = resources.getDimensionPixelSize(resourceId);
    else
        statusBarHeight = (int) Math.ceil((Build.VERSION.SDK_INT >= Build.VERSION_CODES.M ? 24 : 25)
                * resources.getDisplayMetrics().density);
    CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams) materialSearchView.getLayoutParams();
    lp.setMargins(lp.leftMargin, statusBarHeight, lp.rightMargin, lp.bottomMargin);
    materialSearchView.setLayoutParams(lp);

    MenuItem searchItem = menu.findItem(R.id.action_search);
    materialSearchView.setMenuItem(searchItem);

    if (!materialSearchView.isSearchOpen() && mExpandedSearchView) {
        materialSearchView.showSearch();
        mExpandedSearchView = false;
    } else if (!mExpandedSearchView)
        materialSearchView.closeSearch();

    materialSearchView.setHint(getString(R.string.search_hint));
    if (mSearchQuery != null && !mSearchQuery.equals("")) {
        searchItem.expandActionView();
        materialSearchView.setQuery(mSearchQuery, false);
        if (mSearchFocused)
            materialSearchView.requestFocus();
        else
            materialSearchView.clearFocus();
        mSearchQuery = null;
    }
    Lyrics storedLyrics = mLyrics == null ? null
            : DatabaseHelper.getInstance(getActivity()).get(new String[] { mLyrics.getArtist(),
                    mLyrics.getTitle(), mLyrics.getOriginalArtist(), mLyrics.getOriginalTrack() });

    MenuItem saveMenuItem = menu.findItem(R.id.save_action);
    if (saveMenuItem != null) {
        SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(getActivity());
        if (mLyrics == null)
            saveMenuItem.setVisible(false);
        else if (mLyrics.getFlag() == Lyrics.POSITIVE_RESULT && sharedPref.getBoolean("pref_auto_save", true)) {
            if (storedLyrics == null || (mLyrics.isLRC() && !storedLyrics.isLRC())) {
                lyricsPresentInDB = true;
                new WriteToDatabaseTask().execute(this, saveMenuItem, mLyrics);
            }
            saveMenuItem.setVisible(false);
        } else {
            saveMenuItem.setIcon(lyricsPresentInDB ? R.drawable.ic_trash : R.drawable.ic_save);
            saveMenuItem.setTitle(lyricsPresentInDB ? R.string.remove_action : R.string.save_action);
        }
    }
    MenuItem resyncMenuItem = menu.findItem(R.id.resync_action);
    MenuItem convertMenuItem = menu.findItem(R.id.convert_action);
    if (resyncMenuItem != null)
        resyncMenuItem.setVisible(mLyrics != null && mLyrics.isLRC());
    if (convertMenuItem != null) {
        Lyrics stored = mLyrics == null || mLyrics.isLRC() ? null : storedLyrics;
        convertMenuItem
                .setVisible((mLyrics != null && (mLyrics.isLRC())) || (stored != null && stored.isLRC()));
        convertMenuItem.setTitle(stored == null ? R.string.full_text_action : R.string.pref_lrc);
    }

    MenuItem shareMenuItem = menu.findItem(R.id.share_action);
    if (shareMenuItem != null)
        shareMenuItem.setVisible(
                mLyrics != null && mLyrics.getFlag() == Lyrics.POSITIVE_RESULT && mLyrics.getURL() != null);
}

From source file:com.ksharkapps.musicnow.ui.activities.AudioPlayerActivity.java

/**
 * {@inheritDoc}/*from   ww w.j  a va2  s .  com*/
 */
@Override
public boolean onPrepareOptionsMenu(final Menu menu) {
    // Hide the EQ option if it can't be opened
    MenuItem fav = menu.findItem(R.id.menu_favorite);

    final Intent intent = new Intent(AudioEffect.ACTION_DISPLAY_AUDIO_EFFECT_CONTROL_PANEL);
    if (getPackageManager().resolveActivity(intent, 0) == null) {
        final MenuItem effects = menu.findItem(R.id.menu_audio_player_equalizer);
        effects.setVisible(false);
    }
    if (MusicUtils.mService != null && MusicUtils.getCurrentAudioId() != -1) {
        if (MusicUtils.isFavorite()) {
            fav.setIcon(R.drawable.ic_favorited);
        } else {
            fav.setIcon(R.drawable.ic_unfavorited);
            // Theme chooser
        }
    }
    return true;
}

From source file:com.tweetlanes.android.view.BaseLaneActivity.java

public boolean configureOptionsMenu(Menu menu) {

    Integer defaultOptionsMenu = getDefaultOptionsMenu();
    if (defaultOptionsMenu != null) {
        MenuInflater inflater = getMenuInflater();

        BaseLaneFragment fragment = mLaneFragmentHashMap.get(getCurrentLaneIndex());

        if (fragment != null) {
            if (fragment.configureOptionsMenu(inflater, menu) == false) {
                inflater.inflate(defaultOptionsMenu.intValue(), menu);
            }//  w ww  .  j a va  2 s.com
        } else {
            inflater.inflate(defaultOptionsMenu.intValue(), menu);
        }

        if (menu != null && App.getActionLauncherInstalled() == true) {
            MenuItem buyALP = menu.findItem(R.id.action_buy_alp);
            if (buyALP != null) {
                buyALP.setVisible(false);
            }
        }

        configureActionBarSearchView(menu);
    }
    return true;
}

From source file:gc.david.dfm.ui.activity.MainActivity.java

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

    searchMenuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) searchMenuItem.getActionView();
    final SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    // Indicamos que la activity actual sea la buscadora
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setSubmitButtonEnabled(false);
    searchView.setQueryRefinementEnabled(true);
    searchView.setIconifiedByDefault(true);

    // TODO: 16.01.17 move this to presenter
    final MenuItem loadItem = menu.findItem(R.id.action_load);
    loadDistancesUseCase.execute(new LoadDistancesUseCase.Callback() {
        @Override//  w w w  . j  a va 2  s .  c o m
        public void onDistanceListLoaded(final List<Distance> distanceList) {
            if (distanceList.isEmpty()) {
                loadItem.setVisible(false);
            }
        }

        @Override
        public void onError() {
            loadItem.setVisible(false);
        }
    });

    menu.findItem(R.id.action_crash).setVisible(!isReleaseBuild());

    return super.onCreateOptionsMenu(menu);
}

From source file:at.jclehner.rxdroid.ui.DoseLogFragment.java

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    final int titleResId = isAllCollapsed() ? R.string._title_expand : R.string._title_collapse;

    MenuItem item = menu.add(0, MENU_COLLAPSE_EXPAND, 0, titleResId)
            .setIcon(isAllCollapsed() ? R.drawable.ic_action_expand_white : R.drawable.ic_action_collapse_white)
            .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {

                @Override/* ww w .  jav  a2s .  c o  m*/
                public boolean onMenuItemClick(MenuItem item) {
                    final boolean isAllCollapsed = isAllCollapsed();

                    if (isAllCollapsed)
                        expandAll(true);
                    else
                        collapseAll();

                    Settings.putBoolean(Keys.LOG_IS_ALL_COLLAPSED, !isAllCollapsed);
                    //invalidateOptionsMenu();

                    return true;
                }
            });

    item.setVisible(!getListAdapter().isEmpty());
    MenuItemCompat.setShowAsAction(item, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);

    super.onCreateOptionsMenu(menu, inflater);
}

From source file:github.popeen.dsub.activity.SubsonicActivity.java

private void setDrawerItemVisible(int id, boolean visible) {
    MenuItem item = drawerList.getMenu().findItem(id);
    if (item != null) {
        item.setVisible(visible);
    }/* w  w w  . ja v a  2  s  .  c  om*/
}

From source file:com.android.calendar.AllInOneActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    mOptionsMenu = menu;/*from  www.  ja  va2 s  .  com*/
    getMenuInflater().inflate(R.menu.all_in_one_title_bar, menu);

    // Add additional options (if any).
    Integer extensionMenuRes = mExtensions.getExtensionMenuResource(menu);
    if (extensionMenuRes != null) {
        getMenuInflater().inflate(extensionMenuRes, menu);
    }

    MenuItem item = menu.findItem(R.id.action_import);
    item.setVisible(ImportActivity.hasThingsToImport(this));

    mSearchMenu = menu.findItem(R.id.action_search);
    mSearchView = (SearchView) MenuItemCompat.getActionView(mSearchMenu);
    if (mSearchView != null) {
        Utils.setUpSearchView(mSearchView, this);
        mSearchView.setOnQueryTextListener(this);
        mSearchView.setOnSuggestionListener(this);
    }

    // Hide the "show/hide controls" button if this is a phone
    // or the view type is "Month" or "Agenda".

    mControlsMenu = menu.findItem(R.id.action_hide_controls);
    if (!mShowCalendarControls) {
        if (mControlsMenu != null) {
            mControlsMenu.setVisible(false);
            mControlsMenu.setEnabled(false);
        }
    } else if (mControlsMenu != null && mController != null
            && (mController.getViewType() == ViewType.MONTH || mController.getViewType() == ViewType.AGENDA)) {
        mControlsMenu.setVisible(false);
        mControlsMenu.setEnabled(false);
    } else if (mControlsMenu != null) {
        mControlsMenu.setTitle(mHideControls ? mShowString : mHideString);
    }

    MenuItem menuItem = menu.findItem(R.id.action_today);
    if (Utils.isJellybeanOrLater()) {
        // replace the default top layer drawable of the today icon with a
        // custom drawable that shows the day of the month of today
        LayerDrawable icon = (LayerDrawable) menuItem.getIcon();
        Utils.setTodayIcon(icon, this, mTimeZone);
    } else {
        menuItem.setIcon(R.drawable.ic_menu_today_no_date_holo_light);
    }
    return true;
}

From source file:com.android.calendar.AllInOneActivity.java

private void clearOptionsMenu() {
    if (mOptionsMenu == null) {
        return;//from w w w .j av a2  s  .  co m
    }
    MenuItem cancelItem = mOptionsMenu.findItem(R.id.action_cancel);
    if (cancelItem != null) {
        cancelItem.setVisible(false);
    }
}

From source file:com.tweetlanes.android.core.view.BaseLaneActivity.java

boolean configureOptionsMenu(Menu menu) {

    Integer defaultOptionsMenu = getDefaultOptionsMenu();
    if (defaultOptionsMenu != null) {
        MenuInflater inflater = getMenuInflater();

        BaseLaneFragment fragment = mLaneFragmentHashMap.get(getCurrentLaneIndex());

        if (fragment != null) {
            if (!fragment.configureOptionsMenu(inflater, menu)) {
                inflater.inflate(defaultOptionsMenu.intValue(), menu);
            }/*w w  w. j  ava 2 s .com*/
        } else {
            inflater.inflate(defaultOptionsMenu.intValue(), menu);
        }

        if (menu != null && App.getActionLauncherInstalled()) {
            MenuItem buyALP = menu.findItem(R.id.action_buy_alp);
            if (buyALP != null) {
                buyALP.setVisible(false);
            }
        }

        configureActionBarSearchView(menu);
    }
    return true;
}