Search code examples
androidandroid-layoutandroid-toolbarsearchview

Searchview overlaps custom title in toolbar. How to prevent?


My toolbar contains :

  • my own views at start of the toolbar and
  • then a standard iconized searchview, which can be expanded by clicking the search icon:

enter image description here

When clicking icon to expand the search it overlaps the title:

enter image description here

How can I achieve to expand the searchview only to the end of "MY TITLE"?

Main layout:

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/home_appbarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:elevation="0dp">

        <androidx.appcompat.widget.Toolbar
            android:id="@+id/app_toolbar"
            android:layout_width="match_parent"
            android:layout_height="44dp"
            android:theme="@style/myToolbarTheme"
            app:contentInsetLeft="0dp"
            app:contentInsetStart="5dp"
            app:contentInsetStartWithNavigation="0dp"
            app:popupTheme="@style/myToolbarTheme"
            app:title="">

            <include layout="@layout/toolbar_title" />
        </androidx.appcompat.widget.Toolbar>

        <include layout="@layout/toolbar_tabs" />
    </com.google.android.material.appbar.AppBarLayout>

    <ViewFlipper .....

toolbar_title layout: included in Mainlayout

<ImageView
    android:id="@+id/current_view_icon"
    android:layout_width="@dimen/toolbar_view_icon"
    android:layout_height="@dimen/toolbar_view_icon"
    android:layout_alignParentLeft="true"
    android:layout_marginLeft="10dp"
    app:srcCompat="@drawable/ic_chronology" />


<TextView
    android:id="@+id/appbar_date"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="5dp"
    android:gravity="center_vertical"
    android:text="MY TITLE"
    android:textColor="@color/navigation_inner_color"
    android:textSize="22dp" />

Inflating menu in code:

AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
appToolbar = context.findViewById(R.id.app_toolbar);
((AppCompatActivity) context).setSupportActionBar(appToolbar);
appToolbar.inflateMenu(R.menu.main_menu);

R.menu.main_menu with quicksearch:

<?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"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <item
        android:id="@+id/app_bar_search"
        android:icon="@drawable/ic_search_black_24dp"
        android:title="Search"
        app:actionViewClass="mypackage.toolbar.ArrayAdapterSearchView"
        app:showAsAction="always" />
    <item
        android:id="@+id/app_bar_bookmark"
        android:icon="@drawable/ic_bookmark"
        android:title="Bookmark"
        app:showAsAction="always" />
    <item
        android:id="@+id/app_bar_menuitem_settings"
        android:title="Settings"
        app:showAsAction="never" />
</menu>

Solution

  • I think you have 2 options,

    Set width to SearchView in styles

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="searchViewStyle">@style/MySearchViewStyle</item>
    </style>
    
    <style name="MySearchViewStyle" parent="Widget.AppCompat.SearchView">
        <item name="android:maxWidth">140dp</item>
    </style>
    

    Second option is get SeachView instance in onCreateOptionsMenu and calculate width of your toolbar_title layout set remaining width to search bar.

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
       getMenuInflater().inflate(R.menu.menu, menu);
       MenuItem searchItem = menu.findItem(R.id.action_search);
       SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
       searchView.setMaxWidth(calculatedWidth);
       return super.onCreateOptionsMenu(menu);
    }
    

    But as per android standards, Search View has to extend completely in toolbar. For this we have to use searchView.setMaxWidth(Integer.MAX_VALUE);.