What are the steps to activate dark mode in a React Native WebView?

There's a problem reported in the react native webview regarding dark mode. It says that on iOS, the WebView automatically switches to dark theme when the system appearance is set to dark, but this does not happen on Android. There's also a PR created for this issue, although it hasn't been merged yet. I'm wondering how to enable dark mode in iOS webviews as mentioned. It doesn't switch automatically when changing the system-wide theme.

Is there something I'm overlooking here?

Answer №1

TLDR

To resolve the styling issue, update the parent of AppTheme from Light to DayNight in the main/res/values/styles.xml file.

Details

The styling for your app is managed through XML files located in the main/res/values folder. These files are automatically created by React Native or Expo, with the central file being styles.xml.

Within this file, there is an element named AppTheme:

<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:editTextStyle">@style/ResetEditText</item>
    <item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

By default, the parent property of AppTheme is set to a Light theme. Changing it to DayNight resolved the issue:

<style name="AppTheme" parent="Theme.AppCompat.DayNight.NoActionBar">
    <item name="android:textColor">@android:color/black</item>
    <item name="android:editTextStyle">@style/ResetEditText</item>
    <item name="android:editTextBackground">@drawable/rn_edit_text_material</item>
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
</style>

Sources

I discovered this solution by referencing a similar example on GitHub. For more information on the DayNight parent theme, check out Google's documentation.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

After clicking, I would like the text from the list item to have a style of "background-color: aqua" when displayed in the textarea

Is there a way to apply a highlight with a style of "background-color: aqua" in the textarea only to the word selected from the list above after clicking on it? See below for my HTML: <head> <script src="https://ajax.googleapis.com/ajax/libs/ ...

Issues with spacing in navigation bar

I've been searching and experimenting for hours, but I can't seem to make it work. In my "inline-form," I have a button on the left, an input field in the middle, and a button on the right within a navbar. Everything displays correctly when the ...

What is the best method for positioning two forms in a vertical arrangement?

I'm looking to integrate a Login form in HTML within my C# project. Here's what I have so far: However, as you can see, the layout is not very aesthetically pleasing. I'm struggling to align the elements in a more visually appealing way. My ...

Issue with screen cropping in Unity's 2D game development

I recently created a 2-D game in Unity and encountered some display issues. When running the game on a web player, the screen was cutting off from both ends. I then tried creating an Android APK from it, but the problem persisted on Android devices as well ...

Error: Firebase Database has encountered an OutOfMemoryError in Kotlin/Android

I have encountered an issue while retrieving data from the Realtime Database for Android. Occasionally, a crash occurs with the following message: java.lang.RuntimeException: Firebase Database encountered an OutOfMemoryError. To mitigate this error, you ...

There has been an issue with the JSON data: org.json.JSONException is showing an error indicating the end

My Login code has an issue - when a user registers, it works fine. However, during the login process, I receive the error message "end of input at character 0 of". This is my Login.php code: <?php include "connect.php"; if ($_SERVER['REQUEST_METH ...

A dark view controller appears when transitioning via pushing, but a lighter one when presented mod

Struggling with a UIViewController issue... I have a UINavigationController with a root view controller containing a UITableView. When selecting a cell, I want to push open a new UIViewController. Here's the code snippet: - (void)tableView:(UITabl ...

Steps to display a div on top of a background image

Here is a visual representation of my design for better understanding: I am currently working on developing the central content that overlays the image. However, when I insert divs with background colors into my HTML to test their placement, I do not see ...

Clicking on a custom notification will prompt for unlocking the lock screen

I have implemented a custom notification on the lock screen using a broadcast pending intent to trigger an action in my app when the notification is clicked. However, I am facing an issue where clicking on the notification causes it to disappear from the l ...

How can I eliminate the error message "WARNING: no homepage content found in homepage.md"?

Starting my journey with KSS utilizing Keith Grant's CSS in Depth as a guide. In section 10.1.4, the author suggests: To begin, create a new markdown file named homepage.md inside the css directory. This file will serve as an introduction to the patt ...

In React Native, pause and await the completion of a function before proceeding

My code synchronization is causing issues, as my program is only reacting on the second button press instead of the first one (further details below). This is the Firebase API function I am using: export const noOpponent = (Key, setOpponent) => { con ...

Retrieve incoming call details automatically on iOS7

Is it possible to retrieve incoming call details (such as the number) programmatically on iOS7? I am looking to develop an app that can automatically reject incoming calls and send a text message to the caller's number instead. ...

I am having issues with my CSS file not functioning correctly on my Django template. Does anyone have any suggestions on how to resolve this issue

I've encountered issues with CSS files not working properly on my Django template. Whenever I make changes to the CSS file and reload the browser page, nothing happens. However, if I restart my computer or power off and on again, the changes reflect o ...

When initiating an activity on Android, I am unable to view the camera preview

Update: The rotation issue has been resolved (modified code will be provided in a later answer). However, I am still unable to see the camera preview when starting the activity. I have gone through the following links but they did not help me. Can't c ...

Designing a layout for a chat application that is structured from the bottom up

I'm currently in the process of designing a web application for a chat platform. I have access to an API that provides a list of messages: chatsite.com/api/thread/1/messages/ [ { "id": 2, "sender": { "id": 2, ...

What is the best way to integrate an API into a Flutter app?

Hello! I am in the process of developing a mobile application for iOS and Android using Flutter. The app is running smoothly as it should, but now I need to integrate an ad API into it. Thank you for taking the time to read this and offering your assista ...

What could be the reason for the unexpected outcome when checking if display is equal to "none"?

I have a JavaScript function called "display()". I want to hide my navigation menu on click, but it is not working properly - it just blinks. I am looking for a solution that uses only JavaScript and does not involve querySelector. function display() { ...

Please indicate a width of 3 characters for the HTML input text

Is there a way to create an HTML text box that can only fit 3 characters exactly? I came across this code online: <input type="text" style="width: 10px; padding: 2px; border: 1px solid black"/> This code creates a text box with a width of 10px ...

What is the best way to conceal a div element on a Razor Page depending on the user's input?

How can I display the state field only when the user selects United States in the country field, and hide it otherwise? Here is the code snippet from my cshtml page: <div class="form-group col-sm-4 mt-4"> <label asp-for="Form.Co ...

Ways to place a background image without using the background-position attribute

On the menu of this page (blog.prco.com), I have added a background image for the hover effect on the elements. However, when hovering, the dot seems to move towards the final position from its origin point. My question is, how can I center the image belo ...