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,
- Check Internet Connection WIFI 4G is active on Android Programmatically
- Android Emulator cannot be opened because the developer cannot be verified. [M1 Mac]
- How to Change Android Toast Position?
- Fail to connect to camera service Android java RuntimeException
- How to create Custom RatingBar Android Programming Tutorial
- Fixing Android unknown error 961 while downloading app
- Android AlertDialog with Yes No and Cancel Button
- Share or Send SMS via Android Intent
- The Android Virtual Device myEmulator is currently running an emulator and cannot be deleted.
- Pass data between two Android Activities and access it using Intent
- SQLite with Android Easy to Understand Tutorial that covers Select, Insert, Update and Delete
- [FIX] AndroidRuntime: FATAL EXCEPTION: main - java.lang.RuntimeException NullPointerException
- Android EditText Cursor Colour appears to be white
- Android Development - How to switch between two Activities
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Android : Remove ListView Separator/divider programmatically or using xml property
- Android is starting optimizing... app 1 of 1
- java.lang.NoClassDefFoundError android.support.v4.content.LocalBroadcastManager
- AlertDialog with single button example : Android
- Android : Exception raised during rendering: action_bar API 22
- Maven : java.lang.ClassNotFoundException: Xmx512m
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian)
- Center align text in TextView Android Programming
- How to Download and Install Android adb Tool on Linux, Mac or Windows
- Multiline EditText in Android Example
More Posts:
- [fix] Java JDBC SQLSyntaxErrorException: Unknown database - Java
- New-SPLogFile PowerShell - create new SharePoint log file - SharePoint
- Android: Unknown error code during application install : - Android
- Java JDBC Transition Management using PreparedStatement Examples - Java
- fix macOS: The digital signature on the update is missing or invalid. Ventura - MacOS
- How to redirect SharePoint Site Collection to different URL - SharePoint
- How to lock cells in Microsoft Excel for Mac - Windows
- How to URL Decode a Query String in Python - Python