39

I'm trying to use toolbar for my project. Here is the code I am using:

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:contentInsetLeft="0dp"
    android:elevation="@dimen/margin_padding_8dp"
    android:contentInsetStart="0dp">

    <RelativeLayout
        android:id="@+id/rlToolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <TextView
            android:id="@+id/tvTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:paddingRight="@dimen/margin_padding_16dp"
            android:text="AppBar"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textColor="@color/white"
            android:textSize="@dimen/text_size_20sp" />

    </RelativeLayout>


toolbar

I want to remove left margin, Here I set android:contentInsetLeft="0dp" and android:contentInsetStart="0dp" but it's not working..Please help me !

Saamzzz
  • 246
  • 1
  • 14
natuan241
  • 731
  • 3
  • 7
  • 11
  • 22
    change to use app:contentInsetLeft="0dp" and app:contentInsetStart="0dp". add xmlns:app="http://schemas.android.com/apk/res-auto" to most parent view xml – calvinfly Jun 26 '15 at 04:04
  • @calvinfly thanks for the answer .. works like a charm .. – Mohammad Zekrallah Dec 28 '15 at 07:39
  • This is not left margin, Its Toolbar inner side space so we call toolbar left padding and you can remove left padding. [Remove Space](https://stackoverflow.com/a/58690606/5788247) – Shomu Nov 04 '19 at 10:03
  • [possible duplicates](https://stackoverflow.com/q/27354812/3763032) @natuan241 – mochadwi Jan 13 '20 at 17:53
  • this link was useful for me, try it... [enter link description here](https://stackoverflow.com/questions/27354812/android-remove-left-margin-from-actionbars-custom-layout) – banoo Jun 24 '20 at 09:13

9 Answers9

51

replace your xml with below xml

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="?attr/colorPrimary"
    android:elevation="@dimen/margin_padding_8dp"
    android:contentInsetStart="0dp" 
    android:contentInsetLeft="0dp"
    android:contentInsetRight="0dp"
    android:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp">

<RelativeLayout
    android:id="@+id/rlToolbar"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tvTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingRight="@dimen/margin_padding_16dp"
        android:text="AppBar"
        android:textAppearance="@style/TextAppearance.AppCompat"
        android:textColor="@color/white"
        android:textSize="@dimen/text_size_20sp" />

</RelativeLayout>
Shahab Saalami
  • 557
  • 6
  • 15
Damodhar
  • 1,209
  • 8
  • 16
48

Use app:contentInsetStart="0dp" to remove that left space.

Akshay
  • 5,721
  • 7
  • 39
  • 58
12

See the below code and here I added app:contentInsetStart="0dp". You need to add that to your code because before API 21 (i.e. Lollipop) you need to add that line.

<android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="64dp"
            android:background="@color/colorPrimary"
            android:minHeight="?attr/actionBarSize"
            android:contentInsetStart="0dp"
            app:contentInsetStart="0dp"
            >
 </android.support.v7.widget.Toolbar>
wbk727
  • 7,218
  • 11
  • 52
  • 106
Amit Walke
  • 283
  • 3
  • 6
3

Referring to @calvinfly comment:

I updated my code

<RelativeLayout
    android:id="@+id/rlTop"
    android:layout_width="fill_parent"
    android:layout_height="?attr/actionBarSize"
    android:layout_alignParentTop="true"
    android:background="@android:color/white" >

    <TextView
        android:id="@+id/toolbar_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="@string/titleString"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#468bac"
        android:textStyle="bold" />

    <RelativeLayout
        android:id="@+id/rlStarsTop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:gravity="center"
        android:layout_marginRight="2dp"
        android:layout_toRightOf="@+id/toolbar_title"
        android:layout_toEndOf="@+id/toolbar_title"
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:scrollHorizontally="true">

        <RatingBar
            android:id="@+id/txtRatings"
            style="?android:attr/ratingBarStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_margin="1dp"
            android:gravity="center"
            android:max="5"
            android:rating="3.7"
            android:textColor="@android:color/holo_blue_bright"
            android:textStyle="bold" />
    </RelativeLayout>

</RelativeLayout>

VVB
  • 6,983
  • 7
  • 45
  • 78
3

Just add these two lines in your toolbar.xml

  1. app:contentInsetStart="0dp"
  2. app:contentInsetEnd="0dp"
Hardik Hirpara
  • 1,727
  • 16
  • 27
2

This works for me...

<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app2="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app2:contentInsetStart="0dp"/>
Rahul
  • 2,967
  • 2
  • 26
  • 41
  • you have to emphasize the xmlns part on the toolbar, it seems when you auto add it, it adds to the top most parent, and that doesnt work – Lena Bru Jul 31 '20 at 03:32
2

Add below xml code to your Toolbar !

app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
Hadi Note
  • 1,308
  • 16
  • 14
2

Add following code to your .xml file It may solve. Perfectly working solution I have tried.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="@color/white"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetEnd="0dp"
    app:theme="@style/toolbarPopup">

above 21 use following code

<android.support.v7.widget.Toolbar
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" />
Sagar
  • 4,549
  • 3
  • 31
  • 44
0

Use This Toolbar property for remove toolbar left space

app:contentInsetStart="0dp"

<androidx.appcompat.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="?attr/actionBarSize"
    android:background="@color/colorPrimary"
    app:contentInsetStart="0dp">

</androidx.appcompat.widget.Toolbar>
Shomu
  • 2,229
  • 19
  • 32