0

I have two fragments. When I switch from the first fragment to the second, a back arrow is displayed on the toolbar. How to make that there is no void on the home screen instead of the back arrow, but there is another button?

Toolbar:

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:elevation="4dp"
    android:minHeight="64dp"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

Both screens: 1

yaromchikV
  • 79
  • 10
  • Does this answer your question? [How to change Toolbar Navigation and Overflow Menu icons (appcompat v7)?](https://stackoverflow.com/questions/29038861/how-to-change-toolbar-navigation-and-overflow-menu-icons-appcompat-v7) – BabyishTank Nov 27 '21 at 18:35
  • @BabyishTank, No, "setNavigationIcon()" doesn't work for me. – yaromchikV Nov 27 '21 at 18:43

1 Answers1

0

I solved the problem by adding my own button.

<ImageButton
    android:id="@+id/button_settings"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="?attr/actionBarItemBackground"
    app:srcCompat="@drawable/ic_settings" />

Please note by specifying android:background="?attr/actionBarItemBackground" you can achieve the same clicking effect as a regular toolbar button.

If you want to hide your button on some screens, add the addOnDestinationChangedListener to NavController:

navController.addOnDestinationChangedListener { _, destination, _ ->
    binding.buttonSettings.visibility =
        if (destination.id == R.id.account_add_fragment) View.GONE else View.VISIBLE
}
yaromchikV
  • 79
  • 10