Search code examples
androidbuttonsearchview

Change SearchView backbutton in Android


I have Action bar in white color, When I click on search view button, I am able to get the search view but Unable to get the line beneath of search view and unable to see the back button. I want to change the color/complete icon of search view back button and I want to get the line beneath of search view. I Googled for it and tried lot of different ways, but no luck. I have tried changing the toolbar style also, but no luck. I am using search view in activities and Fragments too..

I have tried changing the toolbar style like below

 <style name="ToolbarStyle" parent="@style/ThemeOverlay.AppCompat.ActionBar">
    <!-- Customize color of navigation drawer icon and back arrow -->
    <item name="colorControlNormal">@color/colorBlack</item>
    <item name="android:collapseIcon" tools:ignore="NewApi">@mipmap/filter_red</item>
</style>

Any help would be Grateful!!!!

Thanks in Advance


Solution

  • (i) You can include app:collapseIcon attribute in your Toolbar.

    <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:elevation="2dp"
                app:collapseIcon="@drawable/cancel"
                app:layout_scrollFlags="scroll|enterAlways"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    

    (ii) Another way is to do that programmatically, with the help of AppBarLayout.

    AppBarLayout appBar =  findViewById(R.id.appBarLayout);
            appBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    for (int i = 0; i < toolbar.getChildCount(); i++) {
                        View view = toolbar.getChildAt(i);
                        if (view instanceof ImageButton) {
                            ImageButton btn = (ImageButton) view;
                            btn.setImageDrawable(getResources().getDrawable(R.drawable.cancel)); // Here we can change icon.
                        }
                    }
                }
            });
    

    Layout will be as follows:

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="?attr/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:elevation="2dp"
            app:layout_scrollFlags="scroll|enterAlways"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    
    </android.support.design.widget.AppBarLayout>