How to add back button in the Title Bar - Android Hire
Coding Tutorials

How to add back button in the Title Bar

Aditya Singh
In this tutorial, you will learn how to add a back button in the title bar in Android Studio. Adding an arrow help in the navigation, as you can jump from one activity to the main activity.

One way is to use the android toolbar back button in this, you will replace the action bar with the toolbar. Another way is to add the back button on the title bar.

How to Add Back Button in the Title Bar

METHOD 1

1. First, we need to initialize the toolbar-

Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

2. Now, add a call for an up button

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

3. In the ActivityMain.xml add the widget.ToolBar

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

</android.support.design.widget.AppBarLayout>

4. In the Manifest.xml you have to add no action bar to the activity so that toolbar can show up.

android:theme="@style/AppTheme.NoActionBar

METHOD 2

In this method, you have to add the parentActivityName in the AndroidManifest.xml 

android:parentActivityName=".MainActivity"

This will enable the back button in the title bar. This will only work when there is an action bar enabled. If the Action bar is hidden then the back button will not appear.

Aditya Singh's profile picture

Aditya Singh

With over seven years of experience, I help people understand technology through clear and insightful articles. I cover the latest in technology, mobile devices, PCs, how-tos, guides, news, and gadget reviews, always staying updated to provide accurate and reliable information.