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

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.

How to Add Back Button In Android Title Bar?

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

how to add back button in android title bar
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. You can download android studio from here.

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

49 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.

  2. Incredible! This blog looks just like my old one! It’s on a totally different subject but it has pretty much the same page layout and design. Great choice of colors!

  3. Hiya, I am really glad I’ve found this information. Nowadays bloggers publish only about gossips and web and this is actually frustrating. A good website with interesting content, this is what I need. Thank you for keeping this website, I will be visiting it. Do you do newsletters? Can not find it.

  4. I simply could not leave your site prior to suggesting that I extremely enjoyed the usual info a person supply to your guests? Is gonna be back frequently to inspect new posts

  5. You actually make it appear so easy with your presentation however I in finding this topic to be actually something which I feel I would by no means understand. It seems too complicated and very wide for me. I’m taking a look ahead to your subsequent post, I’ll attempt to get the dangle of it!

  6. I’m impressed, I have to say. Really hardly ever do I encounter a weblog that’s both educative and entertaining, and let me inform you, you have hit the nail on the head. Your idea is outstanding; the difficulty is one thing that not sufficient people are talking intelligently about. I’m very completely satisfied that I stumbled throughout this in my search for something referring to this.

  7. I do not even know how I ended up here, but I believed this publish used to be good. I do not recognise who you’re however definitely you’re going to a well-known blogger for those who are not already 😉 Cheers!

  8. Hey, you used to write magnificent, but the last few posts have been kinda boring… I miss your super writings. Past several posts are just a little out of track! come on!

  9. This design is spectacular! You definitely know how to keep a reader entertained. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job. I really loved what you had to say, and more than that, how you presented it. Too cool!

  10. I’ve been absent for a while, but now I remember why I used to love this website. Thanks , I will try and check back more frequently. How frequently you update your web site?

  11. A person essentially help to make seriously posts I would state. This is the first time I frequented your web page and thus far? I surprised with the research you made to make this particular publish incredible. Fantastic job!

  12. Hello, i think that i saw you visited my site so i came to “return the favor”.I’m attempting to find things to improve my web site!I suppose its ok to use some of your ideas!!

  13. Pretty nice post. I just stumbled upon your blog and wished to say that I have truly enjoyed surfing around your blog posts. After all I’ll be subscribing to your rss feed and I hope you write again soon!

  14. Hey very cool site!! Man .. Beautiful .. Wonderful .. I will bookmark your blog and take the feeds also…I am happy to search out a lot of helpful information right here in the submit, we’d like develop extra techniques on this regard, thank you for sharing. . . . . .

  15. Hmm is anyone else encountering problems with the images on this blog loading? I’m trying to figure out if its a problem on my end or if it’s the blog. Any suggestions would be greatly appreciated.

  16. I’m still learning from you, but I’m trying to achieve my goals. I certainly liked reading everything that is written on your blog.Keep the tips coming. I loved it!

  17. I was suggested this blog by my cousin. I am not sure whether this post is written by him as no one else know such detailed about my trouble. You are incredible! Thanks!

  18. What’s Happening i’m new to this, I stumbled upon this I have found It absolutely useful and it has aided me out loads. I hope to contribute & help other users like its aided me. Great job.

  19. Have you ever considered about adding a little bit more than just your articles? I mean, what you say is important and all. Nevertheless just imagine if you added some great pictures or video clips to give your posts more, “pop”! Your content is excellent but with images and videos, this website could certainly be one of the very best in its field. Great blog!

  20. You could certainly see your skills in the work you write. The world hopes for more passionate writers like you who are not afraid to say how they believe. Always go after your heart.

  21. Hello very cool website!! Guy .. Excellent .. Superb .. I’ll bookmark your site and take the feeds additionally…I’m glad to find a lot of useful information here in the post, we’d like develop more techniques in this regard, thank you for sharing. . . . . .

  22. obviously like your web-site however you need to take a look at the spelling on several of your posts. Several of them are rife with spelling problems and I in finding it very bothersome to tell the truth however I will surely come back again.

  23. I used to be very happy to seek out this net-site.I wished to thanks in your time for this glorious learn!! I positively having fun with each little little bit of it and I’ve you bookmarked to check out new stuff you weblog post.

  24. Heya! I’m at work surfing around your blog from my new iphone 4! Just wanted to say I love reading your blog and look forward to all your posts! Carry on the fantastic work!

  25. Generally I do not read post on blogs, however I wish to say that this write-up very pressured me to check out and do it! Your writing taste has been surprised me. Thanks, very great article.

  26. Youre so cool! I dont suppose Ive read something like this before. So nice to find any person with some original thoughts on this subject. realy thank you for starting this up. this website is one thing that is wanted on the net, someone with a bit originality. useful job for bringing one thing new to the web!

  27. It’s actually a great and useful piece of information. I am happy that you shared this useful info with us. Please stay us up to date like this. Thank you for sharing.

  28. Good post. I learn something more challenging on totally different blogs everyday. It would always be stimulating to read content material from other writers and follow somewhat something from their store. I’d want to use some with the content material on my blog whether or not you don’t mind. Natually I’ll provide you with a link on your internet blog. Thanks for sharing.

  29. naturally like your website however you need to check the spelling on quite a few of your posts. Many of them are rife with spelling issues and I to find it very bothersome to inform the reality nevertheless I will surely come again again.

  30. Pretty component to content. I simply stumbled upon your web site and in accession capital to claim that I get in fact enjoyed account your blog posts. Anyway I will be subscribing to your feeds or even I fulfillment you get admission to consistently fast.

  31. I’ve read several good stuff here. Certainly worth bookmarking for revisiting. I surprise how much effort you put to make such a wonderful informative web site.

  32. Generally I don’t read post on blogs, but I would like to say that this write-up very forced me to try and do so! Your writing style has been amazed me. Thanks, quite nice post.

  33. Wonderful work! This is the type of information that should be shared around the web. Shame on the search engines for not positioning this post higher! Come on over and visit my web site . Thanks =)

  34. Appreciating the dedication you put into your site and detailed information you provide. It’s good to come across a blog every once in a while that isn’t the same unwanted rehashed material. Great read! I’ve saved your site and I’m adding your RSS feeds to my Google account.

  35. You really make it seem so easy with your presentation but I find this topic to be really something that I think I would never understand. It seems too complex and very broad for me. I’m looking forward for your next post, I’ll try to get the hang of it!

  36. Nice post. I was checking continuously this blog and I’m impressed! Extremely useful information specially the last part 🙂 I care for such information much. I was looking for this particular information for a long time. Thank you and good luck.

  37. Right now it appears like Movable Type is the top blogging platform out there right now. (from what I’ve read) Is that what you’re using on your blog?

  38. I think this is among the most important information for me. And i’m glad reading your article. But wanna remark on some general things, The web site style is ideal, the articles is really great : D. Good job, cheers

  39. I’m not that much of a internet reader to be honest but your sites really nice, keep it up! I’ll go ahead and bookmark your site to come back later. Cheers

  40. Superb post however , I was wondering if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Thank you!

  41. I’m still learning from you, but I’m making my way to the top as well. I definitely liked reading all that is posted on your site.Keep the information coming. I liked it!

  42. I think this is among the most vital info for me. And i am glad reading your article. But wanna remark on some general things, The site style is ideal, the articles is really excellent : D. Good job, cheers

  43. Wonderful beat ! I would like to apprentice even as you amend your website, how could i subscribe for a weblog web site? The account helped me a applicable deal. I had been tiny bit familiar of this your broadcast offered vibrant clear idea

  44. Sweet blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Many thanks

  45. I’d need to test with you here. Which isn’t one thing I often do! I take pleasure in studying a submit that may make people think. Additionally, thanks for allowing me to remark!

Leave a Comment

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