Layouts & Views (XML)

June 02, 2026 1 min read

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 dp for sizes and sp for text sizes.
  • Keep strings in res/values/strings.xml for 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.