The ConstraintLayout does not work as expected when used in a Bottom sheet. In this instance a ConstraintLayout contains 2 images comprising the handle and 1 view for the content in the Bottom Sheet. The content view is supposed to be placed below the handle images which is not happening.
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="350dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<ImageView
android:id="@+id/bottom_handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_bottom_sheet_handle"
android:contentDescription="@string/saved_bottomsheet_handle_content_description"
android:elevation="16dp"
android:src="@drawable/ic_save_planet_dark_48dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_elevation_height"
android:background="@color/bottom_sheet_handle_elevation"
android:contentDescription="@string/saved_bottomsheet_handle_content_description"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/bottom_handle" />
<FrameLayout
android:id="@+id/savedContentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/bottom_handle" />
</androidx.constraintlayout.widget.ConstraintLayout>
The Actionbar from the content view is floating behind the handle views.
The handle sits above the content view and Action Bar.
As much as I'd rather use the ConstraintLayout over RelativeLayout, RelativeLayout works here.
<RelativeLayout
android:id="@+id/bottomSheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:elevation="16dp"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<ImageView
android:id="@+id/bottom_handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="@drawable/ic_bottom_sheet_handle"
android:contentDescription="@string/saved_bottomsheet_handle_content_description"
android:elevation="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/ic_save_planet_dark_48dp" />
<ImageView
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_sheet_elevation_height"
android:background="@color/bottom_sheet_handle_elevation"
android:contentDescription="@string/saved_bottomsheet_handle_content_description"
android:layout_alignBottom="@id/bottom_handle"/>
<FrameLayout
android:layout_below="@id/bottom_handle"
android:id="@+id/savedContentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" />
</RelativeLayout>
If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.
ConstraintLayout has dual power of both Relative Layout as well as Linear layout: Set relative positions of views ( like Relative layout ) and also set weights for dynamic UI (which was only possible in Linear Layout). Despite the fact that it's awesome, it fails to serve the purpose with simple UI layouts.
A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).
To convert an existing layout to a constraint layout, follow these steps: Open your layout in Android Studio and click the Design tab at the bottom of the editor window. In the Component Tree window, right-click the layout and click Convert layout to ConstraintLayout.
try by changing this in constraint layout
<FrameLayout
....
android:layout_height="wrap_content"
android:layout_width= "wrap_content"
>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With