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 — lesson 3 of 6. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, one of 6 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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!
Frequently asked questions
Is the “Error Handling & UX” lesson free?
Yes — the full text of “Error Handling & UX” is free to read here on the web, and the Android Academy course includes 6 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Android Academy course, upgrade to CoddyKit PRO.
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; this is — lesson 3 of 6, so you can start here or from the beginning and move at your own pace.
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.