Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display Back Arrow on Toolbar

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.

enter image description here

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?

like image 731
Huy Duong Tu Avatar asked Oct 30 '14 11:10

Huy Duong Tu


People also ask

How do I get rid of the back arrow on my android toolbar?

getActionBar(). setDisplayShowHomeEnabled(false); //disable back button getActionBar(). setHomeButtonEnabled(false); In a older android phone, the back button is removed with these two code lines.

How do I show the toolbar icon?

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.

How do I use Getupportactionbar?

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.


2 Answers

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; } 
like image 66
MrEngineer13 Avatar answered Sep 30 '22 09:09

MrEngineer13


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); } 
like image 33
Vasil Valchev Avatar answered Sep 30 '22 08:09

Vasil Valchev