Home » Android » how to remove left margin of Android Toolbar?

how to remove left margin of Android Toolbar?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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 !

How to&Answers:

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>

Answer:

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

Answer:

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>

Answer:

Just add these two lines in your toolbar.xml

  1. app:contentInsetStart=”0dp”
  2. app:contentInsetEnd=”0dp”

Answer:

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>

Answer:

Add below xml code to your Toolbar !

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

Answer:

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

Answer:

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

Answer:

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>