Noam
Noam

Reputation: 535

CollapsingToolbarLayout hides my back button

I created collapsing tool bar, but it seems I can't inflate the back button, or other menu items.

I inflate my back button like this:

    toolbar_detail = findViewById(R.id.tool_de);
    setSupportActionBar(toolbar_detail);

    if (toolbar_detail != null) {
        Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);}

And this is my collapsing app bar layout:

  <?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:fitsSystemWindows="true"
    tools:context=".Detail">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        android:id="@+id/app_bar">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/coolaps_tool_bar"
            android:fitsSystemWindows="true"
            android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:contentScrim="@color/colorPrimary">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="250dp"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax"
                android:src="@drawable/ic_launcher_background"
                android:contentDescription="zx" />

            <androidx.appcompat.widget.Toolbar
                android:id="@+id/tool_de"
                android:layout_width="0dp"
                android:layout_height="?attr/actionBarSize"
                android:background="@color/colorPrimary"
                android:elevation="4dp"
                app:layout_collapseMode="pin"
               app:popupTheme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
                app:titleTextColor="@android:color/white" />

        </com.google.android.material.appbar.CollapsingToolbarLayout>


    </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="sdsaa"
            />
    </androidx.core.widget.NestedScrollView>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

How can I fix this problem? I tried several approachs like specify parent activity etc doesn't seem to be working

Upvotes: 0

Views: 948

Answers (1)

Arrowsome
Arrowsome

Reputation: 2859

  1. 0dp makes no sense in your <Toolbar> so make it match_parent
  2. Add your icon with one of these ways:

Add back button on Toolbar directly:

<androidx.appcompat.widget.Toolbar
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:navigationIcon="@drawable/my_back_button_icon"/>

If you want to use your default ActionBar back button:

getSupportActionBar()).setDisplayHomeAsUpEnabled(true);

Be sure to set a parent inside your AndroidManifest:

<activity android:name=".SecondActivity" android:parentActivityName=".MainActivity">

Upvotes: 1

Related Questions