Home > Software engineering >  I am unable to do dark theme in my app with using forceDarkAllowed
I am unable to do dark theme in my app with using forceDarkAllowed

Time:03-08

*I have used forceDarkAllowed but it's not working on my android mobile. It's still showing me a light theme. I have shared my theme.xml file here. I am trying to implement a dark theme without using any programming coding in any activity *

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">

    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/purple_700</item>
    <item name="colorPrimaryVariant">@color/purple_200</item>
    <item name="colorOnPrimary">@color/teal_200</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/teal_200</item>
    <item name="colorSecondaryVariant">@color/teal_700</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
    <item name="android:textColorSecondary">@color/black</item>
    <item name="android:forceDarkAllowed" tools:ignore="NewApi">true</item>
</style>
<style name="Theme.MyApplication.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>

</style>
<style name="AppTheme">
    <item name="android:windowActionBarOverlay">true</item>
</style>
<style name="Theme.MyApplication.AppBarOverlay">
    <item name="backgroundTint">@color/black</item>
</style>

<style name="Theme.MyApplication.PopupOverlay">
    <item name="backgroundTint">@color/white</item>
    <item name="background">@color/white</item>
    <item name="colorPrimary">@color/white</item>
</style>

<!-- Change tab text appearance -->
<style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
    <item name="textAllCaps">true</item>
    <item name="android:textAppearance">@style/CustomTabWidgetText</item>
</style>

<style name="CustomTabWidgetText" parent="@android:style/TextAppearance.Widget.TabWidget">
    <item name="android:textSize">20sp</item>
</style></resources>

CodePudding user response:

Try this in application class call in onCreate()-

 AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

and for more details please find -https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#changing_themes_in-app

CodePudding user response:

The reason could be that your theme inherits from Theme.MaterialComponents.DayNight.DarkActionBar. Try to use Theme.MaterialComponents.Light.DarkActionBar instead.

According to https://developer.android.com/guide/topics/ui/look-and-feel/darktheme:

If your app uses a dark theme (such as Theme.Material), Force Dark will not be applied. Similarly, if your app's theme inherits from a DayNight theme, Force Dark will not be applied, due to the automatic theme switching.

  • Related