Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 698
A.Alqadomi Avatar asked Aug 27 '19 09:08

A.Alqadomi


People also ask

How do I reduce space between items in RecyclerView?

Try to use cardElevation=0dp. This should remove the extra spacing between recyclerview items.

How can a divider line be added in an android RecyclerView?

The simplest way to add dividers to the RecyclerView is to add a DividerItemDecoration. This will add simple line dividers with one line of code.

How many layouts are used in RecyclerView?

The RecyclerView library provides three layout managers, which handle the most common layout situations: LinearLayoutManager arranges the items in a one-dimensional list.


2 Answers

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.

like image 193
Akhil Soman Avatar answered Jan 02 '23 08:01

Akhil Soman


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 .

like image 28
A.Alqadomi Avatar answered Jan 02 '23 08:01

A.Alqadomi