How to add back button in the Title Bar

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

For one way is to use 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 enable. If Action bar is hidden then the back button will not appear.

Recent Articles

[Updated] Temporarily Free Games and Apps on Google Play Store

Every week there are free games and apps on the Google Play store. Here is the list of free games and apps on the Play Store you...

OnePlus 7T vs iPhone 11 | Specs Comparison

The OnePlus 7T and iPhone 11 are the two best phones if you looking for a price of less than $1000. Both the phone...

Elon Musk unveils Tesla’s Cybertruck

Are you into gaming or love watching sci-fi movies?  What about those back to the future movies which gave us a glimpse of the...

7 Best Hiking Apps you Must Try in 2020

Having the best hiking apps always gives you an advantage if you are going for adventurous trips. These apps can track your workout and activities...

[UPDATED] 9 Best Android Launcher Apps of 2019

Android launcher is best to customize the overall experience of Android for a user in terms of look and act. In apps store, there are...

Related Stories

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Stay on Top - Get the daily news in your inbox