47

My problem is the extra space between the nav-drawer icon and toolbar title. The sample images are below:

spacing between nav-drawer icon and title

spacing between back icon and toolbar

The xml view of the toolbar is

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

I have tried to solve this problem by using code below but no change occurred.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    //toolbar.setTitleMarginStart(0);
    toolbar.setTitleMarginStart(-8);
}

Is there any way to solve this problem ?

Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
Sagar Chapagain
  • 1,683
  • 2
  • 16
  • 26
  • you can create a custom action bar and display the items as you like. – JuLes Nov 21 '16 at 10:18
  • 1
    Why is this a problem? This is a design decision by the platform developers, and this is how Google's apps for Android look. – Egor Nov 21 '16 at 10:32
  • @egor Sometimes the title will be longer and it can give a better look and meaning if we can show 2/3 letters more. Plus experimenting on small things is fun too. – Sagar Chapagain Nov 21 '16 at 10:40
  • I agree with @Egor – Tushar Monirul Nov 21 '16 at 10:55
  • I'd say you'll get better UX if you keep titles short, rather then tweaking default system UI. However, I can agree with the perspective that each app is unique: test your UI with your users, and if they react well to this kind of tweaks and find longer titles useful - then this would be the way to go. – Egor Nov 21 '16 at 11:47

5 Answers5

106

Add

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

to the ToolBar.

Complete Code :

<android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:titleTextAppearance="@style/Toolbar.TitleText"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        app:contentInsetStartWithNavigation="0dp" />
Harshad Pansuriya
  • 20,189
  • 8
  • 67
  • 95
10

With the MaterialToolbar and the androidx.appcompat.widget.Toolbar you can use these attributes:

  • contentInsetStartWithNavigation: Minimum inset for content views within a bar when a navigation button is present, such as the Up button (default value=72dp).

  • contentInsetStart: Minimum inset for content views within a bar. Navigation buttons and menu views are excepted (default value = 16dp)

  • titleMarginStart: specifies extra space on the start side of the toolbar's title. If both this attribute and titleMargin are specified, then this attribute takes precedence. Margin values should be positive.

Just use in your layout:

    <com.google.android.material.appbar.MaterialToolbar
        app:contentInsetStartWithNavigation="0dp"
        app:titleMarginStart="0dp"
        ..>

Default:
enter image description here

With app:contentInsetStartWithNavigation="0dp":
enter image description here

With app:contentInsetStartWithNavigation="0dp" and app:titleMarginStart="0dp":
enter image description here

You can also define a custom style:

<style name="MyToolbar" parent="....">
    <item name="titleMarginStart">0dp</item>
    <item name="contentInsetStart">..dp</item>
    <item name="contentInsetStartWithNavigation">..dp</item>
</style>
Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
6

Add app:contentInsetStartWithNavigation="0dp" in toolbar

nawaab saab
  • 1,892
  • 2
  • 20
  • 36
1

Add this line app:contentInsetStartWithNavigation="0dp"

 <android.support.v7.widget.Toolbar
                android:id="@+id/share"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:navigationIcon="@drawable/action_back"
                app:popupTheme="@style/AppTheme.PopupOverlay"
                app:title="@{title}"
                android:background="4855b5"
                app:titleTextColor="ffffff"
                style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
                app:titleTextAppearance="@style/Toolbar.TitleText"
                app:contentInsetStartWithNavigation="0dp" />
MMG
  • 3,226
  • 5
  • 16
  • 43
Kishore Reddy
  • 2,394
  • 1
  • 19
  • 15
0

To do In Flutter Android use titleSpacing: 0, in ** App Bar** layout

** Sample Code **

appBar: AppBar(
    actions: <Widget>[
      Icon(Icons.search_rounded),
      Icon(Icons.notifications),
      Icon(Icons.add_shopping_cart),
    ],
    leadingWidth: 26,
    titleSpacing: 0,
    backgroundColor: Colors.white70,
    leading: Icon(Icons.menu),
    elevation: 100.0,
    iconTheme: IconThemeData(color: Colors.black),
    title: Row(
      mainAxisAlignment: MainAxisAlignment.start,
      children: [
        appbarIcon,
      ],
    ),
  )
sujith s
  • 864
  • 11
  • 20