I'm migrating from ActionBar
to Toolbar
in my application. But I don't know how to display and set click event on Back Arrow on Toolbar
like I did on Actionbar
.
With ActionBar
, I call mActionbar.setDisplayHomeAsUpEnabled(true)
. But there is no the similar method like this.
Has anyone ever faced this situation and somehow found a way to solve it?
getActionBar(). setDisplayShowHomeEnabled(false); //disable back button getActionBar(). setHomeButtonEnabled(false); In a older android phone, the back button is removed with these two code lines.
From the Menu bar, click View > Toolbars > Customize. The Customize dialog and toolbar must be is displayed to perform this action. From the toolbar, drag the icon to any area outside of the toolbar, release your mouse.
To use the ActionBar utility methods, call the activity's getSupportActionBar() method. This method returns a reference to an appcompat ActionBar object. Once you have that reference, you can call any of the ActionBar methods to adjust the app bar. For example, to hide the app bar, call ActionBar.
If you are using an ActionBarActivity
then you can tell Android to use the Toolbar
as the ActionBar
like so:
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar); setSupportActionBar(toolbar);
And then calls to
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true);
will work. You can also use that in Fragments that are attached to ActionBarActivities
you can use it like this:
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true); ((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
If you are not using ActionBarActivities
or if you want to get the back arrow on a Toolbar
that's not set as your SupportActionBar
then you can use the following:
mActionBar.setNavigationIcon(getResources().getDrawable(R.drawable.ic_action_back)); mActionBar.setNavigationOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //What to do on back clicked } });
If you are using android.support.v7.widget.Toolbar
, then you should add the following code to your AppCompatActivity
:
@Override public boolean onSupportNavigateUp() { onBackPressed(); return true; }
I see a lot of answers but here is mine which is not mentioned before. It works from API 8+.
public class DetailActivity extends AppCompatActivity @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_detail); // toolbar Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); // add back arrow to toolbar if (getSupportActionBar() != null){ getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayShowHomeEnabled(true); } } @Override public boolean onOptionsItemSelected(MenuItem item) { // handle arrow click here if (item.getItemId() == android.R.id.home) { finish(); // close this activity and return to preview activity (if there is any) } return super.onOptionsItemSelected(item); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With