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:gc.david.dfm.ui.MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    Mint.leaveBreadcrumb("MainActivity::onCreateOptionsMenu");
    // Inflate the options menu from XML
    final MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);

    // Expandir el EditText de la bsqueda a lo largo del ActionBar
    searchMenuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    // Configure the search info and add any event listeners
    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);

    // Muestra el item de men de cargar si hay elementos en la BD
    final MenuItem loadItem = menu.findItem(R.id.action_load);
    // TODO hacerlo en segundo plano
    final List<Distance> allDistances = getApplicationDaoSession().loadAll(Distance.class);
    if (allDistances.size() == 0) {
        loadItem.setVisible(false);
    }//from www  . java  2s  .c  om
    return super.onCreateOptionsMenu(menu);
}

From source file:com.dsdar.thosearoundme.util.ContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//from  www. ja v a2  s . c o m
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_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 (Util.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) searchItem.getActionView();

        // 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 (Util.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.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 (Util.hasICS()) {
                searchItem.expandActionView();
            }

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

From source file:com.dwdesign.tweetings.activity.ComposeActivity.java

@Override
public void onClick(final View view) {
    switch (view.getId()) {
    case R.id.image_thumbnail_preview: {
        if (mPopupMenu != null) {
            mPopupMenu.dismiss();//from w  w  w .ja  v  a 2 s . c o  m
        }
        mPopupMenu = PopupMenu.getInstance(this, view);
        mPopupMenu.inflate(R.menu.action_attached_image);
        mPopupMenu.setOnMenuItemClickListener(this);

        final Menu menu = mPopupMenu.getMenu();
        final MenuItem itemUpload = menu.findItem(MENU_UPLOAD);
        if (!isNullOrEmpty(mUploadProvider) && itemUpload != null) {
            itemUpload.setVisible(true);
        }

        mPopupMenu.show();
        break;
    }
    }

}

From source file:com.jins_meme.bridge.MainActivity.java

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

    //debug Log.d("DEBUG", "onPrepareOptionsMenu");

    String barTitle;//from   ww w. j  a va2 s  .co m
    if (getSupportActionBar() != null && getSupportActionBar().getTitle() != null) {
        barTitle = getSupportActionBar().getTitle().toString();

        Log.d("DEBUG", "Title = " + barTitle);

        if (barTitle.length() > 1) {
            for (int i = 0; i < menu.size(); i++) {
                MenuItem item = menu.getItem(i);
                String title = item.getTitle().toString();

                if (title.contains(" (for Eye VDJ)")) {
                    title = title.substring(0, title.indexOf(" (for Eye VDJ)"));
                }

                if (barTitle.contains(title)) {
                    item.setVisible(false);
                } else {
                    item.setVisible(true);
                }
            }
        } else {
            for (int i = 0; i < menu.size(); i++) {
                menu.getItem(i).setVisible(true);
            }
        }
    }

    return true;
}

