Error Handling & UX
Model UI states with sealed classes (Loading/Success/Error). Show ProgressBar, Snackbar with Retry actions, and inline TextInputLayout validation errors.
Error Handling & UX is a free Android Academy lesson on CoddyKit. This is lesson 3 of 6. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 6 lessons in total.
Why Error Handling Matters
Apps that crash or freeze without explanation feel broken. Users don't know what happened or what to do next.
Good error handling means:
- The app stays stable
- Users get a clear message
- They can retry or take action
This is the difference between a 2-star and a 5-star app review.
Modeling UI State
A clean pattern: model all possible UI states with a sealed class:
Loading— request is in progressSuccess(data)— data is availableError(message)— something went wrong
The ViewModel emits one of these states; the UI renders it.
Sealed Class UiState
Define a sealed class for all possible states:
sealed class UiState<out T> {
object Loading : UiState<Nothing>()
data class Success<T>(val data: T) : UiState<T>()
data class Error(val message: String) : UiState<Nothing>()
}
// In ViewModel:
private val _state = MutableLiveData<UiState<List<Post>>>()
val state: LiveData<UiState<List<Post>>> = _state
fun loadPosts() {
_state.value = UiState.Loading
viewModelScope.launch {
try {
val posts = withContext(Dispatchers.IO) { repo.getPosts() }
_state.value = UiState.Success(posts)
} catch (e: Exception) {
_state.value = UiState.Error(e.message ?: "Unknown error")
}
}
}Rendering State in the UI
Observe the state and show the right view for each case:
viewModel.state.observe(this) { state ->
when (state) {
is UiState.Loading -> {
binding.progressBar.visibility = View.VISIBLE
binding.recyclerView.visibility = View.GONE
binding.tvError.visibility = View.GONE
}
is UiState.Success -> {
binding.progressBar.visibility = View.GONE
binding.recyclerView.visibility = View.VISIBLE
adapter.submitList(state.data)
}
is UiState.Error -> {
binding.progressBar.visibility = View.GONE
binding.tvError.text = state.message
binding.tvError.visibility = View.VISIBLE
}
}
}ProgressBar for Loading
Show a ProgressBar while waiting for data:
- Use
android:indeterminate="true"for a spinning loader - Toggle visibility between
View.VISIBLEandView.GONE GONEremoves the view from layout (not just invisible — it doesn't take up space)
Snackbar vs Toast
Prefer Snackbar over Toast for error messages:
- Snackbar can have an action button (e.g. "Retry")
- It's attached to the current screen — doesn't float over other apps
- Dismisses automatically or on tap
Snackbar with Retry
Show an error with a Retry action button:
import com.google.android.material.snackbar.Snackbar
fun showError(message: String) {
Snackbar.make(
binding.root, // root view of the screen
message,
Snackbar.LENGTH_INDEFINITE // stays until dismissed
).setAction("Retry") {
viewModel.loadPosts() // retry the request
}.show()
}Input Validation
Validate user input before sending it to the server:
- Use
TextInputLayoutwitherrorproperty to show inline errors below the field - Check empty fields, email format, password length before calling the API
- Clear errors when the user starts typing
TextInputLayout Error
Show inline validation errors on form fields:
fun validateAndSubmit() {
val email = binding.etEmail.text.toString().trim()
val password = binding.etPassword.text.toString()
var hasError = false
if (email.isBlank() || !email.contains("@")) {
binding.tilEmail.error = "Enter a valid email"
hasError = true
} else {
binding.tilEmail.error = null // clear error
}
if (password.length < 6) {
binding.tilPassword.error = "Minimum 6 characters"
hasError = true
} else {
binding.tilPassword.error = null
}
if (!hasError) viewModel.login(email, password)
}Quick Check
Which view visibility state removes a view from the layout completely (takes no space)?
Recap: Error Handling & UX
Your apps can now handle errors gracefully:
- Model state as Loading / Success / Error with a sealed class
- Show a ProgressBar during loading
- Snackbar with Retry action for network errors
- TextInputLayout inline errors for form validation
View.GONEvsView.INVISIBLE— remove vs hide
Final lesson: publish your app to the Play Store!
Learn Kotlin with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 36
- Lessons
- 152
Frequently Asked Questions
Is the “Error Handling & UX” lesson free?
Yes — any 3 lessons of the Android Academy learning path, including “Error Handling & UX”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 6 lessons in total.
What will I learn in “Error Handling & UX”?
Model UI states with sealed classes (Loading/Success/Error). Show ProgressBar, Snackbar with Retry actions, and inline TextInputLayout validation errors. You practise Android Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Android Academy?
No prior experience is required. Android Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 6.
How long does the “Error Handling & UX” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Android Academy lesson?
Yes. Every Android Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Retrofit & REST APIs
- Loading Images with Coil
- Error Handling & UX
- Push Notifications
- WorkManager & Background Tasks
- Publishing to Play Store