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 THIS TESTS: 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.
A GOOD ANSWER COVERS: 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.
COMMON WRONG ANSWERS: 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.
LIKELY FOLLOW-UPS: 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?
ONE 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.
Read the original → developer.android.com
Get five bites like this every day.
Tezvyn delivers a daily feed of 60-second tech bites with quizzes to lock in what you learn.