Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Too much margin on SearchView

Tags:

android

My SearchView is showing up like this:

enter image description here

As you can see there is a margin both on the search_edit_frame of the SearchView, and outside the SearchView itself.

enter image description here

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:

enter image description here

However theres still a big gap between the "back" imagebutton and the SearchView.

like image 610
MichelReap Avatar asked Oct 16 '22 17:10

MichelReap


2 Answers

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.

like image 162
Jimmy Avatar answered Nov 15 '22 06:11

Jimmy


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" />
like image 41
Adrian Ciolea Avatar answered Nov 15 '22 07:11

Adrian Ciolea