21

Im trying to use android dark theme. I install android studio 3.5 preview.

compileSdkVersion 28 targetSdkVersion 28

but still getting this error. Is this dark theme bug or Im doing something wrong?

build.gradle{
dependencies {
    // ...
    implementation 'com.google.android.material:material:1.1.0-alpha06'
    // ...
  }}

styles.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <item name="android:forceDarkAllowed">true</item>
</style>

project link

https://github.com/googlesamples/android-DarkTheme

1

5 Answers 5

20

You need to up your compiledSdkVersion to 29, in you app build.gradle:

android {
    compileSdkVersion 29
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 29
        ...
}

Then you should be good to go!

Sign up to request clarification or add additional context in comments.

2 Comments

What if I can't / don't want to use version 29 but stay at 28?
Then you must not use android:forceDarkAllowed because below 29 dark mode is unknown feature. And how you app behaves when installed on 29 and up? Check yourself.
8

I was searching for an answer and as @Ruben pointed out)

Changing compiledSdkVersion 28 to compileSdkVersion 'android-Q' should solve the issue. android:forceDarkAllowed attribute was only added in Android Q.

Unrelated to this issue, but it seems like you need to also update 'androidx.appcompat:appcompat:1.1.0-alpha04' to 'androidx.appcompat:appcompat:1.1.0-alpha05' or the theme switching doesn't work properly.

1 Comment

Yes, this is correct - thanks for pointing it out. If you don't update to appcompat version, it won't switch themes immediately. You have to restart the app to make the switch.
3

I found the answer in android studio suggestions. for clarification the tag android:forceDarkAllowed is only found in api level 29+ so what we should do is create a folder values-v29 and then write the tag in it like below

in file values/____.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    //removed from here
</style>

in file values-v29/____.xml

<style name="Theme.MyApp" parent="Theme.MaterialComponents.DayNight">
    <item name="android:forceDarkAllowed">true</item>
</style>

1 Comment

I guess that this answer is too new to attract people googling for this issue (the question is two years old), however this was the solution I needed. I need compatibility with API level 26 and increasing it to 29 just to fix an issue with Xiaomi phones is just a no go for me. For others looking to apply it, the names of both files where "styles.xml" in my case, and worked like a charm both in Android 8 and the stupid Xiaomi phone that was messing the dark mode.
2

I encountered the same error message when I tried to run my previously untouched Angular based NativeScript "Hello World" android application. (In command prompt: tns create, tns run android --bundle)

For me the solution was: 1. Open SDK Manager in Android Studio 2. Install Android 10.0 (Q) (API Level: 29) SDK Platform.

After these steps I could start running my app without any errors!

2 Comments

I have the same setting still getting the error with nativescript 6.2
I've done all of the above and still get this error. Surely it cant be this hard. What other gotchas do i need to be aware of?
-6

See the Nativescript docs.

Remove below line:

<item name="android:forceDarkAllowed">true</item>

from file

<project>/app/App_Recoures/Android/src/main/res/values/styles.xml

1 Comment

It is a question about native development and people put the line there with an intention.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.