XML Views
The legacy, imperative way of building UIs in Android relies on XML layouts and View classes (e.g., TextView, RecyclerView, ConstraintLayout).
Key XML Concepts​
1. View Hierarchies​
XML layouts are structured as trees.
- Avoid deep hierarchies as they negatively impact measurement and drawing performance.
- ConstraintLayout is preferred to flatten the view hierarchy, allowing complex layouts without nesting view groups.
2. ViewBinding​
Replaces findViewById, providing null safety and type safety.
- It generates a binding class for each XML layout.
- Directly links to the views via their IDs, avoiding runtime crashes.
3. RecyclerView​
Essential for displaying lists.
- Avoids memory exhaustion by recycling off-screen
ViewHolderviews. - Requires an
Adapter(creates ViewHolders and binds data) and aLayoutManager(positions items). - Use
ListAdapterwithDiffUtilfor calculating list changes efficiently on a background thread and animating the differences.
4. Custom Views​
You can extend existing views (TextView) or View/ViewGroup directly.
- Override
onMeasure()to dictate the view's size. - Override
onDraw()using aCanvasfor custom graphics. - Extract custom attributes into
attrs.xmlfor XML configuration.
Best Practices​
- Accessibility: Always use
android:contentDescriptionon ImageViews for TalkBack support. UseminHeight="48dp"for touch targets. - Styles and Themes: Do not hardcode colors in XML. Use
?attr/colorPrimaryor@style/MyCustomText. Extract common styles intostyles.xml. - Strings: Never hardcode text strings. Use
@string/my_stringto support localization.
Interview Questions​
Q: What is the difference between View.GONE and View.INVISIBLE?
Answer: View.INVISIBLE hides the view, but it still takes up space in the layout hierarchy. View.GONE completely removes the view from the layout calculations, collapsing the space it previously occupied.
Q: Explain how DiffUtil works.
Answer: DiffUtil is a utility class that calculates the difference between two lists and outputs a list of update operations that converts the first list into the second. It determines exactly which items were added, removed, or changed, drastically improving RecyclerView update performance compared to notifyDataSetChanged().