Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Toolbar color/style on SearchView click

I have simple question.

How I can change Toolbar's color/or theme to white when clicked on Search icon?

I want Toolbar to be White(with back arrow colored black/grey) whenever search icon is clicked. I have added SearcView (android.support.v7.widget.SearchView) in MenuItem

This enter image description here

To

enter image description here

And then back to original Toolbar color when Back button is clicked.

like image 446
Faizan Mubasher Avatar asked Dec 30 '15 06:12

Faizan Mubasher


People also ask

How do I change the color of my ToolBar menu?

Open the colors. xml file by navigating to the app -> res -> values -> colors. xml. Create a color tag inside the resources tag with a name and set a color with its hex code.

How do I change the color on SearchView?

Change the text colors and icons of searchview in action bar. Expand the search view by setting iconified and iconifiedByDefault flags to false. Find views inside the search view and set the colors as per your wish. Find views of the icons you wish to change and replace them with your icons.


1 Answers

enter image description here

In Activity/Fragment code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.activity_menu, menu);

    MenuItem searchMenuItem = menu.findItem(R.id.search);
    if (searchMenuItem == null) {
        return true;
    }

    searchView = (SearchView) searchMenuItem.getActionView();
    MenuItemCompat.setOnActionExpandListener(searchMenuItem, new MenuItemCompat.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            // Set styles for expanded state here
            if (getSupportActionBar() != null) {
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.RED));
            }
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            // Set styles for collapsed state here
            if (getSupportActionBar() != null) {
                getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.BLUE));
            }
            return true;
        }
    });

    return true;
}

And actual menu's xml is:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/search"
        android:title="@string/search_title"
        android:icon="@drawable/ic_menu_search"
        app:showAsAction="always|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

To change the color of back/X-buttons for expanded view add this into your styles:

<item name="colorControlNormal">@color/my_great_color</item>

To make change of the color smoother - you can animate it: Animate change of view background color on Android

I hope, it helps

like image 104
Konstantin Loginov Avatar answered Sep 20 '22 00:09

Konstantin Loginov