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.
✌️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,
- Increase Android Emulator Timeout time
- Android : Remove ListView Separator/divider programmatically or using xml property
- Error : Invalid key hash.The key hash does not match any stored key hashes
- How to Change Android Toast Position?
- Android Alert Dialog with Checkboxes example
- Android : No Launcher activity found! Error
- Android Development: Spinners with Example
- Failed to sync Gradle project Error:failed to find target android-23
- INSTALL_FAILED_INSUFFICIENT_STORAGE Android Error
- Disable Fading Edges Scroll Effect Android Views
- How to create Toast messages in Android?
- Channel 50 SMSes received every few minutes Android Phones
- Android xml error Attribute is missing the Android namespace prefix [Solution]
- Create Custom Android AlertDialog
- How To Disable Landscape Mode in Android Application
- Android Development - How to switch between two Activities
- incorrect line ending: found carriage return (\r) without corresponding newline (\n)
- Generate Facebook Android SDK keyhash using java code
- Android Error Generating Final Archive - Debug Certificate Expired
- 21 Useful Android Emulator Short-cut Keyboard Keys
- Android RatingBar Example
- 11 Weeks of Android Online Sessions-15-Jun-to-28-Aug-2020
- Download interrupted: Unknown Host dl-ssl.google.com Error Android SDK Manager
- fill_parent vs match_parent vs wrap_content
- Android : Connection with adb was interrupted 0 attempts have been made to reconnect
More Posts:
- Simple Struts 2 Tutorial in eclipse with tomcat 7 server - Java
- BeanDefinitionStoreException IOException parsing XML document from class path resource [spring.xml] - Java
- Difference between Sublime Text vs Visual Studio Code (VS Code) - Sublime-Text
- How to Get Year from a Date in Java - Java
- 9 Ways to Loop Java Map (HashMap) with Code Examples - Java
- Git: Step-by-Step - How to Push Local Brach to GitHub - Git
- Steps to Compare Two files in Sublime Text Side-by-Side - Sublime-Text
- How to access Trash Folder using Mac Terminal Command - MacOS