androidlayoutandroid-toolbarandroid-menu

Push icons away when expandig searchview in android toolbar


I'm facing this particular behaviour and I don't know how to fix it..

In my activity I have this toolbar:

enter image description here

And when you click on the search icon it expands and pushes the other icon to out of the view.

enter image description here

I want to know how could I keep the icon on the right and reduce the search view space.

This is my menu layout:

<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:title="@string/buscar"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="always" />

<item
    android:id="@+id/alimentos_basket"
    android:orderInCategory="1"
    android:title="@string/alimentos_anadidos"
    app:showAsAction="always" />

In my activity layout I don't have anything

<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"
tools:context="com.irixgalicia.irixhealth.app.comidaDiaria.AnadirAlimentoActivity">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_anadir_alimento" />

And these is the code of the activity where I inflate

    @Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.menu_anadir_alimento, menu);
    final MenuItem menuItem = menu.findItem(R.id.action_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(menuItem);
    searchView.setOnQueryTextListener(this);

    final MenuItem contadorItem = menu.findItem(R.id.alimentos_basket);
    contadorItem.setIcon(buildCounterDrawable(contadorAlimentos, R.drawable.ic_food_variant_white_36dp));

    return super.onCreateOptionsMenu(menu);
}

In my layout I only have the toolbar, not the icons inside.

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:popupTheme="@style/AppTheme.PopupOverlay">

    </android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

Has anyone knows how to achieve what I'm looking for..

Thank you!


Solution

  • Use ifRoom|collapseActionView for the attribute showAsAction

    <item
            android:id="@+id/action_search"
            android:title="search"
            app:actionViewClass="android.support.v7.widget.SearchView"
            android:icon="@drawable/ic_search"
            app:showAsAction="ifRoom|collapseActionView"/>