I am having a Toolbar in my application with SearchView Widget and there are two icons one for search icon and the other is just a simple having grid drawable. when ever I click the search button my toolbar is changes into SearchView but when ever I touch the grid button my toolbar again the SearchView becomes visible however I don't want to open SearchView on grid button click. I only want it to be visible for my search button.
Code:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.vid_search_menu_items, menu);
//Add Tint to icon
Drawable drawable = menu.findItem(R.id.action_grid).getIcon();
drawable = DrawableCompat.wrap(drawable);
DrawableCompat.setTint(drawable, ContextCompat.getColor(this,R.color.white));
menu.findItem(R.id.action_grid).setIcon(drawable);
MenuItem menuItem = menu.findItem(R.id.action_search);
SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
searchView.setOnQueryTextListener(this);
MenuItemCompat.setOnActionExpandListener(menuItem, new MenuItemCompat.OnActionExpandListener() {
@Override
public boolean onMenuItemActionCollapse(MenuItem item) {
// Do something when collapsed
// Toast.makeText(getActivity(),"collapsed",Toast.LENGTH_LONG).show();
searchMeta.setVisibility(View.GONE);
return true; // Return true to collapse action view
}
@Override
public boolean onMenuItemActionExpand(MenuItem item) {
// Do something when expanded
// Toast.makeText(getActivity(),"expanded",Toast.LENGTH_LONG).show();
searchMeta.setVisibility(View.VISIBLE);
return true; // Return true to expand action view
}
});
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_search:
Toast.makeText(this,"search",Toast.LENGTH_LONG).show();
break;
case R.id.action_grid:
Toast.makeText(this,"search",Toast.LENGTH_LONG).show();
isList = !isList;
if(isList){
//Change Item Icon
Drawable myDrawable = ContextCompat.getDrawable(this, R.drawable.ic_list_2);
myDrawable = DrawableCompat.wrap(myDrawable);
DrawableCompat.setTint(myDrawable, ContextCompat.getColor(this,R.color.white));
item.setIcon(myDrawable);
}else{
Drawable myDrawable = ContextCompat.getDrawable(this, R.drawable.ic_grid_2);
myDrawable = DrawableCompat.wrap(myDrawable);
DrawableCompat.setTint(myDrawable, ContextCompat.getColor(this,R.color.white));
item.setIcon(myDrawable);
}
break;
}
Toolbar Layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_frag"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.AppBarOverlay"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/tool_bar_icon"
android:layout_gravity="center_horizontal"
android:text="Videos"
android:id="@+id/page_title"
android:textStyle="bold"
/>
</android.support.v7.widget.Toolbar>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2"
android:layout_gravity="center_horizontal"
android:layout_margin="16dp"
android:visibility="gone"
android:id="@+id/search_meta"
>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/brands"
android:layout_marginEnd="5dp"
android:popupBackground="#fff"
android:theme="@style/CustomSpinnerStyle"
/>
<Spinner
android:theme="@style/CustomSpinnerStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/categories"
android:layout_marginStart="5dp"
android:popupBackground="#fff"
/>
</LinearLayout>
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
items file:
<?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/action_search"
android:icon="@drawable/ic_action_search"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:tint="@color/white"
/>
<item
android:id="@+id/action_grid"
android:icon="@drawable/ic_grid_2"
android:title="Search"
app:actionViewClass="android.support.v7.widget.SearchView"
app:showAsAction="always|collapseActionView"
android:tint="@color/white"
/>
</menu>
Remove
app:actionViewClass="android.support.v7.widget.SearchView"
from the second item and see the magic :)