How to Add Back Arrow in Toolbar Android

How to Add Back Arrow in Toolbar Android

To how to add back arrow in toolbar android, set your Toolbar as the Activity’s support ActionBar, then enable the Up button with supportActionBar?.setDisplayHomeAsUpEnabled(true) and supportActionBar?.setDisplayShowHomeEnabled(true). If you want the arrow to actually navigate back, handle it in onSupportNavigateUp() or through your Navigation Component setup.

Quick answer

The back arrow in an Android Toolbar is usually the Up button, not the system back button. It appears only after the Toolbar is attached to the Activity as the ActionBar. In most apps, the full setup is:

  • Call setSupportActionBar(toolbar)
  • Enable the Up button with supportActionBar?.setDisplayHomeAsUpEnabled(true)
  • Optionally handle clicks in onSupportNavigateUp()

If you skip the first step, the arrow often will not show at all.

Add a back arrow to Toolbar in Android

Add a back arrow to Toolbar in Android

The most reliable way to show a back arrow is to use a Toolbar inside an AppCompatActivity, set it as the support ActionBar, then turn on the Up indicator. This works with both the standard Toolbar and MaterialToolbar.

1. Add the Toolbar to your layout

Add the Toolbar to your layout

Here is a basic XML example:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" />

If you are using Material Components, you can also use MaterialToolbar:

<com.google.android.material.appbar.MaterialToolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary" />

2. Set the Toolbar as the ActionBar

Set the Toolbar as the ActionBar

In your Activity, connect the Toolbar to the Activity before enabling the arrow. This step is required because the back arrow is controlled by the ActionBar API.

Kotlin example

class DetailActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_detail)

        val toolbar = findViewById<Toolbar>(R.id.toolbar)
        setSupportActionBar(toolbar)

        supportActionBar?.setDisplayHomeAsUpEnabled(true)
        supportActionBar?.setDisplayShowHomeEnabled(true)
    }

    override fun onSupportNavigateUp(): Boolean {
        onBackPressedDispatcher.onBackPressed()
        return true
    }
}

Java example

public class DetailActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_detail);

        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }
    }

    @Override
    public boolean onSupportNavigateUp() {
        onBackPressedDispatcher.onBackPressed();
        return true;
    }
}

3. Decide what the arrow should do

Once the arrow appears, you can let it behave like the system back action or route it to a specific destination. For many screens, calling onBackPressedDispatcher.onBackPressed() is enough.

If you want custom behavior, you can replace that line with your own navigation logic, such as finishing the Activity or opening a parent screen.

Why the back arrow may not show

Why the back arrow may not show

If the back arrow does not appear, the issue is usually one of these:

  • The Toolbar was not passed to setSupportActionBar()
  • supportActionBar is null because the Activity is not using AppCompat correctly
  • A theme or style is hiding the ActionBar unexpectedly
  • You are using a custom toolbar, but it was never set as the app bar
  • The Up button was enabled before the Toolbar became the ActionBar

A simple checklist usually solves the problem:

  1. Confirm the Activity extends AppCompatActivity
  2. Confirm the Toolbar ID matches your layout
  3. Call setSupportActionBar(toolbar)
  4. Then call setDisplayHomeAsUpEnabled(true)
  5. Check that your theme is not forcing a different ActionBar setup

Back button vs Up button

Developers often call the Toolbar arrow a back arrow, but in Android documentation it is usually the Up button. The system back button and the Up button are related, but they are not the same thing.

Navigation typeWhat it doesWhere it appears
System back buttonReturns to the previous screen in the activity or task historyDevice navigation bar or gesture area
Toolbar Up buttonMoves to the logical parent screen in your appToolbar or app bar

In simple apps, both actions may feel the same. In larger apps, the Up button should usually follow your app’s structure, not just the recent screen history.

Using Navigation Component

If your app uses Jetpack Navigation, the arrow can be controlled automatically by the NavController. In that case, you usually do not manually manage every back arrow screen by screen.

A common pattern is:

  1. Set the Toolbar as the Activity’s ActionBar
  2. Connect the NavController to the app bar configuration
  3. Let Navigation Component show or hide the Up arrow when appropriate

This is useful when your app has multiple destinations and a graph-based navigation flow. If the destination is not top-level, the arrow can appear automatically. Top-level destinations usually show no Up arrow.

Common mistakes

  • Using Toolbar but not calling setSupportActionBar(), the arrow will not behave like an ActionBar arrow.
  • Forgetting null checks, supportActionBar can be null if the Toolbar is not set correctly.
  • Confusing system back with Up navigation, they solve different navigation problems.
  • Using the wrong theme, some themes already include an ActionBar and can conflict with a custom Toolbar.
  • Setting the arrow before the Toolbar is attached, order matters.

Best practices for Toolbar back navigation

  • Use AppCompatActivity for classic Toolbar setup
  • Prefer a single, consistent navigation pattern across your app
  • Use the Up button for parent navigation, not just arbitrary screen-to-screen history
  • Keep the toolbar title clear so users understand where they are
  • If using Navigation Component, let it manage app bar behavior when possible

Frequently Asked Questions About how to add back arrow in toolbar android

How do I add a back arrow to a Toolbar in Android?

Set the Toolbar as the ActionBar with setSupportActionBar(toolbar), then enable the Up button with supportActionBar?.setDisplayHomeAsUpEnabled(true).

Why is the back arrow not showing in my Toolbar?

Most often, the Toolbar was not set as the ActionBar, or the Activity is using a theme or setup that conflicts with it. Check that supportActionBar is not null.

Do I need an ActionBar to show the Toolbar back arrow?

Yes, in the standard AppCompat setup the arrow is controlled through the ActionBar APIs. The Toolbar must first become the Activity’s support ActionBar.

What is the difference between the back button and the Up button in Android?

The back button follows navigation history, while the Up button follows your app’s logical hierarchy. They can behave similarly, but they are not always the same.

How do I handle toolbar back navigation in Kotlin?

Override onSupportNavigateUp() and call onBackPressedDispatcher.onBackPressed() or your own navigation logic.

Can I use a MaterialToolbar instead of a regular Toolbar?

Yes. A MaterialToolbar works with the same ActionBar setup, as long as you call setSupportActionBar() and enable the Up button.

What if I use Jetpack Navigation?

In that case, the NavController can manage the arrow automatically when you connect it to your app bar configuration.

Conclusion

The cleanest way to add a back arrow in Android is to make the Toolbar your support ActionBar, then enable the Up button. That is the core of how to add back arrow in toolbar android. If the arrow still does not appear, check your theme, your Activity base class, and the order of your setup. For apps using Navigation Component, let the navigation framework manage the app bar when possible.

Similar Posts

Leave a Reply

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