0

I am new to android studio and I am designing an app which must open a pdf file from the android storage device. I set the background color to black in the activity view and it works fine as long as I am not using my android in Dark mode. Once I turn on dark mode, the background color switches from black to white.

The activity view code:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ViewPdfActivity"
android:background="@color/black"
android:orientation="vertical"
>

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:background="@color/black"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

how it looks when my android is not in dark mode and how i basically want it to look like

how it looks like when my android is in dark mode and I don't want it look like that

Themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.PDFwordmeaning" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/black</item>
    <item name="colorSecondaryVariant">@color/black</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>
<style name="AppTheme" parent="@style/Theme.AppCompat.Light.NoActionBar">
    <item name="windowNoTitle">true</item>
    <item name="android:windowNoTitle">true</item>
</style>

night\themes.xml

<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.PDFwordmeaning" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
    <!-- Primary brand color. -->
    <item name="colorPrimary">@color/black</item>
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/black</item>
    <!-- Secondary brand color. -->
    <item name="colorSecondary">@color/black</item>
    <item name="colorSecondaryVariant">@color/black</item>
    <item name="colorOnSecondary">@color/black</item>
    <!-- Status bar color. -->
    <item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
    <!-- Customize your theme here. -->
</style>

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="purple_200">#FFBB86FC</color>
    <color name="purple_500">#FF6200EE</color>
    <color name="purple_700">#FF3700B3</color>
    <color name="teal_200">#FF03DAC5</color>
    <color name="teal_700">#FF018786</color>
    <color name="black">#FF000000</color>
    <color name="white">#FFFFFFFF</color>
</resources>
perpetualdarkness
  • 135
  • 1
  • 4
  • 18

2 Answers2

4

I encountered a similar problem.

https://support.google.com/accessibility/android/answer/6151800?hl=en

As stated above, Try using dark theme on your phone and Turn on color inversion on your phone, if it fixes the issue. Then your app doesn't support dark theme. So some devices force it which leads to this behavior.

You can fix this by simply adding the following to your style in themes.xml

 <item name="android:forceDarkAllowed">false</item>
1

You need to check the theme/night as that will show the colours that will be used in dark mode. The theme will only show colours used during normal usage.

ADITYA RANADE
  • 293
  • 3
  • 10