I was implementing an collpasing toolbar for my Activity. I was wondering if it is possible to set and navigation arrow to the Collapsing toolbar layout and set Onclick listener which will take me to previous activity. I can sucessfully implement such feature using only toolbar layout which is given as
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_details"
android:title="Disclaimer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"
android:minHeight="56dp"
android:layout_alignParentTop="true"
app:theme="@style/ThemeOverlay.AppCompat.Light"/>
and in OnCreate Method, I use
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_details);
setSupportActionBar(toolbar);
tutorProfile.this.setTitle(getIntent().getStringExtra("My Dashboard"));
toolbar.setNavigationIcon(R.drawable.ic_arrow_back_black_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent check = new Intent(CurrentActivity.this, PreviousActivity.class);
startActivity(check);
finish();
}
});
But in Collpasing toolbar layout how to achive same function. Here is the xml layout
<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsing"
android:layout_width="match_parent"
android:layout_height="150dip"
android:fitsSystemWindows="true"
app:contentScrim="#784242"
app:expandedTitleMarginBottom="20dp"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
app:layout_collapseMode="parallax"
android:src="@drawable/feedback_draw"
android:contentDescription="@string/app_name"
android:scaleType="centerCrop"
android:layout_width="match_parent"
android:layout_height="200dp" />
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="56dp"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
I searched for couple of minutes but couldn't find any solution. Thanks for help in advance.
Why don't you simple use toolbar.setDisplayHomeAsUpEnabled(true)
and then
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
android.R.id.home -> {
finish()
return true
}
}
return super.onOptionsItemSelected(item)
}