A.Alqadomi
A.Alqadomi

Reputation: 1589

How can I make a recyclerView to take up to half the screen(max) in android constraint layout?

I have a screen that contains 2 views, a map(top) and a recycler(bottom) The rules are simple .

the recycler view is allowed to extend up to the middle of the screen, if more space is needed then it should scroll instead, the map will occupy the rest of the space, Of course if the recycler has less elements then it should shrink leaving more space for the map ..

I am trying to achieve this using constraint layout, also I am trying to avoid solutions that involved calculations .

Check image below for more information on what I am trying to achieve : enter image description here

Here is my code

<?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/white"
    tools:context="com.qmic.itraffic.ui.activities.crowdsourcing.CrowdSourceReportingDetailsActivity">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/halfScreenGuideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_percent="0.5" />

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/categories"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        android:background="@color/manatee"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/categories"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        android:clipToPadding="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/halfScreenGuideline" />



</androidx.constraintlayout.widget.ConstraintLayout>

My Question is can I achieve this behaviour using xml only (constraint layout )?or I would need to do calculation ?

Upvotes: 9

Views: 3251

Answers (2)

A.Alqadomi
A.Alqadomi

Reputation: 1589

The answer by Akhil Soman is correct but missing one thing

app:layout_constraintVertical_bias="1.0"

Without it the RecyclerView was floating above the bottom of the screen leaving an empty space .

for reference here is the complete answer

<androidx.recyclerview.widget.RecyclerView
        android:id="@+id/categories"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="0dp"
        android:layout_marginBottom="0dp"
        android:clipToPadding="false"
        app:layout_constrainedHeight="true"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/halfScreenGuideline"
        app:layout_constraintVertical_bias="1.0" />

Also the suggestion by the Jaymin seems interesting and can fix this problem, but I haven't applied it because I already used Akil solution .

Upvotes: 2

Akhil Soman
Akhil Soman

Reputation: 2217

Could you please try the following code in your recyclerview xml

android:layout_height="wrap_content"
app:layout_constrainedHeight="true"

Change the height to wrap content and add the second the line. This way the recyclerview should have the height same as its content but never exceeds the guideline/contraint.

Upvotes: 4

Related Questions