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.

Aditya Singh
Aditya Singh
Hi, I’m Aditya Singh. The Co-Founder of this blog AndroidHire. I’m Part-Time blogger, and digital marketer. If you want to contribute then you can contact us at contact@androidhire.com

Latest news

Everything about Android Latest Version

When Google launched the Google Android-powered Nexus One telephone on January 5, 2010, Jobs, Apple Chief executive officer responded to that development in a...

Use of “Services.dart” Package in the Flutter

We often come at a point where we need some flexibility, in our app UI. so in this blog, I want to...

One Plus 8T: All you need to know!

One Plus unveiled its 2020 flagship in a virtual event named One Plus 8T. In this post we would go through its...

Twitter may launch new features to simplify direct messages.

Twitter is said to be working on a couple of new features that could make it easier to manage direct messages...

Related news

Everything about Android Latest Version

When Google launched the Google Android-powered Nexus One telephone on January 5, 2010, Jobs, Apple Chief executive officer responded to that development in a...

Use of “Services.dart” Package in the Flutter

We often come at a point where we need some flexibility, in our app UI. so in this blog, I want to...

One Plus 8T: All you need to know!

One Plus unveiled its 2020 flagship in a virtual event named One Plus 8T. In this post we would go through its...

Twitter may launch new features to simplify direct messages.

Twitter is said to be working on a couple of new features that could make it easier to manage direct messages...