fbpx

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.

Hot Topics

JSON Parsing in Android Tutorial

In this article, we will learn, how to do JSON Parsing in Android. JSON stands for JavaScript Object Notation. It is a lightweight format...

How to get Partner Title in PUBG Mobile in 5 minutes?

This is the new title that PUBG Mobile has introduced in October 2019 update. The title name is "Partner". What is Partner Title in PUBG Mobile? This...

Google Login And Registration For Android Using Firebase Authentication

In this tutorial, we will learn how to build simple google login and registration for android using Firebase Authentication. We have added Email &...

Related Articles

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 share two...

Navigating Through Screens with Named Routes

An app is nothing but just lots of beautiful screens with different functionalities connected together for great and easy user experience. Let us dive...

Social Media Buttons in Flutter Web

Social media buttons on websites are not a new thing. We all are familiar with them somehow. We can find it on every website...