From source file:cgeo.geocaching.cgeocaches.java

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

    try {/*from w  w  w.j  a  va 2  s . c  o  m*/
        if (adapter.isSelectMode()) {
            menu.findItem(MENU_SWITCH_SELECT_MODE).setTitle(res.getString(R.string.caches_select_mode_exit))
                    .setIcon(R.drawable.ic_menu_clear_playlist);
            menu.findItem(MENU_INVERT_SELECTION).setVisible(true);
        } else {
            menu.findItem(MENU_SWITCH_SELECT_MODE).setTitle(res.getString(R.string.caches_select_mode))
                    .setIcon(R.drawable.ic_menu_agenda);
            menu.findItem(MENU_INVERT_SELECTION).setVisible(false);
        }

        final boolean isEmpty = cacheList.isEmpty();
        final boolean isConcrete = isConcreteList();

        setVisible(menu, MENU_SWITCH_SELECT_MODE, !isEmpty);
        setVisible(menu, SUBMENU_MANAGE_HISTORY, !isEmpty);
        setVisible(menu, SUBMENU_SHOW_MAP, !isEmpty);
        setVisible(menu, MENU_SORT, !isEmpty);
        setVisible(menu, MENU_REFRESH_STORED, !isEmpty && (isConcrete || type != CacheListType.OFFLINE));
        setVisible(menu, MENU_DROP_CACHES, !isEmpty);
        setVisible(menu, MENU_DROP_CACHES_AND_LIST, isConcrete && !isEmpty);
        setVisible(menu, MENU_DELETE_EVENTS, isConcrete && !isEmpty && containsEvents());
        setVisible(menu, MENU_MOVE_TO_LIST, !isEmpty);
        setVisible(menu, MENU_EXPORT, !isEmpty);
        setVisible(menu, MENU_REMOVE_FROM_HISTORY, !isEmpty);
        setVisible(menu, MENU_CLEAR_OFFLINE_LOGS, !isEmpty && containsOfflineLogs());

        if (navigationMenu != null) {
            navigationMenu.setVisible(!isEmpty);
        }

        final boolean hasSelection = adapter != null && adapter.getCheckedCount() > 0;
        final boolean isNonDefaultList = isConcrete && listId != StoredList.STANDARD_LIST_ID;

        if (type == CacheListType.OFFLINE || type == CacheListType.HISTORY) { // only offline list
            setMenuItemLabel(menu, MENU_DROP_CACHES, R.string.caches_drop_selected, R.string.caches_drop_all);
            menu.findItem(MENU_DROP_CACHES_AND_LIST)
                    .setVisible(!hasSelection && isNonDefaultList && !adapter.isFiltered());
            setMenuItemLabel(menu, MENU_REFRESH_STORED, R.string.caches_refresh_selected,
                    R.string.caches_refresh_all);
            setMenuItemLabel(menu, MENU_MOVE_TO_LIST, R.string.caches_move_selected, R.string.caches_move_all);
        } else { // search and global list (all other than offline and history)
            setMenuItemLabel(menu, MENU_REFRESH_STORED, R.string.caches_store_selected,
                    R.string.caches_store_offline);
        }

        MenuItem item = menu.findItem(MENU_DROP_LIST);
        if (item != null) {
            item.setVisible(isNonDefaultList);
        }
        item = menu.findItem(MENU_RENAME_LIST);
        if (item != null) {
            item.setVisible(isNonDefaultList);
        }

        final boolean multipleLists = cgData.getLists().size() >= 2;
        item = menu.findItem(MENU_SWITCH_LIST);
        if (item != null) {
            item.setVisible(multipleLists);
        }
        item = menu.findItem(MENU_MOVE_TO_LIST);
        if (item != null) {
            item.setVisible(!isEmpty);
        }

        setMenuItemLabel(menu, MENU_REMOVE_FROM_HISTORY, R.string.cache_remove_from_history,
                R.string.cache_clear_history);
        setMenuItemLabel(menu, MENU_EXPORT, R.string.export, R.string.export);
    } catch (Exception e) {
        Log.e("cgeocaches.onPrepareOptionsMenu", e);
    }

    return true;
}

From source file:at.alladin.rmbt.android.main.RMBTMainActivity.java

/**
 * /*  w  w w . j  a v  a 2 s .com*/
 * @param id
 */
public void setVisibleMenuItems(Integer... id) {
    if (actionBarMenu != null) {
        if (id != null && id.length > 0) {
            Set<Integer> idSet = new HashSet<Integer>();
            Collections.addAll(idSet, id);
            for (int i = 0; i < actionBarMenu.size(); i++) {
                MenuItem item = actionBarMenu.getItem(i);
                if (idSet.contains(item.getItemId())) {
                    item.setVisible(true);
                } else {
                    item.setVisible(false);
                }
            }
        } else {
            for (int i = 0; i < actionBarMenu.size(); i++) {
                MenuItem item = actionBarMenu.getItem(i);
                item.setVisible(false);
            }
        }
    }
}

From source file:de.azapps.mirakel.main_activity.MainActivity.java

