0

Add back navigation to an appBarLayout, I want to add a toolbar inside an appBarLayout in xml, and then add a back arrow to that toolbar.

I have followed this and tried to adjust it for my needs, with no luck. Display Back Arrow on Toolbar

Matthew Mitchell
  • 514
  • 4
  • 14

2 Answers2

2

According to Android Docs

AppBarLayout is a vertical LinearLayout

So you will need to include your toolbar and other components inside it, something like:

<android.support.design.widget.AppBarLayout
             android:layout_height="wrap_content"
             android:layout_width="match_parent">

         <android.support.v7.widget.Toolbar
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

         <android.support.design.widget.TabLayout
                 ...
                 app:layout_scrollFlags="scroll|enterAlways"/>

     </android.support.design.widget.AppBarLayout>

Then you can add your back navigation button to the toolbar:

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

And then calls to:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);

Override method:

@Override
    public boolean onSupportNavigateUp() {
        onBackPressed();
        return true;
    }
Matthew Mitchell
  • 514
  • 4
  • 14
0

In your activity do the following in onCreate

ActionBar bar = getSupportActionBar();

if(bar != null){
    bar.setDisplayHomeAsUpEnabled(true);
}
Akshatha S R
  • 1,237
  • 1
  • 15
  • 30