In the style add
<style name="MyAppTheme">
.......
<item name="android:forceDarkAllowed">true</item>
</style>
Determine whether it is a dark theme
boolean isNight = (activity.getApplicationContext().getResources().getConfiguration().uiMode
& Configuration.UI_MODE_NIGHT_YES) != 0;