I'm facing a problem with the visibility of an Android ProgressBar in my app. The ProgressBar is supposed to be shown and hidden based on changes in a LiveData observed in my BaseActivity. I'm using ViewBinding and LiveData to manage the UI state.
<androidx.constraintlayout.widget.Group
android:id="@+id/in_progress_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
app:constraint_referenced_ids="progress_bar,gray_overlay" />
<View
android:id="@+id/gray_overlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black_alpha2"
android:clickable="true"
android:focusable="true" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
override fun onStart() {
super.onStart()
progressViewModel.getProgressLiveData()
.observe(this, androidx.lifecycle.Observer {
when (it) {
ProgressState.STARTED -> {
Log.e("ProgressState", "ProgressState.STARTED")
binding.inProgressGroup.visibility = View.VISIBLE
}
ProgressState.IN_PROGRESS -> {
Log.e("ProgressState", "ProgressState.IN_PROGRESS")
binding.inProgressGroup.visibility = View.VISIBLE
}
ProgressState.ENDED -> {
Log.e("ProgressState", "ProgressState.ENDED")
binding.inProgressGroup.visibility = View.INVISIBLE
}
else -> {
binding.inProgressGroup.visibility = View.GONE
}
}
})
}
Even though I can see the correct log messages indicating that the progress has started and ended at the right times, the ProgressBar itself is not behaving as expected. It only shows during the splash screen and not when explicitly called.