Android Home button in minimized image toolbar

I want to save the home button on the toolbar. I have a minimizing toolbar with an image that disappears when scrolling up. In my other toolbar, I implemented a toolbar using

getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

but now it will not work. I can’t see the button when the toolbar is minimized (when the image is not displayed) and when the toolbar is open (when the image is visible and the toolbar expands).

My toolbar code:

    Toolbar toolbar = (Toolbar) findViewById(R.id.anim_toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbar.setTitle("Awesome");
+4
source share
2 answers

You have to put ToolBar in CollapsingToolbarLayout

<android.support.design.widget.AppBarLayout
        android:layout_height="192dp"
        android:layout_width="match_parent">
    <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">
        <android.support.v7.widget.Toolbar
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin" />
        </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
+2
source

You can add a navigation icon,

   final Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
   toolbar.setNavigationIcon(R.drawable.nav_icon);

onOptionsItemSelected :

@Override
public boolean onOptionsItemSelected(final MenuItem item) {
    final int id = item.getItemId();
    if (id == android.R.id.home) {
        //finish();
    }
 //........
}
-1

Source: https://habr.com/ru/post/1598611/


All Articles