Android Development Android UI and Layouts 2 — Questions and Answers
Question 1: What class is used to implement custom drawing on a Canvas in Android?
- Custom View extending View (Correct answer)
- SurfaceView only
- ImageView subclass
- DrawableView
Correct answer: Custom View extending View
You create a custom view by extending the View class and overriding the onDraw(Canvas) method to perform custom drawing.
Question 2: Which attribute controls the visibility of a view in Android XML?
- android:visibility (Correct answer)
- android:display
- android:show
- android:hidden
Correct answer: android:visibility
The android:visibility attribute accepts VISIBLE, INVISIBLE, or GONE to control whether a view is shown or takes up space.
Question 3: What is the purpose of the include tag in Android XML layouts?
- To reuse a layout file inside another layout (Correct answer)
- To include Java code in XML
- To import external resources
- To embed a WebView
Correct answer: To reuse a layout file inside another layout
The <include> tag allows you to embed another layout XML file inside the current layout, promoting reusability.
Question 4: Which method is called when the size of a view changes?
- onSizeChanged (Correct answer)
- onLayout
- onMeasure
- onDraw
Correct answer: onSizeChanged
onSizeChanged is called when the size of a view changes, providing the new width, height, and old values.
Question 5: What does the dp unit represent in Android?
- Density-independent pixels (Correct answer)
- Device pixels
- Drawable pixels
- Default pixels
Correct answer: Density-independent pixels
dp (density-independent pixels) is a virtual unit that scales based on screen density to ensure consistent physical size across devices.
Question 6: Which component in Material Design is used to display brief, non-disruptive messages at the bottom of the screen?
- Snackbar (Correct answer)
- Toast
- Dialog
- AlertDialog
Correct answer: Snackbar
Snackbar provides brief feedback about an operation at the bottom of the screen and can include an optional action.
What class is used to implement custom drawing on a Canvas in Android?