natsumiyu
natsumiyu

Reputation: 3257

How can I remove the left space between back button and SearchView

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

Upvotes: 5

Views: 2764

Answers (4)

Yamini Balakrishnan
Yamini Balakrishnan

Reputation: 2411

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">

Upvotes: 18

Ravi.Dudi
Ravi.Dudi

Reputation: 1364

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

Upvotes: 1

Abhishek c
Abhishek c

Reputation: 549

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"

Upvotes: 3

Harshad Pansuriya
Harshad Pansuriya

Reputation: 20920

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.

Upvotes: 2

Related Questions