A layout describes what a screen looks like. Classic Android uses XML files in res/layout; each UI element is a View (TextView, Button, ImageView, EditText...).
ConstraintLayout — the flexible default
<androidx.constraintlayout.widget.ConstraintLayout ...>
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Reading a view from code (View Binding)
Enable View Binding to access views type-safely instead of findViewById.
// build.gradle: buildFeatures { viewBinding = true }
private lateinit var binding: ActivityMainBinding
override fun onCreate(s: Bundle?) {
super.onCreate(s)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.title.text = "Hello"
}
match_parent= fill the parent,wrap_content= only as big as content.- Use
dpfor sizes andspfor text sizes. - Keep strings in
res/values/strings.xmlfor translation.
Common mistake: Deeply nested layouts hurt performance. ConstraintLayout lets you build flat hierarchies — prefer it.
Summary
You can build screens with XML, position views with ConstraintLayout, and read them safely with View Binding.