What is a View in Android?
Tier: EssentialDifficulty: Easy
A View is the base class for every UI element on screen, a Button, a TextView, an ImageView, anything the user can see or touch is a View or a subclass of it. It's responsible for its own measuring, drawing, and handling of input events.
Every View goes through the same core lifecycle to get on screen, it is measured through onMeasure(), which figures out how much space it needs given the constraints its parent hands it, positioned through onLayout(), which places it at its final coordinates, and painted through onDraw(), which is where its actual pixels get rendered onto a Canvas.
val button = Button(context).apply {
text = "Submit"
setOnClickListener { submitForm() }
}
ViewGroup is the other half of this system, a View subclass that acts as an invisible container holding other Views and ViewGroups, LinearLayout and ConstraintLayout are both ViewGroups. A screen is ultimately a tree built out of these two kinds of nodes, ViewGroups arranging their children, and Views at the leaves actually rendering content. In Jetpack Compose there is no View class at all for the UI you write yourself, a composable function describes what to draw and Compose's own renderer paints it directly, though a Compose screen can still host a legacy View through AndroidView when it needs to.