0

I'm using edge-to-edge in my app. I want to add info window (ViewStub) at the top of the screen. Also i need to apply margin to this window to avoid it drawing on system top panel. I'm using this code:

fun View.applyTopInsetAsMargin() {
    ViewCompat.setOnApplyWindowInsetsListener(this) { view, windowInsets ->
        val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
        view.updateLayoutParams<ViewGroup.MarginLayoutParams> {
            topMargin = insets.top
        }
        windowInsets
    }
}

It's working fine but it's overriding margin to just inset size. In my viewstub i'm setting topMagin to 8 dp because i need to lay it down from status bar and add extra 8 dp margin.

The problem is: when i'm using this extension my 8 dp margin getting overrided so viewstub is only getting top inset size. How can i presave my extra margin and apply it to my view?

val popupBinding = DemoPopupWindowBinding.bind(binding.demoPopupStub.inflate())
popupBinding.root.applyTopInsetAsMargin()

<ViewStub
        android:id="@+id/demo_popup_stub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="@dimen/horizontal_margin_16"
        android:layout_marginTop="@dimen/vertical_margin_8"
        android:layout_marginEnd="@dimen/horizontal_margin_16"
        android:inflatedId="@+id/demo_popup_window"
        android:layout="@layout/demo_popup_window" />

viewstub layout:

<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="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/demo_scheme_popup_background"
    android:padding="@dimen/padding_8">
    ...
Danteee313
  • 11
  • 1

0 Answers0