1

A searchview and a submenu are put on my actionbar in my demo like this: @Override public boolean onPrepareOptionsMenu(Menu menu) {

    SearchView searchView = new SearchView(getSupportActionBar()
            .getThemedContext());
    searchView.setQueryHint("Search...");
    searchView.setOnQueryTextListener(new OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {

            if(currentPosition == 0) {
                System.out.println("Now search index 0 data...");
            }else if(currentPosition == 1) {
                System.out.println("Now search index 1 data...");
            }
            return false;
        }
    });

    menu.add(Menu.NONE, 0, 0, "")
            .setIcon(R.drawable.abs__ic_search)
            .setActionView(searchView)
            .setShowAsAction(
                    MenuItem.SHOW_AS_ACTION_IF_ROOM
                            | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

    SubMenu sub = menu.addSubMenu(Menu.NONE, 1, 1, "").setIcon(
            R.drawable.abs__ic_menu_moreoverflow_holo_dark);
    if(currentPosition == 0) {
        sub.add(0, R.style.Theme_Sherlock, 0, "Mail");
        sub.add(0, R.style.Theme_Sherlock_Light, 0, "IM");
    }else if(currentPosition == 1) {
        sub.add(0, R.style.Theme_Sherlock, 0, "City");
        sub.add(0, R.style.Theme_Sherlock_Light, 0, "Relocate");
    }

    sub.getItem().setShowAsAction(
            MenuItem.SHOW_AS_ACTION_ALWAYS
                    | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    return super.onPrepareOptionsMenu(menu);
}

How can I hide the SubMenu when click the SearchView icon on ActionBar using HoloEveryWhere?

Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
matrix1024
  • 27
  • 1
  • 8

1 Answers1

1

Like this:

menu.add(...)
        ...
        .setOnActionExpandListener(new OnActionExpandListener() {

            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                sub.getItem().setVisible(false);
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                sub.getItem().setVisible(true);
                return true;
            }
        })
        ...

Although I'm wondering why you create your own overflow menu as a sub menu. Why don't you use the default overflow functionality?

Matthias Robbers
  • 15,689
  • 6
  • 63
  • 73
  • Hello,My project is using Holo Everywhere,but overflow menu do not present on the actionbar?Do u know why?ManyThanks! – matrix1024 Jan 15 '13 at 06:05
  • I don't think that this is related Holo Everywhere. You need to have menu items that have `android:showAsAction="never"`, respectively `MenuItem#setShowAsAction(MenuItem. SHOW_AS_ACTION_NEVER)`. – Matthias Robbers Jan 15 '13 at 08:52