Reputation: 597
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
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
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