1

I am using Navigation Component to route between 2 different fragment. However, I failed to change the 2nd fragment's ActionBar color and I had no idea why. Below is my code.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setHasOptionsMenu(true)
        
    activity?.window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
    activity?.window?.statusBarColor = Color.WHITE

    actionBar = (activity as AppCompatActivity).supportActionBar

    actionBar?.title = chatViewModel.roomName
    actionBar?.setBackgroundDrawable(ColorDrawable(Color.WHITE)) <-- already set actionBar's color here

}

Then, I went to layout inspector to inspect the whole layout. I found that only the action_bar_container changed its color, not action_bar itself. Any idea on how to solve this?

Teo
  • 876
  • 9
  • 22

1 Answers1

1

To customize each fragment's tool bar individually, I have created a custom tool bar and using it inside each fragment's layout.

common_toolbar.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.appbar.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/const_item"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize">

        <TextView
            android:id="@+id/back_btn"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="@dimen/margin_normal"
            android:gravity="center"
            android:padding="@dimen/padding_five"
            android:text="Back"
            android:textColor="@android:color/white"
            android:textSize="@dimen/font_size_fifteen"
            app:drawableStartCompat="@drawable/ic_action_chevron_left"
            app:drawableTint="@android:color/white"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/title_toolbar"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_centerHorizontal="true"
            android:gravity="center"
            android:text="@string/menu"
            android:textColor="@android:color/white"
            android:textSize="@dimen/font_size_fifteen"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
        
    </androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
And then adding it to fragments's UI. Let's say, adding to fragment_user_details.xml
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <include layout="@layout/toolbar_common"
        android:id="@+id/toolbar_user_details"/>
    <!-- 
     Other View widgets
     -->
</androidx.constraintlayout.widget.ConstraintLayout>
As each fragment has its own tool bar, so I am getting it by it's id and customize as per my requirement. Make sure Activity has `Theme.MaterialComponents.Light.NoActionBar` theme.
NRUSINGHA MOHARANA
  • 1,489
  • 8
  • 13
  • Can custom toolbar have `onCreateOptionMenu`? Because I am using this to menu item action. – Teo Mar 11 '21 at 10:50
  • https://stackoverflow.com/questions/35648913/how-to-set-menu-to-toolbar-in-android/49098384 – NRUSINGHA MOHARANA Mar 11 '21 at 10:56
  • Thanks for your explanation. I struggle yesterday the whole day to implement this custom toolbar with custom style and now it works! – Teo Mar 12 '21 at 05:16