'Status Bar sets my Constraint Layout dimensions to 0
I'm having a very strange case. In my application, depending on the fragment I want to open, I show or hide the Status Bar (by status bar I mean the system bar, the one with the time).
The problem comes when I do the action of showing it, and if I'm in the main fragment of my Bottom Navigation and I navigate to another fragment where it should be shown, this fragment is not shown, because its container is assigned 0 dp despite being in match_parent.
The view of one of the fragments that is giving me problems is as follows:
<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:background="@android:color/white">
<FrameLayout
android:id="@+id/fragment_menu__container__search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/blue_horizontal_gradient"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/fragment_menu__input__search"
style="@style/fontFamilyCustom_regular"
android:textColor="@color/black_text"
android:textColorHint="@color/gray_hours_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/twelve"
android:layout_marginTop="@dimen/twelve"
android:layout_marginEnd="@dimen/twelve"
android:layout_marginBottom="@dimen/twelve"
android:hint="@string/menu__label__search"
android:paddingStart="@dimen/thirty_two"
android:imeOptions="actionSearch"
android:inputType="text"
android:background="@drawable/bg_rectangle_round_corners"
android:textSize="@dimen/text_medium"
android:paddingTop="@dimen/eight"
android:paddingBottom="@dimen/eight"/>
<androidx.appcompat.widget.AppCompatImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="@dimen/twenty"
app:srcCompat="@drawable/ic_search" />
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/fragment_menu__rv__items_menu"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/fragment_menu__container__search" />
<ProgressBar
android:theme="@style/CustomProgressBar"
android:id="@+id/fragment_menu__pb__loading"
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" />
</androidx.constraintlayout.widget.ConstraintLayout>
The methods used to show or hide the Status Bar are as follows:
fun hideStatusBar() {
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
window.statusBarColor = Color.TRANSPARENT
window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
window?.decorView?.requestApplyInsets()
}
fun setStatusBar(window: Window) {
window.decorView?.systemUiVisibility =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
} else {
View.SYSTEM_UI_FLAG_VISIBLE
}
window.decorView.requestApplyInsets()
}
And the container where I load them is located in the MainActivity in a FrameLayout:
<FrameLayout
android:id="@+id/activity_main__container__content"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@id/activity_main__view__ads_banner"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/activity_main__toolbar__component"/>
The thing is that if I am in the foreground with the application where the fragment is not being displayed, I switch to the background and return to the foreground, the fragment is displayed with the size that the match_parent assigns to it.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|

