Search code examples
android-toolbarandroid-imagebutton

Remove space of image in imagebutton in toolbar


This my "toolbar_main.xml" file for use custom toolbar menu in every activity :

<?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_main"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="#ddFFFFFF">

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

       <ImageButton
            android:id="@+id/ib_back"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_centerVertical="true"
            android:backgroundTint="#00000000"
            app:srcCompat="@drawable/ic_back" />

       <TextView
            android:id="@+id/tv_app_title"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="Image Status"
            android:textAppearance="?
            android:textAppearanceMedium"
            android:textColor="@android:color/black"
            android:visibility="visible" />
   </RelativeLayout>
</android.support.v7.widget.Toolbar>

I am using svg image for image button which is use for custom back press button. Problem is Relativelayout put some space on left side.

Screenshot of toolbar_main.xml file


Solution

  • Simply put,

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

    in your Toolbar.

    for eg:

     <?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_main"
        app:contentInsetLeft="0dp"
        app:contentInsetStart="0dp"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#ddFFFFFF">
    
       <RelativeLayout
           android:layout_width="match_parent"
           android:layout_height="match_parent">
    
           <ImageButton
                android:id="@+id/ib_back"
                android:layout_width="40dp"
                android:layout_height="40dp"
                android:layout_centerVertical="true"
                android:backgroundTint="#00000000"
                app:srcCompat="@drawable/ic_back" />
    
           <TextView
                android:id="@+id/tv_app_title"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="center"
                android:text="Image Status"
                android:textAppearance="?
                android:textAppearanceMedium"
                android:textColor="@android:color/black"
                android:visibility="visible" />
       </RelativeLayout>
    </android.support.v7.widget.Toolbar>