Khawar
Khawar

Reputation: 859

Remove back button from SearchView in toolbar AppCompat

enter image description here

How can I remove the back button icon when search view shows up in Toolbar (AppCompat)?

toolbar = (Toolbar) findViewById(R.id.tool_bar);


    // Set an OnMenuItemClickListener to handle menu item clicks
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Handle the menu item
            return true;
        }
    });

    // Inflate a menu to be displayed in the toolbar
    toolbar.inflateMenu(R.menu.menu_main);
    toolbar.setTitle("");


    setSupportActionBar(toolbar);
    actionBar = getSupportActionBar();

    // this works for normal back button but not for one appears on tapping SearchView
    actionBar.setDisplayHomeAsUpEnabled(false);

Upvotes: 4

Views: 3840

Answers (3)

thepoosh
thepoosh

Reputation: 12587

There is no way to remove the back arrow from a SearchView. you can try finding it yourself in the class (of the support.v7 lib or the main Android project) it looks like this:

mCloseButton = (ImageView) findViewById(R.id.search_close_btn);

But it's a private member and the visibility changes upon text input

Upvotes: 1

Piyush
Piyush

Reputation: 18923

Use:

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

toolbar.setNavigationIcon(null);

OR

toolbar.setNavigationIcon(getResources().getColor(android.R.color.transparent));

Upvotes: 4

Somnath Sarode
Somnath Sarode

Reputation: 477

getSupportActionBar().setDisplayHomeAsUpEnabled(false);

Upvotes: 1

Related Questions