ASK ASK - 4 months ago 100
Android Question

Remove margin in android toolbar icon

I get this weird margin in my app toolbar between icon and navigation icon in the toolbar (as in the image).
I've got no idea about where it comes from and how to remove it. After searching the internet I found this:

<android.support.v7.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_height="wrap_content"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:background="?attr/colorPrimaryDark"
android:layout_margin="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
android:contentInsetEnd="0dp"
android:padding="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetEnd="0dp">
</android.support.v7.widget.Toolbar>


But I still get this margin as in the figure:
Margin

Edit >> Solution

Well after using layout bound I figured much of the margins are of the icon(as in figure). But can I still remove this margin and change the size of the icon and the title text.
enter image description here

Edit

Following @Amir solution:
Helper for java:

class BasicActivity extends Activity{
protected Toolbar mToolbar; /// Initilize it in onCreate methode
.....

public void setToolbarTitle(CharSequence title){
TextView titleContainer=(TextView) mToolbar.findViewById(R.id.title);
titleContainer.setText(title);
}
}


And in your activity class:

class Main extends BasicActivity{
@override
public void onCreate(Bundle saved){
super.onCreate(saved);
....
mToolbar=(Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
setToolbarTitle("Main");
}
}

Answer

If you want to use default behaviour of Toolbar it's not possible to remove that margin (by default behaviour I mean using this code getSupportActionBar().setLogo(R.drawable.ic_launcher);). But you can do it easily with some change in your toolbar like following code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.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/color_primary"
    app:contentInsetEnd="0dp"
    app:contentInsetLeft="0dp"
    app:contentInsetRight="0dp"
    app:contentInsetStart="0dp"
    app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical">

        <ImageView
            android:id="@+id/icon_toolbar_left"
            style="@style/IconFont.Large"
            android:layout_width="48dp"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="?attr/selectableItemBackground" />



        <TextView
            android:id="@+id/text_toolbar_title"
            style="@style/Textview.White.MediumSmall"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_toLeftOf="@+id/icon_toolbar_right"
            android:layout_toRightOf="@+id/icon_toolbar_left"
            android:gravity="center"
            android:text="@string/nav_category"/>


        <ImageView
            android:id="@+id/icon_toolbar_right"
            style="@style/IconFont.Large"
            android:layout_width="48dp"
            android:layout_height="match_parent"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:background="?attr/selectableItemBackground"/>

    </RelativeLayout>

</android.support.v7.widget.Toolbar>
Comments