Dark theme

Dark theme is available in ahtq.vn10 (API cấp độ 29) and higher. It has many benefits:

Can reduce power usage by a significant amount (depending on the device’sscreen technology).Improves visibility for users with low vision and those who are sensitive sầu tobright light.Makes it easier for anyone khổng lồ use a device in a low-light environment.

Bạn đang xem: Dark theme

Dark theme applies to lớn both the ahtq.vn system UI và apps running on the device.

There are three ways to enable Dark theme in ahtq.vn10 (API level 29) & higher:

Use the system setting (Settings -> Display -> Theme) lớn enable Dark theme.Use the Quiông xã Settings tile lớn switch themes from the notification tray (onceenabled).On Pixel devices, selecting the Battery Saver mode enables Dark theme at thesame time. Other OEMs may or may not tư vấn this behavior.

For instructions on applying a dark theme khổng lồ web-based nội dung using a WebViewcomponent, see Darken website nội dung in WebView.

Support Dark theme in your app

In order khổng lồ support Dark theme, you must phối your app's theme (usually found inres/values/styles.xml) to inherit from a DayNight theme:

You can also use MaterialComponents' darktheming:

This ties the app's main theme lớn the system-controlled night mode flags andgives the tiện ích a default Dark theme (when it is enabled).

Themes và styles

Your themes and styles should avoid hard-coded colors or iconsintended for use under a light theme. You should use themeattributes (preferred) or night-qualified resources instead.

Here are the two most important theme attributes to know about:

?ahtq.vn:attr/textColorPrimary This is a general purpose text color. It isnear-blaông chồng in Light theme và near-Trắng in Dark themes. It contains adisabled state.?attr/colorControlNormal A general-purpose icon color. It contains adisabled state.

We recommend using Material DesignComponents, since its color themingsystem (such as the themeattributes ?attr/colorSurface and ?attr/colorOnSurface) provides easy accesskhổng lồ suitable colors. Of course, you can customize these attributes in your theme.

Change themes in-app

You might want lớn allow users khổng lồ change the app's theme while the tiện ích isrunning. Your app can let the user choose between themes. The recommendedoptions are:

LightDarkSystem mặc định (the recommended mặc định option)

Each of the options bản đồ directly khổng lồ one of the AppCompat.DayNight modes:

To switch the theme, callAppCompatDelegate.setDefaultNightMode().

Note: Starting with AppCompat v1.1.0, setDefaultNightMode() automaticallyrecreates any started activities.

Force Dark

ahtq.vn10 provides Force Dark, a feature for ahtq.vns to lớn quicklyimplement a Dark theme without explicitly setting a DayNight theme, asdescribed above.

Force Dark analyzes each view of your light-themed tiện ích, and applies a dark themeautomatically before it is drawn to lớn the screen. Some ahtq.vns use a mix ofForce Dark và native sầu implementation lớn cut down on the amount of time needed toimplement Dark theme.

Apps must opt-in to lớn Force Dark by setting ahtq.vn:forceDarkAllowed="true" inthe activity's theme. This attribute is phối on all of the system and ahtq.vnXprovided light themes, such as Theme.Material.Light. When you use Force Dark,you should make sure lớn demo your app thoroughly và exclude views as needed.

If your tiện ích uses a dark theme (such as Theme.Material), Force Dark will notbe applied. Similarly, if your app's theme inherits from a DayNight theme,Force Dark will not be applied, due to the automatic theme switching.

Xem thêm: Mẹo Giúp Lông Mi - Cách Để Mi Mọc Lại Nhanh

Disable Force Dark on a view

Force Dark can be controlled on specific views with the ahtq.vn:forceDarkAllowedlayout attribute or withsetForceDarkAllowed().

Web content

For information on using dark themes in web-based content, see Darken webnội dung in WebView. For an example of dark themeapplied, see the WebView test onGitHub.

Best practices

The following sections provide best practices for implementing dark themes.

Notifications & Widgets

For UI surfaces that you display on the device but vì not directly control, itis important lớn make sure that any views that you use reflect the host app’stheme. Two good examples are notifications và launcher widgets.

Notifications

Use the system-provided notification templates (such as MessagingStyle). Thismeans that the system is responsible for ensuring the correct view styling isapplied.

Widgets and custom notification views

For launcher widgets, or if your tiện ích uses custom notification contentviews, it is important to make sure you thử nghiệm the content on both the Light andDark themes.

Common pitfalls to look out for:

Assuming that the background color is always lightHardcoding text colorsSetting a hardcoded background color, while using the mặc định text colorUsing a drawable ibé which is a static color

In all of these cases, use appropriate theme attributes instead of hardcodedcolors.

Launch screens

If your ứng dụng has a custom launch screen, it may need khổng lồ be modified so thatit reflects the selected theme.

Remove any hardcoded colors, for example any background colors pointing may bewhite. Use the ?ahtq.vn:attr/colorBackground theme attribute instead.

cảnh báo that dark-themed ahtq.vn:windowBackground drawables only work on ahtq.vn10.

Configuration changes

When the app’s theme changes (either through the system setting or AppCompat) ittriggers a uiModeconfiguration change. This means that Activities will be automatically recreated.

In some cases you might want an app to lớn handle the configuration change. Forexample, you might want lớn delay a configuration change because a video clip isplaying.

Xem thêm: Tên Nhân Vật Anime - Top Các Hay, Dễ Thương, Lạnh Lùng Đủ Thể

An app can handle the implementation of Dark theme itself by declaring that eachActivity can handle the uiMode configuration change:

When an Activity declares it handles configuration changes, itsonConfigurationChanged()method will be called when there is a theme change.


Chuyên mục: Tin Tức