I want to achieve this type of dynamic height of grid layout in recycler view. If a cell is having two lines then other cells in the same row should be of same height despite of having one line in other cells.
Here is my row file code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginBottom="-20dp"
android:layout_marginLeft="-12dp">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/cvHomeItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="@dimen/margin_8"
app:cardElevation="@dimen/margin_8"
app:cardUseCompatPadding="true">
<LinearLayout
android:id="@ id/llChild"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/_6ssp"
android:gravity="center_vertical"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/minus_margin_2"
android:gravity="center_vertical"
android:weightSum="1">
<ImageView
android:id="@ id/ivImage"
android:layout_width="@dimen/margin_24"
android:layout_height="@dimen/margin_24"
android:layout_marginLeft="@dimen/margin_2"
android:scaleType="centerInside"
tools:src="@drawable/ic_announcement"
android:contentDescription="@string/app_name" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end">
<TextView
android:id="@ id/tvDays"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_12"
android:gravity="center_vertical"
android:lines="2"
android:textColor="@color/colorText"
android:textSize="@dimen/_11ssp"
tools:text="3.5 Day(s)" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="@ id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="Annual Leave"
android:textColor="@color/colorText"
android:textSize="@dimen/_11ssp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
Do let me know if any clarification or any code required. Thank you!
CodePudding user response:
Make the height of your linearlayout ("@ id/llChild) to match_parent
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="-4dp"
android:layout_marginTop="-4dp"
android:layout_marginBottom="-20dp"
android:layout_marginLeft="-12dp">
<androidx.cardview.widget.CardView xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@ id/cvHomeItem"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="@color/white"
app:cardCornerRadius="@dimen/margin_8"
app:cardElevation="@dimen/margin_8"
app:cardUseCompatPadding="true">
<LinearLayout
android:id="@ id/llChild"
android:layout_width="match_parent"
android:layout_height="match_parent" <= correction
android:layout_margin="@dimen/_6ssp"
android:gravity="center_vertical"
android:orientation="vertical">
.....
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
CodePudding user response:
try this
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@ id/cvItemMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="6dp"
android:orientation="vertical"
app:cardCornerRadius="6dp"
app:cardElevation="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@ id/ivLogo"
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="6dp"
android:src="@drawable/ic_your_image"
app:layout_constraintBottom_toTopOf="@id/tvName"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:gravity="bottom"
android:maxLines="2"
android:minLines="2"
android:text="Text 1"
android:textColor="#000"
android:textSize="14sp"
app:layout_constraintBottom_toBottomOf="@id/tvPrice"
app:layout_constraintStart_toStartOf="@id/ivLogo"
app:layout_constraintTop_toBottomOf="@id/ivLogo"
app:layout_constraintWidth_percent="0.8" />
<TextView
android:id="@ id/tvPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
android:maxLines="1"
android:minLines="1"
android:text="Text 2"
android:textColor="#F56114"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="@id/tvName"
app:layout_constraintTop_toBottomOf="@id/tvName" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>