Search code examples
androidandroid-toolbar

Android Changing Shape of The Back Button


The back button on the action bar of my app seems a little large. When I hold my finger on it on my phone the button highlights and I can see the right side of it is rounded out pushing over the text.

enter image description here

How can I make the button square and move the text closer to the left side of the action bar instead of being more out to the middle?


Solution

  • Try setting a custom drawable for your Toolbar's back navigation icon

    ActionBar ab = getSupportActionBar();
    ab.setHomeAsUpIndicator(R.drawable.ic_back);
    ab.setDisplayShowHomeEnabled(true);
    ab.setDisplayHomeAsUpEnabled(true);
    

    Or if you want precise decoration use a custom toolbar layout,

    <?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="?android:attr/actionBarSize"
        android:minHeight="?android:attr/actionBarSize"
        app:layout_collapseMode="pin"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:theme="@style/ToolbarStyle">
    
            <!-- Left side layout-->
            <LinearLayout
                android:id="@+id/layout_left_container"
                android:layout_width="wrap_content"
                android:layout_height="?attr/actionBarSize"
                android:layout_gravity="start|center"
                android:layout_marginEnd="8dp"
                android:layout_marginRight="8dp"
                android:gravity="center|start"
                android:orientation="horizontal">
    
                <ImageButton
                    android:id="@+id/iv_left_icon"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />
    
                <TextView
                    android:id="@+id/txt_left_title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ellipsize="end"
                    android:maxLength="30"
                    android:singleLine="true"
                    android:text="@string/app_name"
                    android:textColor="@color/white"
                    android:textSize="14sp" />
    
            </LinearLayout>
    
    </android.support.v7.widget.Toolbar>