My SearchView is showing up like this:
As you can see there is a margin both on the search_edit_frame
of the SearchView, and outside the SearchView itself.
Where is this coming from? Inspecting the layout reveals a margin of 24 pixels to the left of search_edit_frame
but no margin elsewhere.
How can I remove this extra space?
menu.xml:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/appbar_menu_search"
android:icon="@drawable/ic_search"
android:title="@string/search_search"
app:showAsAction="always"
app:actionViewClass="android.support.v7.widget.SearchView" />
</menu>
Layout:
<RelativeLayout
android:id="@+id/root_layout"
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.support.design.widget.AppBarLayout
android:id="@+id/profile_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/topbar_gradient"
android:theme="@style/AppTheme.Dark"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/topbar_gradient"
android:fitsSystemWindows="true"
android:minHeight="?android:attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
(...)
</RelativeLayout>
I managed to reduce the spacing by applying @Jimmy's answer:
However theres still a big gap between the "back" imagebutton and the SearchView.
search_edit_frame
is a LinearLayout
in searchview
layout. It has start and end margin of 8dip by default .
relevant code from source
<LinearLayout
android:id="@+id/search_edit_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginStart="8dip"
android:layout_marginEnd="8dip"
android:orientation="horizontal"
android:layoutDirection="locale">
Your issue is probably because of start margin on it. You can get this linear layout from search view and set the layout parameter to reduce the gap in your activity ( probably inside onCreateOptionsMenu
or similar where you are inflating thissearchview
) .
Something like this , assuming searchView
is your SearchView instance,
LinearLayout searchFrameLL=(LinearLayout)searchView.findViewById(R.id.search_edit_frame);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.MATCH_PARENT);
params.setMargins(0,0,8,0); //params.setMargins(left, top, right, bottom)
// params.setMarginStart(0); //(or just use individual like this
searchFrameLL.setLayoutParams(params);
In similar fashion, you can update other properties in that xml to meet your needs.
I'm not sure if it helps, but I also had an unwanted margin in the Toolbar view. This answer about content insets helped me figure it out.
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With