Why Learn XML Views?
Many professional apps still contain XML screens. You will meet them in jobs, so you should be able to read and fix them, even if you build new screens with Compose.
An XML Layout
xml
<!-- res/layout/activity_main.xml -->
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp">
<TextView
android:id="@+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="@+id/clickButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Tap me"
app:layout_constraintTop_toBottomOf="@id/titleText"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
| Layout | Use |
LinearLayout | Items in one row or column |
ConstraintLayout | Flexible, flat layouts with constraints |
FrameLayout | Stack views on top of each other |
RecyclerView | Long lists |
ViewBinding
ViewBinding generates a class for each layout, so you no longer use findViewById.
kotlin
// build.gradle.kts
android { buildFeatures { viewBinding = true } }
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.titleText.text = "Hello XML"
binding.clickButton.setOnClickListener {
binding.titleText.text = "Button clicked"
}
}
}
Fragments
A Fragment is a reusable part of a screen with its own lifecycle. Older single-screen-per-Activity apps use fragments heavily.
kotlin
class ProfileFragment : Fragment(R.layout.fragment_profile) {
private var _binding: FragmentProfileBinding? = null
private val binding get() = _binding!!
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
_binding = FragmentProfileBinding.bind(view)
binding.saveButton.setOnClickListener { /* ... */ }
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.uiState.collect { state -> binding.nameText.text = state.name }
}
}
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null // avoid leaking the view hierarchy
}
}
| Fragment callback | Meaning |
onCreate | Fragment created (no view yet) |
onCreateView / onViewCreated | Create and set up the views |
onStart, onResume | Visible, then interactive |
onDestroyView | View destroyed: clear the binding |
onDestroy | Fragment finished |
Memory leak warning: A Fragment can outlive its view. Always set the binding to null in onDestroyView, and use viewLifecycleOwner for observers.
Compare: The same screen in Compose is about a third of the code and has no binding, findViewById or fragment lifecycle problems. This is why new screens use Compose.
Conclusion
XML layouts with ViewBinding and Fragments are still common. Learn to read them, and prefer Compose for new work.
Quick Summary: Describe UI in XML, access it with ViewBinding, keep the binding null-safe in Fragments and use viewLifecycleOwner.