Skip to content
tezvyn:
Menu

Difference between Style and Theme, and how ?attr/ resolves vs @color/

Source: developer.android.comMediumHow cards are made

Difference between Style and Theme, and how ?attr/ resolves vs @color/

Probes Android resource indirection depth. A style targets one View; a theme targets a Context. ?attr/ resolves dynamically against the current theme at runtime, but @color/ is a static compile-time constant. Red flag: saying both resolve the same way.

What's really being asked

Deep understanding of the Android resource framework and the distinction between static resource references and dynamic theme attributes. Interviewers want to know if you understand that themes are scopes applied to Contexts while styles are attribute bundles applied to Views, and that attribute references enable design systems and runtime theme switching.

The full answer

Four things in order. First, a Style is a collection of view attributes applied to a single View or ViewGroup, while a Theme is a style applied to an Activity, Application, or Context that defines default values for attributes across an entire window or app. Second, a direct reference like at_color slash purple_500 is compiled to a constant integer resource ID and points to exactly one resource with no indirection. Third, an attribute reference like question_mark attr slash colorPrimary is resolved at runtime by querying the current Context's Theme; the system walks the theme's style hierarchy until it finds a value for that attribute, then resolves the concrete resource it points to. Fourth, this means question_mark attr values change based on the active theme, supporting light slash dark modes and brand customization, while at_color references are immutable at compile time.

The mistakes people make

Claiming styles and themes are identical except for naming. Saying question_mark attr references are resolved at compile time or are just aliases. Asserting that attribute resolution is prohibitively expensive without context. Failing to mention that themes live on Contexts and styles live on Views. Confusing app-defined attributes with framework attributes from the android namespace.

What usually comes next

How would you define and resolve a custom attribute for a custom view? What happens if the current theme does not define the requested attribute? How does applyStyle differ from setTheme on a Context? How does Material You dynamic color hook into attribute resolution? Can you override a theme attribute for a single View subtree?

A concrete example

Imagine two app flavors using identical layout XML. A TextView background set to at_color slash purple_500 displays the same purple in both flavors. A TextView background set to question_mark attr slash colorPrimary displays purple_500 in Flavor A because its theme assigns that color to colorPrimary, but displays teal_700 in Flavor B because its theme overrides the same attribute. The layout XML never changes; only the Context's Theme changes.

Interview question

Why can two app flavors share the same layout XML yet render different colors for the same view?

  • a.@color/ references are resolved at runtime against the current theme, allowing per-flavor overrides.
  • b.?attr/ resolves dynamically against the Context's theme, so each flavor's theme can supply a different concrete color.Correct
  • c.?attr/ is a compile-time alias that points to different resources in each build flavor.
  • d.Styles apply to Contexts and themes apply to Views, reversing their normal scopes.
Why?

?attr/ is resolved at runtime by querying the Context's theme, enabling different colors per flavor without changing layout XML. Option D is tempting because it correctly names styles and themes but dangerously reverses their scopes, which is a common misconception.

Just read this? Test yourself on what you have been reading.

Read the original → developer.android.com

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on android — each one lists the topics its interview covers.

See open roles