Jack
Jack

Reputation: 161

Hide SubMenu and SearchView in the Actionbar

I am trying to hide both the SubMenu and the SearchView in the ActionBar. I am using ActionBarSherlock.

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    //Used to put dark icons on light action bar
    boolean isLight = SampleList.THEME == R.style.Theme_Sherlock_Light;         

    //Create the search view
    SearchView searchView = new SearchView(getSupportActionBar().getThemedContext());
    searchView.setQueryHint("Zong ol na...");
    searchView.setOnQueryTextListener(this);
    searchView.setOnSuggestionListener(this);

    searchView.setSuggestionsAdapter(mSuggestionsAdapter);

    SubMenu sub = menu.addSubMenu("Menu");
    sub.add(0, R.style.Theme_Sherlock, 0, "Topic");
    sub.add(0, R.style.Theme_Sherlock_Light, 0, "Thuthlunghlui");        
    sub.getItem().setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS | MenuItem.SHOW_AS_ACTION_WITH_TEXT);

    menu.add("Search")
        .setIcon(isLight ? R.drawable.ic_search_inverse : R.drawable.abs__ic_search)
        .setActionView(searchView)
        .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_COLLAPSE_ACTION_VIEW);

I want to hide SubMenu and SearchView by clicking the button

Btn_Hide_Menu.setOnClickListener(new OnClickListener() 
    {           
        @Override
        public void onClick(View v) 
        { //here I want to hide both the SubMenu and SearchView

I can't sort it out myself. Is this possible? Excuse me I am being too naive. enter image description here

Upvotes: 1

Views: 311

Answers (2)

Tanim reja
Tanim reja

Reputation: 2188

If you have menu in menu_main.xml file like this

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity">
    <item
        android:id="@+id/action_settings"
        android:title="@string/action_settings"
        android:orderInCategory="100"
        app:showAsAction="never" />

    <item
        android:id="@+id/tanim"
        android:title="Books"
        android:orderInCategory="100"
        app:showAsAction="never" />    

</menu>

declare some global variable in your activity class

private Menu menu;
private MenuItem item;
private MenuItem item1;

now look inside your onOptionCreateMenu()

    @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
             item = menu.findItem(R.id.tanim);// hare tanim is menu id comes from xml file..

            menu.add("search"); // this menu add hare no xml id ... so you need the index number of this menu  
            // index is 0=search,1=settings,2=tanim  

            item1=menu.getItem(0);// this is search menu
             return true;
        }

and finally your button click event , hare item,item1 is global var..

button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                item.setVisible(false);
                item1.setVisible(false);

            }
        });

feel free to ask any question... Thank you

Upvotes: 1

Tanim reja
Tanim reja

Reputation: 2188

Try this Hope it works

    Btn_Hide_Menu.setOnClickListener(new OnClickListener() 
        {           
            @Override
            public void onClick(View v) 
            { //here I want to hide both the SubMenu and SearchView
                MenuItem item = menu.findItem(R.id.my_item);
                item.setVisible(false);    
            }    
     }

Upvotes: 1

Related Questions