Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Status Bar color not consistent between Fragments with CollapsingToolbar and only MaterialToolbar #4324

Open
ArcherEmiya05 opened this issue Oct 2, 2024 · 0 comments

Comments

@ArcherEmiya05
Copy link

ArcherEmiya05 commented Oct 2, 2024

Description:
I am setting android:windowTranslucentStatus to true to achieve a transparent status bar to blend with the ImageView of a CollapsingToolbar when expanded and it follows android:statusBarColor when collapsed in FragmentA. However when navigating to FragmentB that only has MaterialToolbar the status bar color change and does not follow android:statusBarColor of the base theme.

Expected behavior:
If I set statusBarForeground at the Theme.App.NoActionBar.TranslucentStatusBar then there will be no translucency of status bar on both destinations, I want both behaviors.

  1. Translucent status bar when CollapsingToolbar is expanded and colored status bar when collapsed on FragmentA.
image image
  1. Controlled color on status bar for FragmentB, not a variant color of the default background of MaterialToolbar. I noticed that it is using a light version of the default background color of MaterialToolbar shown in the right image which is pinkish.
image image

So far I cannot achieve the second requirement.

Source code:

Toolbars theme set

<resources xmlns:tools="http://schemas.android.com/tools">
    <!-- Toolbar style on both light and night mode -->
    <style name="AppToolbar" parent="Widget.Material3.Toolbar.Surface" tools:keep="@style/AppToolbar">
        <item name="contentInsetStart">0dp</item>
        <item name="contentInsetEnd">0dp</item>
        <item name="contentInsetStartWithNavigation">0dp</item>
        <item name="materialThemeOverlay">@style/ThemeOverlay.App.Toolbar</item>
    </style>

    <style name="ThemeOverlay.App.Toolbar" parent="" tools:keep="@style/ThemeOverlay_App_Toolbar">
        <!-- Background color -->
        <item name="colorSurface">@color/colorWhite_Primary</item>
        <!-- Title and Navigation icon color -->
        <item name="colorOnSurface">@color/colorPrimaryDark_White</item>
        <!-- Subtitle and Action item color -->
        <item name="colorOnSurfaceVariant">@color/colorPrimaryDark_White</item>
        <!-- Action item and Overflow menu color when `colorOnSurfaceVariant` is not working -->
        <!-- Drawer icon and Navigation icon also use this -->
        <item name="colorControlNormal">@color/colorPrimaryDark_White</item>
    </style>

    <!-- CollapsingToolbar style on both light and night mode -->
    <style name="AppCollapsingToolbar" parent="Widget.Material3.CollapsingToolbar" tools:keep="@style/AppCollapsingToolbar">
        <item name="titleCollapseMode">scale</item>
        <item name="scrimAnimationDuration">700</item>
        <!-- Collapsed Toolbar background -->
        <item name="contentScrim">?attr/colorPrimary</item>
        <item name="expandedTitleTextColor">@android:color/white</item>
        <item name="collapsedTitleTextColor">@color/colorPrimaryDark_White</item>
        <item name="statusBarScrim">@color/colorGray_PrimaryDark</item>
        <item name="collapsedTitleTextAppearance">@style/TextAppearance.App.CollapsingToolbar.Collapsed</item>
        <item name="expandedTitleTextAppearance">@style/TextAppearance.App.CollapsingToolbar.Expanded</item>>
    </style>

    <style name="TextAppearance.App.CollapsingToolbar.Expanded" parent="TextAppearance.Material3.HeadlineSmall" tools:keep="@style/TextAppearance_App_CollapsingToolbar_Expanded">
        <item name="android:shadowRadius">4</item>
        <item name="android:shadowDy">1</item>
        <item name="android:shadowColor">@android:color/black</item>
    </style>

    <style name="TextAppearance.App.CollapsingToolbar.Collapsed" parent="TextAppearance.Material3.TitleLarge" tools:keep="@style/TextAppearance_App_CollapsingToolbar_Collapsed">
        <!-- no-op -->
    </style>

    <!-- ActionBar -->
    <!-- Used by some external module like Firebase UI Auth or OSS License Android -->
    <style name="AppActionBar" parent="" tools:keep="@style/AppActionBar">
        <!-- Background color -->
        <item name="colorSurface">@color/colorWhite_Primary</item>
        <!-- Title and Navigation icon color -->
        <item name="colorOnSurface">@color/colorPrimaryDark_White</item>
        <!-- Subtitle and Action item color -->
        <item name="colorOnSurfaceVariant">@color/colorPrimaryDark_White</item>
        <!-- Action item and Overflow menu color when `colorOnSurfaceVariant` is not working -->
        <!-- Drawer icon and Navigation icon also use this -->
        <item name="colorControlNormal">@color/colorPrimaryDark_White</item>
    </style>
</resources>

App's base theme

