Antict
Antict

Reputation: 597

Back button in Toolbar without ActionBarActivity

I'm using a Toolbar inside a Fragment (which covers the whole screen) and I want to show the back button, but I can't use getSupportActionBar().setDisplayHomeAsUpEnabled(true) because I'm not using ActionBarActivity.

I need something which works with API 9, so I can't use Toolbar's setNavigationIcon.

Upvotes: 1

Views: 339

Answers (2)

pelotasplus
pelotasplus

Reputation: 10052

In my case, I use the following code:

public void showBackButton(final Runnable runnable) {
    toolbar.setNavigationIcon(getResources().getDrawable(R.drawable.abc_ic_ab_back_mtrl_am_alpha));
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            runnable.run();
        }
    });
}

Upvotes: 1

Blackbelt
Blackbelt

Reputation: 157457

call setNavigationIcon(int), on the Toolbar instance. From the documentation:

Set the icon to use for the toolbar's navigation button.

The navigation button appears at the start of the toolbar if present. Setting an icon will make the navigation button visible.

if you need a ClickListener for it, you can use setNavigationOnClickListener(View.OnClickListener)

Upvotes: 2

Related Questions