If you are looking for programmatic ways of adding a back button to the Toolbar of your Android Application Activity then you are at the right place.
Tutorial Prerequisite:
- You may already have created created an Android Application by selecting a default template that displays a ToolBar.
How to add Back Button (Arrow) to Toolbar
- Open your Activity class file: Example - MainActivity.java
- Add the below lines just after super.onCreate(savedInstanceState); in the onCreate() method,
package com.example.myapplication; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.widget.Toolbar; import android.os.Bundle; /** * Example program to display back button * on the ToolBar on Android Activity * */ public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //To have the back button!! ActionBar actionBar = getSupportActionBar(); actionBar.setDisplayHomeAsUpEnabled(true); setContentView(R.layout.activity_main); } }
- Now run your App you should see the back arrow button displayed just before the Title.

Android Toolbar Back Button Example
✌️You would see that when you click on the back arrow button no action happens - that's because we have not written any listen for it - let's do that as well.
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Toast.makeText(this,"Back button pressed!",Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
As the icon that's displayed is called home - we have overridden the onOptionsItemSelected method and when the home button is clicked we are displaying a toast message just to show that the functionality works. You can do the implementation as per your needs.
Happy coding :)
Have Questions? Post them here!
More Posts related to Android,
- Change Android Toast background color
- Maven : java.lang.ClassNotFoundException: Xmx512m
- This class should be public (android.support.v7.internal.widget.ActionBarView.HomeView) Lint Error
- Android Alert Dialog with Checkboxes example
- Android Error Generating Final Archive - Debug Certificate Expired
- How to add Newline to text in Android TextView
- Read Text file from SD Card : Android Programming
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- ActivityManager Warning: Activity not started, its current task has been brought to the front
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Android Developers Bluetooth Tutorial
- java.lang.ClassNotFoundException android.support.v7.widget.Toolbar [Fix]
- Android: Save Data in local Db using Android Room
- Channel 50 SMSes received every few minutes Android Phones
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Changing Android Intent Tittle using java code
- Android : No Launcher activity found! Error
- How to change TextView or EditText Text Color on Focus and on Press
- How to display Toast on Button Click : Android
- Android : Execute some code after back button is pressed
- Stop android adb service from command prompt or terminal
- [Soluiton] You already have the latest version of Android Studio installed
- Create Custom Android AlertDialog
- Android R Cannot Be Resolved To A Variable
- How to make Android EditText not editable
More Posts:
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode - Java
- Common Microsoft Teams sign in errors and how to fix - Teams
- Portable Notepad++ for windows - NotepadPlusPlus
- Twitch chat down, error loading data, content unavailable, streaming problem - News
- How to check if a port is in use using terminal [Linux or macOS] - MacOS
- How to enable Do Not Disturb mode for Notification Center in Mac OS X 10.10 Yosemite - Mac-OS-X
- Using Java 8 Month Enum with Examples - Java
- Change Android Toast background color - Android