<resources xmlns:tools="http://schemas.android.com/tools">

    <!-- Base application theme -->
    <style name="Theme.App" parent="Theme.Material3.DayNight">
        <!-- Primary brand color -->
        <item name="colorPrimary">@android:color/white</item>

        <item name="toolbarStyle">@style/AppToolbar</item>

        <item name="actionBarSize">@dimen/AppTopBarSize</item>

        <item name="collapsingToolbarLayoutStyle">@style/AppCollapsingToolbar</item>

        <!-- Status bar color -->
        <item name="android:statusBarColor">@android:color/darker_gray</item>

        <item name="android:forceDarkAllowed" tools:targetApi="q">false</item>
    </style>

    <!-- Activity with disabled ActionBar -->
    <style name="Theme.App.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <!-- Activity theme for CollapsingToolbarLayout -->
    <style name="Theme.App.NoActionBar.TranslucentStatusBar">
        <item name="android:windowTranslucentStatus">true</item>
    </style>

</resources>

Manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

     <application
            android:name=".App"
            android:enableOnBackInvokedCallback="true"
            android:icon="@mipmap/ic_launcher"
            android:label="@string/app_name"
            android:roundIcon="@mipmap/ic_launcher_round"
            android:supportsRtl="true"
            android:theme="@style/Theme.App"
            android:usesCleartextTraffic="false"
            tools:targetApi="tiramisu">

            <activity
                android:name=".presentation.NewsDetailActivity"
                android:theme="@style/Theme.App.NoActionBar.TranslucentStatusBar" />

    </application>

</manifest>

FragmentA

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".presentation.NewsDetailActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:id="@+id/collapsingToolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/collapsingToolbarLayoutLargeSize"
            android:fitsSystemWindows="true"
            app:layout_scrollFlags="scroll|snap|enterAlways|enterAlwaysCollapsed"
            app:maxLines="3">

            <androidx.appcompat.widget.AppCompatImageView
                android:id="@+id/featuredImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:adjustViewBounds="true"
                android:fitsSystemWindows="true"
                android:src="@drawable/logo_dynamic_placeholder"
                android:background="@color/colorWhiteDark_Primary"
                app:layout_collapseMode="parallax" />

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="@android:color/transparent"
                app:navigationIcon="?attr/homeAsUpIndicator"
                app:navigationContentDescription="@string/abc_action_bar_up_description"
                app:layout_collapseMode="pin"
                app:layout_scrollFlags="scroll|snap|enterAlways|enterAlwaysCollapsed"
                app:title="@tools:sample/cities" />

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

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

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

        <androidx.appcompat.widget.LinearLayoutCompat
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <org.sufficientlysecure.htmltextview.HtmlTextView
                android:id="@+id/description_html_txt_v"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingHorizontal="@dimen/space_12"
                android:paddingTop="@dimen/space_48"
                android:paddingBottom="@dimen/space_12"
                android:text="@tools:sample/lorem/random"
                android:textSize="@dimen/textSize14" />

            <androidx.appcompat.widget.LinearLayoutCompat
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:paddingVertical="@dimen/space_16"
                android:visibility="visible">

                <com.google.android.material.textview.MaterialTextView
                    android:id="@+id/labelTxt"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:paddingHorizontal="@dimen/space_12"
                    android:paddingBottom="@dimen/space_8"
                    android:text="@string/related_content"
                    android:textAllCaps="true"
                    android:textColor="@color/colorPrimaryDark_Accent"
                    android:textSize="@dimen/textSize24" />

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/feedRecycler"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:clipToPadding="false"
                    android:nestedScrollingEnabled="false"
                    android:paddingHorizontal="@dimen/space_12" />

            </androidx.appcompat.widget.LinearLayoutCompat>

        </androidx.appcompat.widget.LinearLayoutCompat>

    </androidx.core.widget.NestedScrollView>

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/visitWeb"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:useCompatPadding="true"
        android:contentDescription="@string/news"
        android:src="@drawable/ic_open_tab"
        app:layout_anchor="@id/appBar"
        app:layout_anchorGravity="bottom|end" />

    <FrameLayout
        android:id="@+id/ad_frame"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center|bottom" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

FragmentB

<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".presentation.NewsDetailActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:fitsSystemWindows="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_scrollFlags="scroll|snap"
            app:navigationContentDescription="@string/abc_action_bar_up_description"
            app:navigationIcon="?attr/homeAsUpIndicator"
            app:title="@string/app_name"
            app:subtitle="@string/app_name"
            app:titleCentered="true"
            app:subtitleCentered="true" />

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

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Android API version: 5.0

Material Library version: 1.12.0

Device: Pixel (Emulator)

@ArcherEmiya05 ArcherEmiya05 changed the title Status Bar color not consistent between Fragment with CollapsingToolbar and only MaterialToolbar Status Bar color not consistent between Fragments with CollapsingToolbar and only MaterialToolbar Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants