Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Activity layout jumping behind statusbar, when updated from Fragment

Problem

I have an activity, where my user i as able to edit different values for an entity. I have created a BottomSheetDialogFragment, where the user is able to change some time values for said entity.

When the user changes a value (here it's a time) i want to change the value the activity is showing in the background as well. However, when i update my activity view, the entire activity jumps behind the Statusbar like this (i know there is an inconsistency between the number picked and the number shown, it's a timezone thing i haven't fixed yet).

Code

I open the BottomSheetDialogFragment with:

TimePickerDialogFrag.newInstance(shift!!, type).run {
        window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
        show(supportFragmentManager, "timepicker_modal")
    }

The BottomSheetDialogFragment communicates to the activity through a delegate pattern, defined by an interface called OnTimeChangedListener as so:

override fun onAttach(context: Context) {
    super.onAttach(context)
    if (context is OnTimeChangedListener) {
        listener = context
    } else {
        throw RuntimeException(context.toString() + " must implement OnTimeChangedListener")
    }
}

override fun onDetach() {
    super.onDetach()
    listener = null
}

interface OnTimeChangedListener {
    fun onTimeChanged(shift: ShiftEntity)
}

The onTimeChanged method is called when the user spins the TimePicker widget.

The method is implemented in the activity as:

override fun onTimeChanged(shift: ShiftEntity) {
    this.shift = shift
    startTimeTextView?.text = TimeFormatter.formatTime(shift.startTime, "HH:mm")
    endTimeTextView?.text = TimeFormatter.formatTime(shift.endTime, "HH:mm")
}

I'd guess the solution is somewhere in the Activitys layout file, which looks like this:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:focusableInTouchMode="true"
    tools:context=".activities.myshifts.ShiftActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            style="@style/myNavDrawer"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:background="@color/primaryBg"
            android:contentInsetEnd="0dp"
            android:contentInsetLeft="0dp"
            android:contentInsetRight="0dp"
            android:contentInsetStart="0dp"
            app:contentInsetEnd="0dp"
            app:contentInsetEndWithActions="0dp"
            app:contentInsetLeft="0dp"
            app:contentInsetRight="0dp"
            app:contentInsetStart="0dp"
            app:contentInsetStartWithNavigation="0dp"
            app:popupTheme="@style/AppTheme"
            app:theme="@style/ToolbarColoredBackArrow">

            <android.support.constraint.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                //Content of custom AppBar goes here. Removed for an easier overview


            </android.support.constraint.ConstraintLayout>


        </android.support.v7.widget.Toolbar>

    </android.support.design.widget.AppBarLayout>
    
//Actual content of activity goes here. Removed it for an easier 
overview, will add if needed.

</android.support.constraint.ConstraintLayout>

Already tried

I have already tried using the android:fitsSystemWindows="true" attribute - adding it to both the parent view and the AppBarLayout.

Adding it to the parent layout creates a padding inside of the AppBar, as soon as the Fragment opens, like seen in the top here

Adding the attribute to the AppBarLayout does not make a difference.

like image 802
Lars Avatar asked Sep 10 '18 00:09

Lars


2 Answers

Try adding this attribute:

android:fitsSystemWindows="true"

In BottomSheetDialogFragment root of xml that you inflate or in your FrameLayout that contain your Fragment.

like image 103
Daniel Carreto Avatar answered Oct 20 '22 09:10

Daniel Carreto


I found the solution. I did not realise the way i inflated the BottomSheetDialogFragment would have an effect and therefore i initially didn't include that code in the question.

However removing window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS) from the Fragment did the trick.

like image 40
Lars Avatar answered Oct 20 '22 07:10

Lars