Home > Mobile >  Keep the button aligned to bottom of the dialog
Keep the button aligned to bottom of the dialog

Time:07-16

I've a layout like design 1 and I want to align cancel button to bottom of the dialog always. recyclerview1 & recyclerview2 have different heights due to display content. if the both recyclerview1 and recyclerview content are small and able to display within screen without scroll it should be like design 2 . If the recyclerviews exceeded the heights dialog should take the device height and align cancel button to bottom of the dialog like design 3.

Current output and final output

Current code:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" >
        
        <TextView
            android:id="@ id/textview1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:text="text1"
            android:textSize="10sp" />
        
        <TextView
            android:id="@ id/textview2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentStart="true"
            android:layout_below="@id/textview1"
            android:text="text2"
            android:textSize="10sp" />
        
        <androidx.core.widget.NestedScrollView
            android:id="@ id/scrollView"
            android:layout_below="@id/textview2"
            android:layout_marginTop="10dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">
                
                <LinearLayout
                    android:id="@ id/linearLayoutContainer1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@ id/recyclerView1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                        
                </LinearLayout>
                
                <LinearLayout
                    android:id="@ id/linearLayoutContainer2"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical" >
                    
                    <androidx.recyclerview.widget.RecyclerView
                        android:id="@ id/recyclerView2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                
                </LinearLayout>
            
            </LinearLayout>
        
        </androidx.core.widget.NestedScrollView>
        
        <LinearLayout
            android:id="@ id/linearLayout1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/scrollView"
            android:layout_alignParentBottom="true"
            android:orientation="horizontal"
            android:gravity="right">
            
            <Button
                android:id="@ id/btnCancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Cancel"/>
        
        </LinearLayout>
    
    </RelativeLayout>

CodePudding user response:

You will have to create a custom dialog

inside onViewCreated() inflate a layout_bottom_button using gravity bottom, so inflated layout will always be at bottom

    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
    );
    params.gravity = Gravity.BOTTOM;
    View bottomButtonView = this.getLayoutInflater().inflate(R.layout.layout_bottom_button, null);
    
this.getDialog().addContentView(viewM, params);

keep in mind that recyclerView should have bottom margin same as layout_bottom_button layout height, so that recyclerView does not hide below button.

  • Related