Yahya Naimi
Yahya Naimi

Reputation: 61

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

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

Upvotes: 6

Views: 1373

Answers (3)

georgij
georgij

Reputation: 2200

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

Upvotes: 5

Lam Nguyen
Lam Nguyen

Reputation: 585

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

Upvotes: 0

Andrey Kijonok
Andrey Kijonok

Reputation: 416

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

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

In OnCreate()

Upvotes: 2

Related Questions