Search code examples
androidtoolbar

Android toolbar not sizing correctly


I have an android toolbar and inside the toolbar I have an editText, and the editText is doing a margin which I don't want.

Here is a picture:

enter image description here

Here is the toolbar code:

<?xml version="1.0" encoding="utf-8"?>

<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_top"
    android:background="@drawable/background_toolbar_top"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <EditText
        android:id="@+id/buscador"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="10dp"
        android:textColor="#666"
        android:background="#ccc"
        android:textColorHint="#cccc"
        android:hint="Buscar"/>

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

Background of the toolbar:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item>
        <shape android:shape="rectangle">
            <solid android:color="#333"></solid>
        </shape>
    </item>

    <item android:bottom="1dp">
        <shape android:shape="rectangle">
            <solid android:color="#fff"></solid>
        </shape>
    </item>

</layer-list>

Solution

  • The question is that why there is a blank space on the left in Toolbar. It's Toolbar's contentInsetStart which by default is 16dp, you can set 0 to remove it:

    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    

    Hope this can help:

    <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_top"
        android:background="@drawable/background_toolbar_top"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp">
    
        <EditText
            android:id="@+id/buscador"
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:textSize="10dp"
            android:textColor="#666"
            android:background="#ccc"
            android:textColorHint="#cccc"
            android:hint="Buscar"/>
    
    </android.support.v7.widget.Toolbar>
    

    And you can check this for more information about the marginLeft in Toolbar.