public void updateShare() {
    if (this.menu != null) {
        final MenuItem share_list = this.menu.findItem(R.id.share_list);
        if (share_list != null) {
            runOnUiThread(new Runnable() {
                @Override//from ww  w . j  a  v  a 2  s .  co m
                public void run() {
                    if (MainActivity.this.currentList.countTasks() == 0) {
                        share_list.setVisible(false);
                    } else if (MainActivity.this.currentList.countTasks() > 0) {
                        share_list.setVisible(true);
                    }
                }
            });
        } else if ((this.currentPosition == MainActivity.getTasksFragmentPosition()) && (share_list == null)
                && (this.currentList != null) && (this.currentList.countTasks() > 0)
                && !this.mDrawerLayout.isDrawerOpen(DefinitionsHelper.GRAVITY_LEFT)) {
            loadMenu(MainActivity.getTasksFragmentPosition(), true, true);
        }
    }
}

From source file:com.dsdar.thosearoundme.util.MemberAddContactsListFragment.java

@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
@Override//  w w  w . jav a 2  s . c  om
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate the menu items
    inflater.inflate(R.menu.contact_list_menu, menu);
    // Locate the search item
    MenuItem searchItem = menu.findItem(R.id.menu_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 (Util.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) searchItem.getActionView();

        // 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,
                        MemberAddContactsListFragment.this);
                return true;
            }
        });

        if (Util.hasICS()) {
            // This listener added in ICS
            searchItem.setOnActionExpandListener(new MenuItem.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,
                            MemberAddContactsListFragment.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 (Util.hasICS()) {
                searchItem.expandActionView();
            }

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

From source file:com.yahala.ui.LaunchActivity.java

public void updatePresenceMenuIcon() {

    try {//from w  ww.  j  av a  2  s.c  o  m
        MenuItem menuTypeAvailability = menu.findItem(R.id.menu_type_availability);
        if (menuTypeAvailability != null) {
            //FileLog.e("Test updatePresenceMenuIcon","!Null");
            if (XMPPManager.getInstance().currentPresence == XMPPManager.Available) {
                menuTypeAvailability.setIcon(R.drawable.ic_type_available);
            } else if (XMPPManager.getInstance().currentPresence == XMPPManager.Away) {
                menuTypeAvailability.setIcon(R.drawable.ic_type_away);
            } else if (XMPPManager.getInstance().currentPresence == 5) {
                menuTypeAvailability.setIcon(R.drawable.ic_type_unavailable);
            } else if (XMPPManager.getInstance().currentPresence == XMPPManager.DoNotDisturb) {
                menuTypeAvailability.setIcon(R.drawable.ic_type_dns);
            } else if (XMPPManager.getInstance().currentPresence == XMPPManager.FreeToChat) {
                menuTypeAvailability.setIcon(R.drawable.ic_type_available);
            }
            menuTypeAvailability.setVisible(true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

}

From source file:me.tb.player.SkeletonActivity.java

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

    //        MenuItem tile_item = menu.findItem(R.id.tile_count);
    //        MenuItem shuffle = menu.findItem(R.id.shuffle);
    //        MenuItem pass = menu.findItem(R.id.pass);
    MenuItem exit = menu.findItem(R.id.exit);
    //        MenuItem enter = menu.findItem(R.id.enter);
    MenuItem clear = menu.findItem(R.id.clear);
    MenuItem settings = menu.findItem(R.id.action_settings);
    settings.setVisible(false);

    //        if (bl != null) {
    //            tile_item.setTitle("Tiles Remaining: " + bl.getList_of_letters().size());
    //        }/*w w w  .j  a va  2 s  .c  om*/
    if (!myTurn) {
        //            tile_item.setVisible(true);
        //            shuffle.setVisible(false);
        //            pass.setVisible(false);
        exit.setVisible(false);
        //            enter.setVisible(false);
        clear.setVisible(false);
    } else {
        //            tile_item.setVisible(true);
        //            shuffle.setVisible(true);
        //            pass.setVisible(true);
        exit.setVisible(true);
        //            enter.setVisible(true);
        clear.setVisible(true);
    }

    return true;
}