Anirudh
Anirudh

Reputation: 3358

How to remove left and right padding for a toolbar?

Left and Right padding is added automatically. This is the code I used. I'm running the app on Android version 6.0.1.

 <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="45dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:layout_scrollFlags="scroll|enterAlways">

Upvotes: 3

Views: 3194

Answers (5)

M.ghorbani
M.ghorbani

Reputation: 149

I think the best way to solve the problem is to create a custom toolbar:

First create a custom toolb:

class CustomToolbar(context: Context): androidx.appcompat.widget.Toolbar(context) {

    constructor(context: Context , attrs: AttributeSet): this(context)
    constructor(context: Context , attrs: AttributeSet, defStyleAttr: Int): this(context)

    override fun getContentInsetStart(): Int {
        return 0
    }

    override fun getContentInsetEnd(): Int {
        return 0
    }

    override fun getContentInsetLeft(): Int {
        return 0
    }

    override fun getContentInsetRight(): Int {
        return 0
    }

    override fun getCurrentContentInsetStart(): Int {
        return 0
    }

    override fun getCurrentContentInsetEnd(): Int {
        return 0
    }

    override fun getCurrentContentInsetLeft(): Int {
        return 0
    }

    override fun getCurrentContentInsetRight(): Int {
        return 0
    }
}

Now you can use it anywhere without any problems:

<com.example.CustomToolbar
   android:id="@+id/toolbar"
   android:layout_width="match_parent"
   android:layout_height="?actionBarSize">
  //your views or layouts
</com.example.CustomToolbar>

Note that com.example is my package name and you must replace your project package name.

Upvotes: 0

Oussema Aroua
Oussema Aroua

Reputation: 5329

check the parent element and if you include the toolbar check to parent too.

Upvotes: 0

user3324792
user3324792

Reputation: 119

Try setting contentInsetStartWithNavigation to 0dp as well. Like contentInsetStart, contentInsetStartWithNavigation also is 16dp by default. Try this:

    <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="45dp"
    app:contentInsetLeft="0dp"
    app:contentInsetStart="0dp"
    app:contentInsetStartWithNavigation="0dp"
    app:layout_scrollFlags="scroll|enterAlways">

Also, trying replacing the 'app' with 'android' if you haven't already. Sometimes we try to fix compatibility issues when there are none. Try this:

   <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="45dp"
    android:contentInsetLeft="0dp"
    android:contentInsetStart="0dp"
    android:contentInsetStartWithNavigation="0dp"
    android:layout_scrollFlags="scroll|enterAlways">

You can also hard-code your Toolbar attributes in your activity file. You will use padding if you do so.

     Toolbar tb = (Toolbar)findViewById(R.id.toolbar);
     tb.setPadding(padding, tb.getPaddingTop(), padding, tb.getPaddingBottom());

Upvotes: 2

Anirudh
Anirudh

Reputation: 3358

I added the below line of code and it worked.

 android:padding="0dp"

Upvotes: 2

tahsinRupam
tahsinRupam

Reputation: 6405

Use as the following:

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:minHeight="45dp"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:contentInsetStart="0dp"               //Add this
        android:contentInsetLeft="0dp"               //Add this
        app:contentInsetStartWithNavigation= "0dp"  //Add this too
        app:layout_scrollFlags="scroll|enterAlways">

Upvotes: 8

Related Questions