Views and XML: Layouts, ViewBinding and Fragments

Android Development 13 min min read Updated: Nov 16, 2026 Intermediate
Views and XML: Layouts, ViewBinding and Fragments
Intermediate Topic 1 of 4

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>
LayoutUse
LinearLayoutItems in one row or column
ConstraintLayoutFlexible, flat layouts with constraints
FrameLayoutStack views on top of each other
RecyclerViewLong 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 callbackMeaning
onCreateFragment created (no view yet)
onCreateView / onViewCreatedCreate and set up the views
onStart, onResumeVisible, then interactive
onDestroyViewView destroyed: clear the binding
onDestroyFragment 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.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators