0

I've seen answers from this thread, but I'm wondering if it's possible to do this in XML by adding a button component.

Earlier I had a button wrapped within a CardView, which will float as the users scrolling the RecyclerView, so I had a lot of code associated with this button. But now I need to make this button stay at the bottom of the screen instead of floating. I've tried to add another level of LinearLayout or even change the CoordinatorLayout to LinearLayout, but none of these worked.

Here's my original XML:

   <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/ui_root"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    tools:context=".ui.pages.MainActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/appbar_padding_top"
        android:theme="@style/AppTheme.AppBarOverlay">


        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/action_bar_height"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
            app:popupTheme="@style/AppTheme.PopupOverlay">

        <include layout="@layout/common_toolbar_with_buttons" />
        </androidx.appcompat.widget.Toolbar>

        <android.common.view.indicator.LoadingIndicatorBar
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="visible" />
       </com.google.android.material.appbar.AppBarLayout>


    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/ui_attribute_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:paddingBottom="@dimen/item_input_attribute_list"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <androidx.cardview.widget.CardView
        android:id="@+id/ui_button_next_container"
        android:layout_width="match_parent"
        android:layout_height="@dimen/button_height_normal"
        android:layout_gravity="center_horizontal|bottom"
        android:layout_margin="24dp"
        android:layout_marginBottom="8dp"
        app:cardBackgroundColor="@color/colorAccent_Light"
        app:cardElevation="12dp"
        app:cardPreventCornerOverlap="true"
        app:layout_dodgeInsetEdges="bottom">

        <View
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@drawable/pointed_gradient" />

        <android.common.view.tint.TintFancyButton
            android:id="@+id/ui_button_next"
            style="@style/TransparentButtonStyle"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="@string/msg_save_current_input"
            android:textAllCaps="true"
            android:textSize="16sp"
            android:textStyle="bold"
            app:fb_borderColor="#2fffffff"
            app:fb_borderWidth="2dp"
            app:fb_defaultColor="@color/transparent"
            app:fb_focusColor="@color/colorAccent_Light"
            app:fb_textColor="#FFF" />
    </androidx.cardview.widget.CardView>
  </androidx.coordinatorlayout.widget.CoordinatorLayout> 

randomcat
  • 413
  • 1
  • 4
  • 16

2 Answers2

0

enter image description hereTake a constraint layout outside of your recycler view and button and then make the button app:layout_constraintTop_toBottomOf="@id/recyclerview" in xml. Or you can also use linear layout in place of constraint layout, and set android:orientation="vertical".

This is the code.

<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/ui_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="MainActivity">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/bar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="20dp"
        >
        
        <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="@dimen/Textsize25"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"
           >

        </androidx.appcompat.widget.Toolbar>

        <android.common.view.indicator.LoadingIndicatorBar
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:visibility="visible" />
    </com.google.android.material.appbar.AppBarLayout>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
        
        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/ui_attribute_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:clipToPadding="false"
            android:paddingBottom="25dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"
    
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toTopOf="@+id/ui_button_next_container"
            app:layout_constraintVertical_bias="0.05"/>
    
        <androidx.cardview.widget.CardView
            android:id="@+id/ui_button_next_container"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_gravity="center_horizontal|bottom"
            android:layout_margin="24dp"
            android:layout_marginBottom="8dp"
            app:cardElevation="12dp"
            app:cardPreventCornerOverlap="true"
            app:layout_dodgeInsetEdges="bottom"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintVertical_bias="1">
    
            <View
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                 />
    
            <Button
                android:id="@+id/ui_button_next"
    
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="@string/msg_save_current_input"
                android:textAllCaps="true"
                android:textSize="16sp"
                android:textStyle="bold"
                 />
        </androidx.cardview.widget.CardView>

    </androidx.constraintlayout.widget.ConstraintLayout>


</LinearLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>
  • 1
    This answer will be more helpful if you include a more complete XML example. – Code-Apprentice May 07 '21 at 02:54
  • I tried both earlier. The ConstraintLayout would prevent floating. However, when my recycerView is longer than the screen size, The CardView is somehow hidden at the bottom and I cannot keep scrolling – randomcat May 07 '21 at 03:24
  • Code updated, also trying to add screenshot of view. I used linear layout and constraint layout. – Joydeep Saha May 07 '21 at 05:08
0

I would recommend using a constraint layout to position all of your views.

In order to get the RecyclerView to fill the space between the top View and Button View. You'll need to use a match constraint for the RecyclerView height. To make the RecyclerView match the constraint height set the layout_height to 0dp.

       <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/ui_attribute_list"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:clipToPadding="false"
            android:paddingBottom="25dp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"

            app:layout_constraintBottom_toTopOf="@+id/ui_button_next_container"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.05" />
helper
  • 100
  • 6