Example usage for android.view MenuItem getActionView

List of usage examples for android.view MenuItem getActionView

Introduction

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

Prototype

public View getActionView();

Source Link

Document

Returns the currently set action view for this menu item.

Usage

From source file:org.openintents.shopping.ui.ShoppingActivity.java

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

    boolean drawerOpen = mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mDrawerListsView);
    boolean holoSearch = PreferenceActivity.getUsingHoloSearchFromPrefs(this);
    // TODO: supposed to hide content-related actions when the drawer is open.

    // TODO: Add item-specific menu items (see NotesList.java example)
    // like edit, strike-through, delete.

    // Add menu option for auto adding items from string array in intent
    // extra if they exist
    if (mExtraItems == null) {
        menu.removeItem(MENU_INSERT_FROM_EXTRAS);
    }//from www .j a  va2s  .  c  o m

    // Selected list:
    long listId = getSelectedListId();

    // set menu title for change mode
    MenuItem menuItem = menu.findItem(MENU_PICK_ITEMS);

    if (mItemsView.mMode == MODE_ADD_ITEMS) {
        menuItem.setTitle(R.string.menu_start_shopping);
        menuItem.setIcon(android.R.drawable.ic_menu_myplaces);
    } else {
        menu.findItem(MENU_PICK_ITEMS).setTitle(R.string.menu_pick_items);
        menuItem.setIcon(android.R.drawable.ic_menu_add);
    }

    menuItem = menu.findItem(MENU_SEARCH_ADD);
    if (menuItem != null) {
        menuItem.setVisible(holoSearch && !drawerOpen);
        if (!holoSearch) {
            mAddPanel.setVisibility(View.VISIBLE);
        }

        View searchView = menuItem.getActionView();
        int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
        View imageView = searchView.findViewById(searchImgId);
        if (imageView instanceof ImageView) {
            ((ImageView) imageView).setImageResource(android.R.drawable.ic_menu_add);
        }

    }

    menuItem = menu.findItem(MENU_SYNC_WEAR);
    if (menuItem != null) {
        menuItem.setVisible(mItemsView.isWearSupportAvailable());
    }

    menuItem = menu.findItem(MENU_MARK_ALL_ITEMS).setVisible(mItemsView.mNumUnchecked > 0);
    menuItem = menu.findItem(MENU_UNMARK_ALL_ITEMS).setVisible(mItemsView.mNumChecked > 0);

    menuItem = menu.findItem(MENU_CLEAN_UP_LIST).setEnabled(mItemsView.mNumChecked > 0).setVisible(!drawerOpen);

    // Delete list is possible, if we have more than one list:
    // AND
    // the current list is not the default list (listId == 0) - issue #105
    // TODO: Later, the default list should be user-selectable,
    // and not deletable.

    // TODO ???
    /*
     * menu.setItemShown(MENU_DELETE_LIST, mCursorListFilter.count() > 1 &&
     * listId != 1); // 1 is hardcoded number of default first list.
     */

    // The following code is put from onCreateOptionsMenu to
    // onPrepareOptionsMenu,
    // because the URI of the shopping list can change if the user switches
    // to another list.
    // Generate any additional actions that can be performed on the
    // overall list. This allows other applications to extend
    // our menu with their own actions.
    Intent intent = new Intent(null, getIntent().getData());
    intent.addCategory(Intent.CATEGORY_ALTERNATIVE);
    // menu.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
    // new ComponentName(this, NoteEditor.class), null, intent, 0, null);

    // Workaround to add icons:
    MenuIntentOptionsWithIcons menu2 = new MenuIntentOptionsWithIcons(this, menu);
    menu2.addIntentOptions(Menu.CATEGORY_ALTERNATIVE, 0, 0,
            new ComponentName(this, org.openintents.shopping.ShoppingActivity.class), null, intent, 0, null);

    return true;
}

From source file:org.brandroid.openmanager.activities.OpenExplorer.java

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (DEBUG)// w w w .  j  a  v a2 s  . co m
        Logger.LogDebug("OpenExplorer.onOptionsItemSelected(" + item + ")");
    if (item.getItemId() == R.id.menu_more && isGTV()) {
        showMenu(mOptsMenu, findViewById(item.getItemId()), false);
        return true;
    }

    if (item.getSubMenu() != null) {
        onPrepareOptionsMenu(item.getSubMenu());
        if (!USE_PRETTY_MENUS)
            return false;
        else {
            View anchor = findViewById(item.getItemId());
            if (anchor == null && !BEFORE_HONEYCOMB && item.getActionView() != null)
                anchor = item.getActionView();
            if (anchor == null && !BEFORE_HONEYCOMB) {
                anchor = getActionBar().getCustomView();
                if (anchor.findViewById(item.getItemId()) != null)
                    anchor = anchor.findViewById(item.getItemId());
            }
            if (anchor == null)
                anchor = mToolbarButtons;
            if (anchor == null)
                anchor = findViewById(android.R.id.home);
            if (anchor == null && !BEFORE_HONEYCOMB && USE_ACTION_BAR)
                anchor = getActionBar().getCustomView().findViewById(android.R.id.home);
            if (anchor == null)
                anchor = getCurrentFocus().getRootView();
            OpenFragment f = getSelectedFragment();
            if (f != null)
                if (f.onClick(item.getItemId(), anchor))
                    return true;
        }
    }

    if (item.isCheckable())
        item.setChecked(item.getGroupId() > 0 ? true : !item.isChecked());

    OpenFragment f = getSelectedFragment();
    if (f != null && f.onOptionsItemSelected(item))
        return true;

    if (DEBUG)
        Logger.LogDebug("OpenExplorer.onOptionsItemSelected(0x" + Integer.toHexString(item.getItemId()) + ")");

    return onClick(item.getItemId(), item, null);
}

From source file:com.lgallardo.qbittorrentclient.RefreshListener.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    // Retrieve the SearchView and plug it into SearchManager
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menu.findItem(R.id.action_search));
    SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default

    // Handle open/close SearchView (using an item menu)
    final MenuItem menuItem = menu.findItem(R.id.action_search);

    // When back is pressed or the SearchView is close, delete the query field
    // and close the SearchView using the item menu
    searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
        @Override//  www. j a va2 s .c o m
        public void onFocusChange(View view, boolean queryTextFocused) {
            if (!queryTextFocused) {
                menuItem.collapseActionView();
                searchView.setQuery("", false);
                searchField = "";

                refreshSwipeLayout();
                refreshCurrent();
            }
        }
    });

    // This must be implemented to override defaul searchview behaviour, in order to
    // make it work with tablets
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {

            //Log.d("Debug", "onQueryTextSubmit - searchField: " + query);

            // false: don't actually send the query. We are going to do something different
            searchView.setQuery(query, false);

            // Set the variable we use in the intent to perform the search
            searchField = query;

            // Refresh using the search field
            refreshSwipeLayout();
            refreshCurrent();

            // Close the soft keyboard
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(menuItem.getActionView().getWindowToken(), 0);

            // Here true means, override default searchview query
            return true;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            return false;
        }
    });

    // There is a bug setting the hint from searchable.xml, so force it
    searchView.setQueryHint(getResources().getString(R.string.search_hint));

    return true;
}