Serdar17
Serdar17

Reputation: 1

How to place the view in the center of the bottom sheet fragment?

Is there a way to position the view in the center of BottonSheetFragment. I have a simple LinearLayout where the name and price located. Under these field i have one more LinearLayout with an error state. I need the error state to always be beetween the price and the bottom system bar, regardless of the state of the BottomSheetFragment.

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">
        
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
        
            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_margin="@dimen/md_theme_indent_2x">
        
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="Product name"
                    android:layout_marginVertical="@dimen/md_theme_indent_1x_half"
                    android:textAppearance="?attr/textAppearanceTitleLarge"/>
        
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="12.00 $"
                    android:textColor="?attr/colorOnSurfaceVariant"
                    android:textAppearance="?attr/textAppearanceBodyMedium"/>
        
            </LinearLayout>
            
            <LinearLayout
                android:id="@+id/error_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:orientation="vertical">
    
                <ImageView
                    android:layout_width="44dp"
                    android:layout_height="44dp"
                    android:src="@drawable/info_24"
                    android:tint="?attr/colorOnSurfaceVariant"/>
    
                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/md_theme_indent_2x"
                    android:text="Error title"
                    android:gravity="center"
                    android:textAppearance="?attr/textAppearanceTitleMedium"
                    android:textColor="?attr/colorOnSurfaceVariant"/>

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/md_theme_indent_1x"
                    android:text="Error subtitle"
                    android:textAppearance="?attr/textAppearanceBodyMedium"
                    android:textColor="?attr/colorOnSurfaceVariant"
                    android:gravity="center"/>
            </LinearLayout>
        </LinearLayout>
    </androidx.core.widget.NestedScrollView>
</LinearLayout>

I found a solution with dynamic calculation height of error_state inside BottomSheetCallback.OnSlide method. But after dragging the bottom sheet for the first time, the error_state jump occurs.

public class BottomSheetCallback(LinearLayout errorView) : BottomSheetBehavior.BottomSheetCallback
{
    public override void OnSlide(View bottomSheet, float slideOffset)
    {
        var y = errorView.GetY();
        var newHeight = (bottomSheet.Height - bottomSheet.Top - y) * 0.8;
        var parameters = errorView.LayoutParameters;
        if (parameters != null)
            parameters.Height = (int)newHeight;

        errorView.LayoutParameters = parameters;
    }

    public override void OnStateChanged(View bottomSheet, int newState)
    {
    }
}

Upvotes: 0

Views: 25

Answers (0)

Related Questions