Home > Net >  Changing Compose Light/Dark XML theme without extending AppCompatActivity
Changing Compose Light/Dark XML theme without extending AppCompatActivity

Time:04-24

I would like to programatically alter the light/dark theme from a user setting in Compose if the user decides to override their OS level setting and have the values read from the colors and night/colors files.

Jetpack compose by default extends ComponentActivity as extending AppCompatActivity would be extending FragmentActivity which is not needed when doing a Compose only app.

When I'm extending ComponentActivity the AppCompatDelegate methods to change light/dark mode no longer function because I'm not extending AppCompatActivity

E.g AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)

If I instead extend AppCompatActivity it works as you would expect.

It seems silly to extend AppCompatActivity when I don't need to, but I can't find any other way to tell a compose app to use the correct xml values based off user input?

Am I missing something, or does this capability not exist yet, and I need to extend AppCompatActivity

Compose has the programmatic themes which I can change, but I want to use the XML color files.

CodePudding user response:

You need to extend AppCompatActivity if you want to use any API from AppCompat, including the light/dark mode theme changing using XML color files available via the AppCompatDelegate APIs.

  • Related