3

Does anyone know how to hide the back button in AppCompat v21 searchview? (outlined by green line)

searchview back button

I've searched a lot but couldn't find anything useful.

menu_main.xml:

<item android:id="@+id/search"
        android:title="@string/search_title"
        app:showAsAction="always|collapseActionView"
        android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
        android:orderInCategory="300"
        app:actionViewClass="android.support.v7.widget.SearchView" />

<item android:id="@+id/action_home"
    android:title="Home"
    android:icon="@drawable/v_home"
    app:showAsAction="always"
    android:orderInCategory="180"/>

<item android:id="@+id/action_favorites"
    android:title="Favorites"
    android:icon="@drawable/v_favorites"
    app:showAsAction="always" />

MainActivity:

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_main, menu);

        firstMenu = menu;


        searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

        MenuItem searchItem = menu.findItem(R.id.search);
        SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setSubmitButtonEnabled(true);
        searchView.setActivated(true);


        searchView.setOnSearchClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                menuItemsVisibility(false);
            }
        });
        searchView.setOnCloseListener(new SearchView.OnCloseListener() {
            @Override
            public boolean onClose() {
                menuItemsVisibility(true);
                return false;
            }
        });

        return true;
    }

    @Override
    public void onBackPressed() {
        menuItemsVisibility(true);
        super.onBackPressed();
    }

// setting visibility of menu items on search
private void menuItemsVisibility(boolean visibility) {

    MenuItem homeItem = firstMenu.findItem(R.id.action_home);
    MenuItem favoriteItem = firstMenu.findItem(R.id.action_favorites);
    MenuItem otItem = firstMenu.findItem(R.id.action_ot);
    MenuItem ntItem = firstMenu.findItem(R.id.action_nt);
    homeItem.setVisible(visibility);
    favoriteItem.setVisible(visibility);
    otItem.setVisible(visibility);
    ntItem.setVisible(visibility);
}

Note: the behavior showAsAction:Always and using methods menuItemsVisibility() to adjust the visibility of toolbar items is intentional.

Another Note: MainActivity extends ActionBarActivity and it also implements implements ObservableScrollViewCallbacks from ObservableScrollView Library.

James
  • 53
  • 1
  • 7
  • 2
    set `getSupportActionBar().setDisplayHomeAsUpEnabled(false);` in your activity. @James – Pooja Mar 27 '15 at 06:02
  • I have already added getSupportActionBar().setDisplayHomeAsUpEnabled(false); to my activity but the back button still appears – James Mar 27 '15 at 06:28
  • can you post your code here. I might be able to figure out. @James – Pooja Mar 27 '15 at 06:31
  • I posted relevant code. @Pooja – James Mar 27 '15 at 06:44
  • I am a bit confused by your code. Your back button is coming from menu.xml? @James – Pooja Mar 27 '15 at 06:50
  • 3
    the back button is part of SearchView. When user press the search button, the SearchView shows a back button which if the user presses make the searchView disappear. The back button is NOT a back button to previous activity. There is only one Activity in my app. The back button is part of SearchView. – James Mar 27 '15 at 06:59
  • sorry for my misunderstanding. Of course my answer won't work for you. @James – Pooja Mar 27 '15 at 07:10
  • I tried to style SearchView and make the back button disappear but didn't work. – James Mar 27 '15 at 07:26
  • 1
    check this question it might help you. http://stackoverflow.com/questions/27348479/how-to-customize-the-up-button-when-the-searchview-is-being-expanded @James – Pooja Mar 30 '15 at 06:33

3 Answers3

5

changed app:showAsAction="always|collapseActionView" to app:showAsAction="always"

Libin Thomas
  • 1,132
  • 13
  • 17
  • It does nothing! I tried `ifRoom` and `always` for SearchView but it won't hide back button: https://stackoverflow.com/questions/56410048/searchview-in-expanded-mode-doesnt-hide-all-action-bar-icons-starting-from-mars – user924 Jun 01 '19 at 20:44
1

Use the method:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

in order to remove the home button from the action bar.

Raptor
  • 53,206
  • 45
  • 230
  • 366
Hanish Sharma
  • 869
  • 8
  • 23
0

It's not perfectly safe method because back button (navigation up) doesn't have id. But if you are using AppCompat with Toolbar, you can use this code to find it. It should be the first in the layout.

  int count = this.getToolbar().getChildCount();

    for(int i = 0; i < count; ++i) {
        View v = this.getToolbar().getChildAt(i);
        if(v instanceof ImageButton) {
            return (ImageButton)v;
        }
    }

Call this method inside onPrepareOptionsMenu if you want to change drawable of that button.

Warlock
  • 2,481
  • 4
  • 31
  • 45