how to remove left margin of Android Toolbar?
Asked Answered
C

10

41

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 !

Concatenate answered 26/6, 2015 at 2:37 Comment(5)
change to use app:contentInsetLeft="0dp" and app:contentInsetStart="0dp". add xmlns:app="schemas.android.com/apk/res-auto" to most parent view xmlUnivocal
@Univocal thanks for the answer .. works like a charm ..Inchoate
This is not left margin, Its Toolbar inner side space so we call toolbar left padding and you can remove left padding. Remove SpacePervasive
possible duplicates @ConcatenateDiao
this link was useful for me, try it... enter link description hereGilgilba
E
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>
Enloe answered 25/2, 2016 at 5:42 Comment(1)
With navigationIcon, add app:contentInsetStartWithNavigation="0dp"Stav
G
49

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

Girand answered 25/7, 2016 at 8:31 Comment(0)
V
13

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>
Vankirk answered 2/1, 2017 at 5:28 Comment(0)
K
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>

Kokura answered 29/1, 2016 at 10:8 Comment(0)
M
3

Just add these two lines in your toolbar.xml

  1. app:contentInsetStart="0dp"
  2. app:contentInsetEnd="0dp"
Marlonmarlow answered 10/5, 2018 at 6:1 Comment(0)
I
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"/>
Ivanivana answered 10/5, 2017 at 4:29 Comment(1)
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 workSheave
C
2

Add below xml code to your Toolbar !

app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
Crosstree answered 1/8, 2017 at 6:13 Comment(0)
I
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" />
Imprest answered 29/9, 2017 at 8:45 Comment(0)
P
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>
Pervasive answered 4/11, 2019 at 9:37 Comment(0)
B
0

Add below xml code to 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">
Breebreech answered 7/8, 2022 at 20:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.