Left toolbar space with SearchView android studio

I am trying to add a simple SearchView to a toolbar in my application. Everything that works fine counts on the layout.

This is the “space” between the NavigationDrawer icon and the SearchView . If I set the title, the space is filled with this line, otherwise empty.

Empty line:

enter image description here

With a title bar:

enter image description here

How to remove this annoying empty space?

Edit:

I use the NavigationDrawer example from Android Studio as the base code

main.xml

  <menu 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" tools:context=".MainActivity" > <item android:id="@+id/action_search" android:icon="@android:drawable/ic_menu_search" android:title="Search" app:actionViewClass="android.support.v7.widget.SearchView" app:showAsAction="always" /> </menu> 

app_bar_main.xml

 <?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" tools:context="ga.musicforyou.musicforyou.MainActivity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <android.support.v7.widget.Toolbar android:id="@+id/toolbar" app:contentInsetEnd="0dp" app:contentInsetStart="0dp" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" /> </android.support.design.widget.AppBarLayout> </android.support.design.widget.CoordinatorLayout> 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" app:theme="@style/ThemeOverlay.AppCompat.Dark" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:itemTextColor="#FFF" app:itemIconTint="#FFF" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout> 
+8
java android searchview android-toolbar
source share
2 answers

The toolbar extends from FrameLayout, and you can put views in it. Like this:

 <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/toolbar" android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/primary_color"> <SearchView android:layout_width="wrap_content" android:layout_height="wrap_content" /> </android.support.v7.widget.Toolbar> 

Edit: you put it in the layout as a menu. It will always be on the right side of the toolbar because it is a menu. If you want more direct control over the views inside the toolbar, just put it there, as I showed you.

+6
source share

I set actionBar.setDisplayHomeAsUpEnabled(true) to the code. So just adding SearchView on the toolbar didn’t work for me, and there is extra space between Navigation Back and SearchView . I do not have a title in the Toolbar . Therefore, I configured the entire toolbar as follows:

 <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:layout_alignParentTop="true" // If parent layout is RelativeLayout android:background="?attr/colorPrimary" android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" app:contentInsetLeft="0dp" // This is important, app:contentInsetStart="0dp" // Otherwise there will be extra space on Left Side app:popupTheme="@style/ThemeOverlay.AppCompat.Light"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="horizontal"> <ImageView android:id="@+id/ivNavBack" android:layout_width="56dp" android:layout_height="56dp" android:background="@drawable/your_drawable" // Custom background (optional), if you want to have a press effect android:padding="16dp" android:src="@drawable/ic_nav_back" /> // You can set icon of your choice here <android.support.v7.widget.SearchView android:id="@+id/searchView" android:layout_width="match_parent" android:layout_height="wrap_content" android:queryHint="@string/search_view_hint" app:queryHint="@string/search_view_hint" /> </LinearLayout> </android.support.v7.widget.Toolbar> 

... & in your business:

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); SearchView searchView = (SearchView) toolbar.findViewById(R.id.searchView); searchView.onActionViewExpanded(); // Expands searchView by default // If this is not set, then you will see default search icon and you have to click it to expand SearchView. 

Now you do not need to set actionBar.setDisplayHomeAsUpEnabled(true) in the code. Just handle the click event ivNavBack for it.

enter image description here

+1
source share

All Articles