neteot
neteot

Reputation: 933

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!

Upvotes: 6

Views: 1835

Answers (3)

saff1x
saff1x

Reputation: 21

What worked for me was removing the "action_search" from the menu resource file and instead use a MaterialToolbar that wraps a SearchView in your activity xml layout.

<com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:menu="@menu/your_menu">
        
        <androidx.appcompat.widget.SearchView
            android:id="@+id/searchView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="end"
            app:iconifiedByDefault="true"
            app:queryBackground="@color/transparent"
            app:queryHint="@string/search..." />


    </com.google.android.material.appbar.MaterialToolbar>

In your activity you can then use setSupportActionBar() and pass the toolbar

Upvotes: 0

Bob
Bob

Reputation: 13865

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"/>

Upvotes: 11

jaibatrik
jaibatrik

Reputation: 7523

Change the showAsAction attribute to ifRoom and try -

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

Upvotes: 0

Related Questions