Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Home button in collapsing toolbar with image

I am looking to keep the home button in my toolbar. I have a collapsing toolbar with an image which disappears if scrolled up. In my other toolbar I implemented the tool bar with:

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

but it doesn't work now. I can't see the button both when the toolbar is collapsed (when the image isn't shown) and when the toolbar is open (when the image is visible and the toolbar is expanded).

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");
like image 238
user4789408 Avatar asked Nov 21 '22 11:11

user4789408


1 Answers

You should 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>
like image 194
Kishan Vaghela Avatar answered Jun 01 '23 01:06

Kishan Vaghela