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

推荐订阅源

博客园 - 三生石上(FineUI控件)
Y
Y Combinator Blog
aimingoo的专栏
aimingoo的专栏
B
Blog
小众软件
小众软件
D
DataBreaches.Net
T
The Blog of Author Tim Ferriss
L
LangChain Blog
WordPress大学
WordPress大学
罗磊的独立博客
GbyAI
GbyAI
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
月光博客
月光博客
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
雷峰网
雷峰网
美团技术团队
F
Fortinet All Blogs
Hugging Face - Blog
Hugging Face - Blog
The GitHub Blog
The GitHub Blog

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