Home > Net >  RelativeLayout height increases after RecyclerView loads
RelativeLayout height increases after RecyclerView loads

Time:01-15

My activity has a RecyclerView and 2 Button. The RecyclerView is in between the 2 Buttons and takes the remaining height of the screen. It works as expected, but when I set the adapter of the RecyclerView and fill it with items, the height of the RecyclerView (and parent RelativeLayout) expands and goes over the Button below it. I want my layout to remain the same (as well as the height of the RelativeLayout and RecyclerView). Here is my xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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:orientation="vertical"
android:padding="25dp">

<Button android:id="@ id/download_btn"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="download all canadian universities"
        android:textColor="#FFFFFF"
        android:textSize="22sp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:layout_weight="0"
/>

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:background="#FFAA22AA">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Cleared!"
        android:visibility="gone"/>

    <android.support.v7.widget.RecyclerView
        android:id="@ id/rvSchools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</RelativeLayout>

<Button android:id="@ id/clear_btn"
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:text="clear"
        android:textColor="#FFFFFF"
        android:textSize="22sp"
        android:background="@drawable/buttonshape"
        android:shadowColor="#A8A8A8"
        android:layout_weight="0"
/>

</androidx.appcompat.widget.LinearLayoutCompat>

CodePudding user response:

I think in that case better solution is to use ConstraintLayout as your root layout:

<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.andr`enter code here`oid.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<Button android:id="@ id/download_btn"
    android:layout_width="0dp"
    android:layout_height="100dp"
    android:text="download all canadian universities"
    android:textColor="#FFFFFF"
    android:textSize="22sp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    app:layout_constraintTop_toTopof="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
/>


<View 
    android:background="#FFAA22AA"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/download_btn"
    app:layout_constraintBottom_toTopOf="@ id/clear_btn"
/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@ id/rvSchools"
    android:layout_width="0dp"
    android:layout_height="0dp"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toBottomOf="@ id/download_btn"
    app:layout_constraintBottom_toTopOf="@ id/clear_btn"/>
    
  <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Cleared!"
    android:visibility="gone"
    app:layout_constraintTop_toTopof="@ id/rvSchools"
    app:layout_constraintStart_toStartOf="@ id/rvSchools"
    app:layout_constraintEnd_toEndOf="@ id/rvSchools"
    app:layout_constraintBottom_toBottomOf="@ id/rvSchools"
    />

<Button android:id="@ id/clear_btn"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:text="clear"
    android:textColor="#FFFFFF"
    android:textSize="22sp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

ConstraintLayout is much more powerful than RelativeLayout. You also don't need separate layout for background for RecyclerView - in my example I used View with required background color that has the same constraints as RecyclerView.

CodePudding user response:

You can also use RelativeLayout instead:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical"
android:padding="25dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_above="@id/clear_btn"
android:orientation="vertical">

<Button android:id="@ id/download_btn"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:text="download all canadian universities"
    android:textColor="#FFFFFF"
    android:textSize="22sp"
    android:background="@drawable/buttonshape"
    android:shadowColor="#A8A8A8"
    android:textAlignment="center"/>

<TextView
    android:id="@ id/clear_textView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="Cleared!"
    android:textSize="24sp"
    android:gravity="center"
    android:visibility="gone"/>

<androidx.recyclerview.widget.RecyclerView
    android:id="@ id/rvSchools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

<Button android:id="@ id/clear_btn"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="clear"
android:textColor="#FFFFFF"
android:textSize="22sp"
android:background="@drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:layout_alignParentBottom="true"/>

</RelativeLayout>
  • Related