Layouts and Views
Views and layouts
A View is a single UI element (text, button, image). A layout is a container that arranges views. You describe them in XML files under res/layout/.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!" />
<Button
android:id="@+id/loginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log in" />
</LinearLayout>
Reacting to a click
findViewById<Button>(R.id.loginBtn).setOnClickListener {
Toast.makeText(this, "Clicked!", Toast.LENGTH_SHORT).show()
}
Common layouts
- LinearLayout — stack views in a row or column.
- ConstraintLayout — position views relative to each other (best for complex screens).
- RecyclerView — show long, scrolling lists efficiently.