Skip to content
tezvyn:

Create a custom View from scratch that draws a shape

Source: developer.android.comMediumHow cards are made

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's really being asked

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.

The full answer

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.

The mistakes people make

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.

What usually comes next

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.

A 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.

Interview question

What is the correct way to implement onMeasure in a custom Android View that draws a shape?

  • a.Allocate a new Paint object to measure bounds, then set the view's width and height fields directly
  • b.Return the intrinsic size from onMeasure and ignore the widthMeasureSpec and heightMeasureSpec parameters
  • c.Resolve the desired size against the parent's MeasureSpec modes and finish by calling setMeasuredDimensionCorrect
  • d.Perform all size calculations in onDraw and call requestLayout whenever the shape needs to change size
Why?

The card states that onMeasure must resolve dimensions against the parent MeasureSpec and always finish with setMeasuredDimension. Option B is wrong because ignoring MeasureSpec and returning an intrinsic size directly breaks parent layouts like ScrollView or ConstraintLayout.

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