My goal is to have an app layout that has a fairly constant navigation UI with various content screens.
So my idea was to use the android architecture navigation component and host an FragmentContainerView
for the varying screens (fragments) in the one and only MainActivity
, which provides the navigation UI (DrawerLayout
etc.).
My problem right now is getting edge-to-edge right: The fab in the fragment sits behind the navigation bar, so I guess the property fitsSystemWindows=true
from the parent isn't propagated down to the fragment?
Having searched and read corresponding articles from ian and banes I tried to manually set the fab margins in the Fragment
which did the trick. But know, if a Snackbar
appears the fab gets kicked upwards (good) but it keeps the margin from the inset, so is way to high over the snackbar.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val mab = binding.fab.marginBottom
val mat = binding.fab.marginRight
ViewCompat.setOnApplyWindowInsetsListener(binding.fab) { v, windowInsets ->
val insets = windowInsets.getInsets(WindowInsetsCompat.Type.systemBars())
v.updateLayoutParams<ViewGroup.MarginLayoutParams> {
bottomMargin = insets.bottom +mab
rightMargin = insets.right + mat
}
windowInsets
}
So my questions is if for the layout I described the following xml is appropriate and if so, how can I make the fab respect the windowInsets
while keeping the default space to the snackbar? Note - there is a smiliar issue but not really solved (the marked solution has the same flaw as the author noticed later) here.
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:openDrawer="start"
android:fitsSystemWindows="true">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.AppBarLayout
android:id="@+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.MaterialToolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
tools:visibility="visible"
app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="@menu/activity_main_drawer_menu"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>
The first fragment would be structured like so:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/large_text" />
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="16dp"
android:clickable="true"
android:src="@drawable/ic_add_24" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>