Android recyclerview Animation not working with constraint layout - android

I am applying recyclerview animation from this link
enter-animation-using-recyclerview-and-layoutanimation
but the only change is my recyclerview is inside constraint layout where I need to set height as 0dp for relating views when I set heights as wrap_content or match_parent animation working good.
My XML code
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/ivBanner"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_img" />
<android.support.v7.widget.RecyclerView
android:id="#+id/rvList"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layoutAnimation="#anim/layout_animation_from_bottom"
app:layout_constraintTop_toBottomOf="#id/ivBanner" />
</android.support.constraint.ConstraintLayout>
In recyclerview android:layout_height="0dp" I set as match_parent or wrap_content animation is working good so there is no issue with the layout_animation_from_bottom Thank you in advance :)

Related

Fluid height RecyclerView when full or empty

I have a dynamic recycler view. Which expands the height of its parent when full and looks really nice however when there are no entries added to it yet or there are few entries in it it just looks ugly.
However if I set the width to say 200dp than it looks a bit better.
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/shading"
tools:context=".MainActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="?android:attr/actionBarSize"
android:background="#color/shading">
<ImageView
android:id="#+id/logo"
android:layout_width="120dp"
android:layout_height="60dp"
android:src="#drawable/ic_logo"
app:layout_constraintBottom_toTopOf="#+id/list_projects"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list_projects"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="20dp"
android:background="#drawable/shape_list_item_projects"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo" />
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:backgroundTint="#color/white"
app:fabCradleRoundedCornerRadius="25dp"
app:fabCradleMargin="5dp"
app:hideOnScroll="false"
app:menu="#menu/menu_main_activity" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_outline_add_circle_outline_24"
app:backgroundTint="#color/white"
app:layout_anchor="#id/bottomAppBar"
app:maxImageSize="40dp" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The desired outcome is to have it wrap the content until it reaches the bounds of BottomAppBar.
You can do that by:
Bias the RecyclerView to the top using ConstraintLayout vertical chain:
app:layout_constraintVertical_bias="0"
app:layout_constraintVertical_chainStyle="packed"
This makes the RecyclerView directly under the top image, and pack both to the top of the layout.
Force the RecyclerView default height dimension to wrap_content:
app:layout_constraintHeight_default="wrap"
This makes the RecyclerView to wrap its content when having 0 or few items until it reaches the bounds of BottomAppBar; then the RV will be strict to the constraint.
Remove app:layout_constraintBottom_toTopOf="#+id/list_projects" from the top image to make the top image and RecyclerView aligned to the top (not centered vertically).

ConstraintLayout & FrameLayout wrap_content then FrameLayout show half content

I don't known why FrameLayout inside ConstraintLayout, and both use wrap_content, then the last item in the RecycleView just show half content.
If I remove FrameLayout, direct use recycleview, it can work!
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:background="#drawable/finance_bg_book_list_dialog">
<TextView
android:id="#+id/tvToolbarTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/finance_bg_book_list_toolbar"
android:drawablePadding="10dp"
android:padding="14dp"
android:text="#string/finance_book"
android:textColor="#android:color/white"
android:textSize="?fontSizeLarge"
app:drawableStartCompat="#drawable/finance_ic_toolbar_shelf"
app:layout_constraintBottom_toTopOf="#+id/vulturLayout"
app:layout_constraintTop_toTopOf="parent"/>
<FrameLayout
android:id="#+id/vulturLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tvToolbarTitle">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_contentCode="successful"/>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
You need some horizontal constraints on your textview.
ex : app:layout_constraintStart_toStartOf=
I recommend you set a dimension for the RecyclerView height. if you use the FrameLayout with a wrap_content height, your RecyclerView will not be scrollable and it will own show half the item at the last based on the screen size

ConstraintLayout - how to wrap over RecyclerView

I have a ConstraintLayout with an appBar (containing a toolbar), two fragments (containing a RecyclerView each) and a button at the bottom.
What I am trying to do is to wrap the height of the layout over the size of the RecyclerViews. As you can see on the picture below, every Recyclerview is taking an equal height regardless of the size of the list.
And this is my layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/template_grey_light"
tools:context=".presentation.ui.activity.MainActivity">
<include
android:id="#+id/AppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#+id/fragmentTop"
app:layout_constraintVertical_chainStyle="packed"
layout="#layout/app_bar" />
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentTop"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/AppBar"
app:layout_constraintBottom_toTopOf="#+id/fragmentBottom"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/fragmentBottom"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#+id/fragmentTop"
app:layout_constraintBottom_toTopOf="#+id/shuffleButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
<Button
android:id="#+id/shuffleButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:background="#color/template_love_red"
android:text="Go!"
android:layout_marginBottom="20dp"
app:layout_constraintTop_toBottomOf="#+id/fragmentBottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I understand I am having 0dp on the height attributes and that makes it equal in height, but then if I don't, everything will overlap at the top. Any idea on how to make it work?
Use android:layout_height="wrap_content" in your FragmentContainerView tag

How to set max height to scrollView?

How can I set maxHeight property for scrollView?
I have a LinearLayout and I add some view (rows) to this layout programmatically. How can I show scroll when rows reach maxHeight of scrollView? ScrollView is located in a CardView
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textViewTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#+id/cardView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_max="100dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textViewTitle">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Because you're using ConstraintLayout you need to add 2 constraints to your CardView
app:layout_constraintTop
app:layout_constraintBotttom
And you also need to add the following lines
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="300dp" // Change this to your maximum height
It should look something like this
<androidx.cardview.widget.CardView
android:layout_width="0dp"
android:layout_height="wrap_content"
app:cardCornerRadius="#dimen/card_radius_10"
app:layout_constrainedHeight="true"
app:layout_constraintHeight_max="300dp" // Change this to your maximum height
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" //Change based on your layout
app:layout_constraintTop_toTopOf="parent"> //Change based on your layout
use this attribute in your ScrollView and change the LinearLayout height to match_parent
android:fillViewport="true"
like this
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/layout" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" />
</ScrollView>
Change the height of parent View of your scroleView. In your case, CardView is Parent layout.so set the height of CardView like
android:layout_height="350dp"
or use the weight according to your need by using linear layout in the parent of CardView. if you are using this layout in the Adapter or something like that then you only set the height with numbers.

ConstraintLayout inside ScrollView cannot retain constraints

Guys, I need to make my ConstraintLayout scroll-able, so I placed it inside an ScrollView like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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="match_parent"
android:fillViewport="true"
android:overScrollMode="never">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/img"
android:layout_margin="64dp"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="fitXY"
app:srcCompat="#drawable/avd"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/btn"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintHeight_min="64dp"
app:layout_constraintHeight_max="256dp"/>
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
app:layout_constraintTop_toBottomOf="#id/img"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
But the problem is that I like to scroll layout when all constraints has reached their minimum size(means all controls has reached to their specified margins and layout cannot get smaller anymore). But it seems when ScrollView needs to show scrolls, it specifies a very large size for ConstraintLayout's height, so my layout scales up to max possible size. If you try this layout in portrait and then landscape, you notice how it changes.
I wonder if there is any way to start scrolling when all constraint has reached their minimum size. Any idea how I can do this?

Categories

Resources