惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
G
Google Developers Blog
S
SegmentFault 最新的问题
Microsoft Security Blog
Microsoft Security Blog
J
Java Code Geeks
罗磊的独立博客
H
Hackread – Cybersecurity News, Data Breaches, AI and More
量子位
P
Proofpoint News Feed
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
L
LangChain Blog
F
Fortinet All Blogs
C
Check Point Blog
博客园_首页
I
InfoQ
Jina AI
Jina AI
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
Engineering at Meta
Engineering at Meta
美团技术团队
Vercel News
Vercel News
Apple Machine Learning Research
Apple Machine Learning Research

Danb Blog

August 2026: What I've Been Working On · Danb Blog Great Tech: Intel 2500k CPU · Danb Blog July 2026: What I've Been Working On · Danb Blog My Experience with Ubuntu Desktop 26.04 · Danb Blog Thumbnails for Orca 3MF in GNOME Files · Danb Blog Cheaper Sodastream Gas with a Big Tank · Danb Blog June 2026: What I've Been Working On · Danb Blog Running Snyk On Forgejo/Codeberg Actions · Danb Blog Tuta & Proton: An Open Source Client Does Not Result in an Open Source Service · Danb Blog May 2026: What I've Been Working On · Danb Blog 3D Printing Parts for my Greenhouse, Round 2 · Danb Blog Great Devices: Nexus 7 2013 · Danb Blog R36S Console RetroArch Stuck Menu Issue · Danb Blog April 2026: What I've Been Working On · Danb Blog Basic OpenCode Sandboxing with Docker · Danb Blog Games Played in 2025 · Danb Blog March 2026: What I've Been Working On · Danb Blog Shivam Mathur's "node-docker" Images are Awesome for PHP CI · Danb Blog February 2026: What I've Been Working On · Danb Blog Your BookStackApp project was assigned as a project for my Software Architecture course · Danb Blog My Sovol SV08 3D Printer Setup in 2026 · Danb Blog January 2026: What I've Been Working On · Danb Blog Epson Printer: The administrator password you entered was not recognized · Danb Blog Pressure to Follow Process · Danb Blog Online Shopping Email Notifications · Danb Blog My HomeLab Setup in 2026 · Danb Blog Linux Label Printing with the Phomemo D30 · Danb Blog Reporting to the CMA: Google Android Developer Verification · Danb Blog OPNsense & Dnsmasq: Responding with specific DNS servers · Danb Blog An Impromptu Introduction to ZFS Drive Replacement at 1am · Danb Blog
Material You (Dynamic/Adaptive) Colors in Legacy Android ...
2024-03-05 · via Danb Blog

Skip to implementation

Context

So recently I decided to create a much needed update to one of my apps that I’ve left neglected, with the last update being deployed over a decade ago in 2013. This was one of my first coding projects so it was fun to revisit it, while feeling good to actually provide some maintenance for its thousands of loyal remaining users. Time had not been kind to the app, with it asking for notification preferences & full-screen mode despite those not ever being needed, as modern Android had a tricky time understand the app’s requirements.

As part of this update, I thought I’d refresh the design, moving to a new “Material You” kind of look, which allows the app to be themed dynamically based upon user preference (and often things like wallpaper colors).

Unfortunately implementing this kind of design in my app wasn’t so straightforward. What made this difficult is that I was working down to API (Supported Android versions) levels below that where this dynamic color system was introduced (I was working down to API level 24, or Android 7 Nougat). Additionally my app, being a decade old, is a “legacy” Java application, not a fancy Kotlin, Flutter or “Jetpack Compose” app. The documentation for things like colors are not only split across design theory and technical implementation, but now also across these different app build styles, which makes finding specific guidance quite a bit trickier than I remember from my early developer days. I could find bits & pieces, but I wasn’t able to connect it up for actual working code.

It likely I’ve since lost a lot of context/understand to transform the more general documentation to code. Looking back at some of the documentation, it’s now a fair bit more obvious.

Eventually though I figured how to use dynamic colors in a pleasant way, for my old Java-based app, with easy fall-backs for old Android versions:

Implementation

Here’s what I did:

First up is to follow the “Getting Started” page here to use Material Components for Android.

I then used the Material Theme Builder to create & generate out a base theme. You have to use the “Custom” tab to be able to export. I exported as “Android Views (XML)” This export included two files:

Note, I am extending a Material 3 theme which can be seen here. This may be required/important for this kind of setup, I’m not sure.

I then added this to the onCreate methods of my activities:

DynamicColors.applyToActivityIfAvailable(this);

With that done, default widgets (styled from the material theme I’m extending) will now use dynamic colors! To specifically apply colors manually in layouts/styles, you can reference these via an ?attr/<name> value, with the names aligning with the items added to you styles.xml file. Here’s an example of a background and text color:

<Button
    android:backgroundTint="?attr/colorTertiaryContainer"
    android:textColor="?attr/colorOnTertiaryContainer" />

The names align with the color roles defined as part of the Material design documentation. If it helps to have a reference, the app I applied this to can be found open source here. I figured out some of this color-specific code stuff from the Material Components Android Color Section