How To Add Back Button in Android Title Bar[2020]

In this post, we are going to learn how to add back button in android title bar. Adding the Back button in the Title bar is very easy. what we learn is adding a back button and customizing the back button also.

So let’s get Started.

Adding back button in android Studio

Step 1: Creating Project

Open Android Studio and Create new project

Step 2: Creating Activity

Go to File then New and then Activity and then Blank Activity.

Let me explain you’re the reason behind the second activity, In our main activity, we add the back button in the Title bar, and on clicking the back button we go to Second Activity.

Let’s give the name of the second activity as BackActivity.

Step 3: Adding Back Button

Now, Add the back button in android title bar by using Action Bar like this.

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
}

Now, you see something like this, a back button is appearing in your toolbar. What we done is simply get the Action Bar and set the Up button enabled using setDisplayHomeAsUpEnabled(true);

back button in android studio
back button in android studio

Adding Click Listener in Back Button

Adding Click Listener on the back button is a very easy task, all you need to simply override a method and code which you want to execute on clicking Back Button.

We are going to show a Toast whenever a back button is pressed.

Step:1 Overriding Method

Override onOptionsItemSelectedMethod like below code

 @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        return super.onOptionsItemSelected(item);
    }

Step 2 : Showing a Toast

Now, we need to show a toast whenever the back button is pressed and we done like this, complete code is given below.


import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.ActionBar;
import android.os.Bundle;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
     Toast.makeText(getApplicationContext(),"Back Button Clicked",Toast.LENGTH_SHORT).show();
     
        return super.onOptionsItemSelected(item);
    }
}
showing toast when back button clicked
showing toast

Step 3: Starting BackActivity whenever Back Button clicked

Now, we start back activity whenever the back button is clicked through Intent.

We do this by simply adding Intent, like this



import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import androidx.appcompat.app.ActionBar;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        startActivity(new Intent(MainActivity.this,BackActivity.class));
        return super.onOptionsItemSelected(item);

    }
}

Also after Back Activity started we need to show a toast that Back Activity starts and we do this by adding toast in backactivity.java file.



import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.Toast;

public class BackActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_back);
        Toast.makeText(getApplicationContext(),"Back Activity",Toast.LENGTH_SHORT).show();
    }
}

Now run your app and you see some thing like this

start activity throuh back
start activity throuh back

Changing Colour Of back button

If you want to customize the color of the back button you can easily do this by adding a new Arrow icon. You can do this by following steps

Step 1: Adding New Drawable

Go to file then new and then vector asset and search for arrow icon and add to your drawable folder. Screenshots attached

adding drawable
adding drawable
adding drawable
adding drawable

Step 2: Set drawable as back button

Set the created drawable as back button by following code

   @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeAsUpIndicator(R.drawable.ic_arrow_back_black_24dp);
    }

Now you see your app like this

customized back button
customized back button

Conclusion

You learn how to add Back button in android title bar, attaching click listener, and also customizing icon. Hope you learn and comment if it works for you

Sudhanshu is Technology geek and also a pro pubg player. I like to create content in my free time that helps others. Currently pursuing BCA from Noida and operating Geekstocode

2 Comments

  1. Thank you for sharing superb informations. Your web-site is very cool. I’m impressed by the details that you have on this website. It reveals how nicely you understand this subject. Bookmarked this web page, will come back for more articles. You, my friend, ROCK! I found simply the info I already searched all over the place and just could not come across. What an ideal website.

Leave a Comment

Your email address will not be published. Required fields are marked *