How to Change color of Dark action bar at night mode?
Asked Answered
L

3

6

I treid to change the background color of the Action bar to another color istead of the default color. I am using :

implementation 'com.google.android.material:material:1.2.0-alpha05'

... values/styles.xml:

    <resources>

<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryDarkColor</item>
        <item name="colorPrimaryVariant">@color/primaryLightColor</item>
        <item name="colorOnPrimary">@color/primaryTextColor</item>
        <item name="colorSecondary">@color/secondaryColor</item>
        <item name="colorSecondaryVariant">@color/secondaryDarkColor</item>
        <item name="colorOnSecondary">@color/secondaryTextColor</item>
        <item name="android:windowBackground">@color/activityBackground</item>
    </style>
</resources>

...and:night/colors.xml

<color name="primaryDarkColor">#430F58</color>
<color name="primaryColor">#F7F2F8</color>
<color name="secondaryColor">#9e9e9e</color>
<color name="secondaryDarkColor">#707070</color>
<color name="primaryTextColor">#eceff1</color>
<color name="activityBackground">#121212</color>

...mainActivity.kt:

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

    }[enter image description here][1]
}

ll works great,but the color of my action bar remaimining black all time.

Can you help me ? thanks

Laurynlausanne answered 11/3, 2020 at 18:36 Comment(0)
B
5

Try <item name="colorSurface">@color/someColor</item> in your dark mode theme.

Breakout answered 17/6, 2022 at 14:7 Comment(0)
C
2

If you want ActionBar color be same when Dark Mode and Usual mode. You can use

supportActionBar?.setBackgroundDrawable(getDrawable(R.color.color))

In OnCreate()

Coplin answered 15/2, 2021 at 15:3 Comment(0)
A
0

I use android:background="@color/colorPrimary" in layout

Anglo answered 29/8, 2021 at 8:34 Comment(1)
This only changes the background for the layout and not the actionbarCareworn

© 2022 - 2024 — McMap. All rights reserved.