tezvyn:

What is the res directory and how do you use orientation qualifiers?

Curated by the Tezvyn teamSource: developer.android.combeginner
What is the res directory and how do you use orientation qualifiers?
WHAT IT TESTS

Android's declarative resource system and runtime configuration adaptation.

ANSWER OUTLINE

res/ holds non-code assets; create layout-port/ and layout-land/ with identical XML filenames for automatic framework selection.

WHAT THIS TESTS: This question probes two foundational Android concepts: the resource system and configuration qualifiers. The interviewer wants to know if you trust the framework to handle device variations declaratively rather than writing manual branching code. Seniors should demonstrate awareness that res/ is not just a folder but a runtime resolution engine that maps device state to the correct asset without explicit code intervention.

A GOOD ANSWER COVERS: First, state that the res/ directory contains non-code resources such as layouts, strings, colors, drawables, dimensions, and raw files. These are compiled into the R class and accessed via generated integer IDs. Second, explain that resource qualifiers are directory name suffixes that let the framework choose the best match for the current device configuration. For portrait and landscape, you create res/layout-port/ and res/layout-land/ directories. Third, emphasize that the XML files inside must share identical filenames so that R.layout.main resolves to the correct variant automatically. Fourth, note that this works because a configuration change triggers the framework to reload resources, and if the Activity does not handle configChanges manually in the manifest, it recreates and pulls the new layout.

COMMON WRONG ANSWERS: A major red flag is saying you would detect orientation in onCreate using getResources().getConfiguration().orientation and then inflate different layouts manually. Another red flag is using different filenames like main_port.xml and main_land.xml, which breaks the qualifier abstraction and forces conditional logic in code. Claiming that res/ is only for images, confusing res/ with assets/, or stating that qualifiers require programmatic registration are also serious gaps that reveal a lack of production experience.

LIKELY FOLLOW-UPS: The interviewer may ask what happens when you add android:configChanges="orientation" to the manifest, which prevents Activity recreation and therefore skips automatic resource reloading. They might also ask how to handle orientation for Fragments, or how the framework resolves conflicts when multiple qualifiers match, such as layout-sw600dp-land versus layout-land. A senior candidate should mention that the framework picks the most specific match and falls back to the default if no exact qualifier directory exists.

ONE CONCRETE EXAMPLE: Suppose you have a news detail screen. In res/layout/ you place article_detail.xml with a single column for phones in portrait. In res/layout-land/ you place another article_detail.xml that uses a two-pane layout with the image on the left and text on the right. When the user rotates the device, the Activity is recreated and setContentView(R.layout.article_detail) automatically inflates the landscape version without any conditional code. This declarative approach keeps the Activity free of orientation branching and lets the system handle caching and inflation efficiently.

Source: developer.android.com

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.

What is the res directory and how do you use orientation qualifiers? · Tezvyn