tezvyn:

Create a custom View from scratch that draws a shape

AI-drafted, machine-checkedSource: developer.android.comintermediate
Create a custom View from scratch that draws a shape

This tests Android View measurement and drawing contracts. A strong answer covers: onMeasure resolves dimensions against parent MeasureSpec; onDraw renders with Canvas and Paint; constructors parse attributes.

WHAT THIS TESTS: This question probes whether you understand the Android View rendering pipeline beyond surface-level APIs. Interviewers want to see that you know the separation of concerns between measurement and drawing, that you respect parent constraints, and that you avoid performance pitfalls in custom rendering.

A GOOD ANSWER COVERS: A good answer hits four things in order. First, the constructor chain and attribute parsing. You should mention reading custom XML attributes from AttributeSet or TypedArray in the constructor so the view is configurable from XML. Second, onMeasure. Explain that this method resolves your desired size against the parent MeasureSpec. You must handle three modes: EXACTLY means use the spec size, AT_MOST means pick the smaller of your desired size and the spec limit, and UNSPECIFIED means use your intrinsic desired size. Crucially, you must finish onMeasure by calling setMeasuredDimension with the resolved width and height. Third, onDraw. Explain that the system provides a Canvas and you use Paint to draw primitives, paths, or shapes. You should reuse Paint objects rather than creating them here. Fourth, invalidation. Explain that calling invalidate requests a redraw and that requestLayout is needed if the view size changes. Optionally mention onSizeChanged as a useful hook to initialize shape bounds once the final size is known.

COMMON WRONG ANSWERS: Common wrong answers include treating onMeasure as a place to simply set width and height without considering MeasureSpec, which breaks parent layouts like ScrollView or ConstraintLayout. Another red flag is allocating Paint, Bitmap, or Path objects inside onDraw, which causes garbage collection stutter during animation or scrolling. Some candidates also confuse onDraw with onLayout, incorrectly placing child positioning logic inside the draw method. Failing to call setMeasuredDimension is a critical error that will crash or mismeasure the view.

LIKELY FOLLOW-UPS: Interviewers often follow up by asking how you would handle touch input inside the custom shape, which leads to onTouchEvent and hit testing against the path bounds. They may ask how to animate the shape, which tests whether you know to use ObjectAnimator with a custom property setter that calls invalidate. They might also ask about supporting different screen densities, which tests awareness of dp-to-pixel conversion and Canvas scaling.

ONE CONCRETE EXAMPLE: Suppose you are building a circular progress indicator. In the constructor you read a custom color attribute from XML. In onMeasure you calculate the desired size as the padding plus the stroke width, then resolve it against widthMeasureSpec and heightMeasureSpec using resolveSize or a manual switch on MeasureSpec mode, then call setMeasuredDimension. In onSizeChanged you compute the center point and radius based on the final measured width and height. In onDraw you reuse a single Paint instance with anti-aliasing enabled, set its color and stroke width, and draw an arc or circle on the supplied Canvas. When progress updates, a public setter changes the sweep angle and calls invalidate to trigger a fresh onDraw pass.

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.