I have an issue in my APP, I have a BottomSheet and I was adding a FAB, the FAB should be always at the top of the BottomSheet so I have added a bottom margin to the FAB, the issue is that once the BottomSheet is expanded the FAB goes anchored to bottomsheet without the initial bottom margin.
How could I achieve the FAB to be always at the top?
Here is my code:
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:padding="2dp"
android:scrollbars="vertical"
tools:listitem="#layout/recyclerview_pterm" />
<include
layout="#layout/bottom_sheet_pterm" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/floatingActionButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="76dp"
android:clickable="true"
android:focusable="true"
app:layout_anchor="#+id/bottomSheet"
app:layout_anchorGravity="top|end"
app:srcCompat="#drawable/ic_covid"
android:contentDescription="#string/verifica_gp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And here is how it looks like:
You can solve that by adding these two attributes to the FAB:
<com.google.android.material.floatingactionbutton.FloatingActionButton
....
app:useCompatPadding="true"
android:layout_gravity="top" />
The android:layout_gravity="top" makes the FAB to be on top of the anchor (i.e. not intersected with it), and app:useCompatPadding="true" Allows the padding between the FAB & The anchor (i.e. bottomSheet).
Sample:
Related
I want to create Button which will move together with BottomSheet. It should be positioned above BottomSheet parent. So I used CoordinatorLayout to achieve this. But if I anchor this Button above BottomSheet layout, its bottom half is cut off (I want to position it above BottomSheet layout like in RelativeLayout). Also if BottomSheet is hidden, its overlapping TabBar. Also Button is for some reason not following BottomSheet. I thought CoordinatorLayout would solve this issue. I dont wanna hand animate Button movement based on BottomSheet peek.
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:orientation="vertical"
tools:ignore="UselessParent">
<ImageButton
android:id="#+id/but_location"
android:layout_width="#dimen/button_height"
android:layout_height="#dimen/button_height"
android:layout_margin="#dimen/padding_medium"
app:layout_anchor="#id/bottomSheetContainer"
app:layout_anchorGravity="end|top"
android:src="#drawable/ic_location_icon"
style="#style/button.shadow" />
<RelativeLayout
android:id="#+id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="#+id/bottomSheetContainer"
layout="#layout/bottom_sheet_container"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_above="#id/tabBar" />
<include
android:id="#+id/tabBar"
layout="#layout/tab_bar_layout"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="66dp" />
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
UI:
I'm using Material FAB and BottomSheet in my Android App and i'm having some issues with the FAB which is overlaying the BottomSheet and i would prevent it.
Actually i have two BottomSheets when the BottomSheet1 is completly expanded i would the FAB to be behind the BottomSheet while when the BottomSheet2 is HalfExpanded i would it to be over the BottomSheet and make it behind it when it's fully expanded.
I've yet tried to make the BottomSheet elevation highter than the FAB one but it's not working any way.
Here is how my code looks like:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/content"
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"
tools:context=".LetturaActivity">
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomAppBar"
style="#style/Widget.MaterialComponents.BottomAppBar.Colored"
android:layout_gravity="bottom"
app:navigationIcon="#drawable/ic_baseline_menu_24"
app:menu="#menu/bottom_app_bar"
app:hideOnScroll="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" \>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fabNuovo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_add"
android:elevation="4dp"
app:tint="#color/white"
android:contentDescription="#string/nuovo_documento"
app:layout_anchor="#id/bottomAppBar"
/>
<include layout="#layout/bottom_sheet_testata" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
And the BottomSheet parent layout is the following:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/bottomSheetTestata"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
app:behavior_hideable="true"
android:elevation="5dp"
android:clickable="false"
android:focusable="false"
app:behavior_peekHeight="0dp"
app:layout_behavior="#string/bottom_sheet_behavior"
android:orientation="vertical">
...
<\LinearLayout>
But it looks like this when BottomSheet is expanded:
Is not overlapping is overlaying, the problem is the elevation.
From the documentation you can see all elements have different elevations.
Fab is 6dp, although the BottomSheet is not easy to find is there, it should be 16dp (you can use your browser functionality for the keyword bottom)
So change your elevation to
android:elevation="16dp"
I am trying to fix this issue where my recyclerview is overlapped by bottomappbar. Here is my layout code .
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/notes_container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:padding="8dp"
android:text="#string/myTasksHeader"
android:textColor="#color/appBarTextColor"
android:textSize="40dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:padding="16dp"
android:scrollbars="none"
tools:listitem="#layout/note_list_item" />
</ScrollView>
</LinearLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:backgroundTint="#color/colorPrimary"
app:hideOnScroll="false"
app:menu="#menu/appbar_menu"
app:navigationIcon="#drawable/ic_menu" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/add_note_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/medium_margin"
android:backgroundTint="#color/colorPrimary"
app:layout_anchor="#id/bottomAppBar"
app:srcCompat="#drawable/ic_add_black_24dp"
tools:ignore="VectorDrawableCompat" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
when I fill my list I cant reach my last item in the recycler view. I tried adding margin but it did not work.
Two ways to achieve this
Hide BottomAppBar layout while scrolling ..
Add following attribute inside BottomAppBar
app:fabAlignmentMode="center"
app:fabCradleMargin="10dp"
app:fabCradleRoundedCornerRadius="10dp"
app:hideOnScroll="true"
If you do not want to hide BottomAppBar then add margin from bottom
in RecyclerView
android:layout_marginBottom="?actionBarSize"
The problem with using margin is that the RecyclerView won't show through the FAB. A better solution than using margin would be to add extra space at the bottom of the RecyclerView using padding.
android:clipToPadding="false"
android:paddingBottom="?actionBarSize"
I have a coordinator layout with a bunch of stuff, and two buttons in the bottom corners. The buttons seem to be floating 1 pixel inside the corner, both horizontally and vertically. The only way to set them to the actual corner, is to actually set layout_marginBottom and layout_marginStart/layout_marginEnd on them to -1
No padding or margin is set anywhere.
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.MaterialComponents.ActionBar">
<com.google.android.material.appbar.MaterialToolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
tools:menu="#menu/map_actions"
app:popupTheme="#style/ThemeOverlay.MaterialComponents.Light" />
</com.google.android.material.appbar.AppBarLayout>
<!-- not the one that causes problems, just included for completeness-->
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:text="#string/action_record"
android:layout_marginBottom="10dp"
style="#style/Widget.Myapp.MaterialComponents.RecordButton"
app:icon="#drawable/ic_action_record_24dp"
android:visibility="gone"
tools:visibility="visible">
</com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.MyApp.BottomStartCorner"
app:fabSize="mini"
app:tint="?attr/colorOnSecondary"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:src="#drawable/ic_people_24dp"
/>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/actn_open_chat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:src="#drawable/ic_chat_24dp"
app:fabSize="mini"
app:shapeAppearanceOverlay="#style/ShapeAppearanceOverlay.MyApp.BottomEndCorner"
app:tint="?attr/colorOnSecondary" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
That's because the fab have default padding defined in its style.
You can check its source code here : material-components-android
You can override the style of fab in the styles.xml
This will help you : material.io
I am using Bottom Sheet from support library 23.4.0. And I want to add a FAB just above the bottom sheet.
<CoordinatorLayout>
<AppBarLayout/>
<android.support.v4.widget.NestedScrollView 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="wrap_content"
android:visibility="gone"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
android:id="#+id/bottom_sheet_main">
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/map_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="92dp"
android:layout_marginRight="#dimen/fab_margin_right"
android:clickable="true"
android:src="#android:drawable/ic_menu_mylocation"
app:backgroundTint="#FFFFFF"
app:elevation="4dp"
app:fabSize="normal"
app:layout_anchor="#id/map_bottom_sheet"
app:layout_anchorGravity="top|end"/>
</CoordinatorLayout>
This results in correct margin when bottom sheet is visibility=gone as seen here 1. But when bottom sheet is expanded the margin is gone as seen in 2