5

I keep having a big white space in the left of my Toolbar between the back button and SearchView. My xml file looks like this

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:layout_scrollFlags="scroll|enterAlways"
        android:contentInsetStart="0dp"
        app:contentInsetStart="0dp">

        <android.support.v7.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>


    </android.support.v7.widget.Toolbar>

In my Activity I add a back button like below

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

if I set it to false it works okay. But I need to have a back button. Is there a way to solve it? Thank you.

I didn't use a menu layout.

enter image description here

natsumiyu
  • 3,217
  • 7
  • 30
  • 54

4 Answers4

18

By default, Toolbar have 16dp inset after backbutton. So, include app:contentInsetStartWithNavigation="0dp" in Toolbar, it will remove that whitespace.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    app:layout_scrollFlags="scroll|enterAlways"
    android:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:contentInsetStart="0dp">
Yamini Balakrishnan
  • 2,361
  • 16
  • 24
2

Add this property in the SearchView

<android.support.v7.widget.SearchView
            android:id="@+id/search_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:paddingLeft="-16dp"
            android:paddingStart="-16dp"/>

and check it. Hope this will help you.

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
2

Add these lines in your Toolbar

android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp"
Abhishek c
  • 539
  • 6
  • 16
1

Use MaxWidth option to do it programmatically in onCreateOptionsMenu() method

@override 
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu_search, menu);
    SearchView searchView = (SearchView)menu.findItem(R.id.menu_search).getActionView();
    searchView.setMaxWidth(Integer.MAX_VALUE);

if using Kotlin use:

searchView.maxWidth = Integer.MAX_VALUE
Ravi.Dudi
  • 1,284
  • 14
  • 14