Skip to content
tezvyn:

Android Style vs. Theme and Attribute Resolution

Source: developer.android.comMediumHow cards are made

Android Style vs. Theme and Attribute Resolution

Tests your grasp of Android's resource scope and resolution timing. A Style targets a single View, while a Theme applies to a whole Context. @color/ is a direct, compile-time link; ?attr/ is an indirect pointer resolved against the Theme at runtime.

What's really being asked

This question assesses your understanding of resource indirection, a core principle for building scalable and maintainable Android UIs. It separates candidates who can only apply static resources from those who understand how to build dynamic, themeable apps. The interviewer is testing if you know the difference between compile-time and runtime resource resolution and the practical implications of each, such as implementing a dark mode.

The full answer

First, define a Style as a collection of attributes applied to a single View. It's for localizing a look, like for a specific type of button. Second, define a Theme as a style applied to a Context (like an Activity or Application), making its attributes available to the entire View hierarchy within that context. A Theme provides global defaults. Third, explain that a direct reference like @color/purple_500 is resolved at compile time by AAPT2 into a static integer ID pointing to a specific hex value. Finally, explain that an attribute reference like ?attr/colorPrimary is an indirect pointer. At runtime, when a View is inflated, the system queries the current Context's Theme to resolve this attribute into a concrete resource, allowing the value to change based on the active theme.

The mistakes people make

Saying Styles and Themes are the same or interchangeable. While related, their scope and application are distinct. The most significant red flag is stating that ?attr/ is resolved at compile time. This indicates a fundamental misunderstanding of the entire theming mechanism. Another mistake is describing a Theme as just a 'big style' without mentioning its critical link to a Context and the View hierarchy. A weak answer also fails to explain why runtime resolution is powerful, missing the connection to features like light/dark modes or product-specific branding.

What usually comes next

Expect questions like: "How would you define and use a custom theme attribute?" (Answer: Declare <attr> in attrs.xml, define it in your <style name="Theme.App">, and use it with ?attr/myCustomAttr in layouts). Or, "What is a theme overlay and when is it used?" (Answer: Applying android:theme to a ViewGroup to style a specific part of the UI differently, like a dark-themed dialog in a light-themed app).

A concrete example

Imagine a styles.xml file where colorPrimary is defined differently for a light and dark theme: <style name="AppTheme.Light"><item name="colorPrimary">@color/blue_500</item></style> and <style name="AppTheme.Dark"><item name="colorPrimary">@color/blue_200</item></style>. A layout file contains <Button android:background="?attr/colorPrimary" ... />. If the Activity's theme is AppTheme.Light, the button's background resolves to @color/blue_500. If the system switches to night mode and the Activity's theme becomes AppTheme.Dark, the same button's background resolves to @color/blue_200 without any code changes, because the ?attr/ lookup happens at runtime against the new theme.

Interview question

To implement a dynamic light/dark mode, how should you set a component's color to ensure it updates automatically when the device's theme changes?

  • a.Set the color using a theme attribute reference, like "?attr/colorPrimary".Correct
  • b.Programmatically check the current mode in the Activity and set the color in code.
  • c.Set the color using a direct resource link, like "@color/brand_purple".
  • d.Apply a specific style directly to the view, like "style=@style/MyWidget.Dark".
Why?

Using a theme attribute (`?attr/`) creates an indirect reference that is resolved at runtime against the currently active theme, allowing the UI to update automatically. A direct color reference (`@color/`) is resolved at compile time and will not change with the theme.

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

Read the original → developer.android.com

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

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