XML cardView is blocking the top half of the recyclerView below it

Viewed 115

I am new to android studio and XML I have a cardview that is blocking my recyclerview. At first I thought it was not working ,but once I took the card view out I could see the recycler.

What i need help on is so that my recylcer view start right below my cardview not matter how long the cardview gets.

XML CODE:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeReplies"
    android:background="#FF9D9D">

    <androidx.cardview.widget.CardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp">

        <RelativeLayout
            android:id="@+id/relativeLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:padding="8dp">

            <de.hdodenhof.circleimageview.CircleImageView
                android:id="@+id/profilePic"
                android:layout_width="60dp"
                android:layout_height="60dp"
                android:layout_alignParentStart="true"
                android:layout_alignParentLeft="true" />

            <TextView
                android:id="@+id/userName"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@+id/profilePic"
                android:maxLines="1"
                android:text="userName"
                android:textAppearance="@style/TextAppearance.AppCompat.Large" />

            <TextView
                android:id="@+id/post"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/userName"
                android:layout_toRightOf="@+id/profilePic"
                android:text="test" />



        </RelativeLayout>

    </androidx.cardview.widget.CardView>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycle"
        android:layout_width="344dp"
        android:layout_height="601dp"
        android:layout_below="@+id/cardView"
        android:layout_marginLeft="25dp"
        android:layout_marginTop="100dp"
        android:layout_marginRight="25dp"
        android:layout_marginBottom="50dp" />


</androidx.coordinatorlayout.widget.CoordinatorLayout>
1 Answers

Best way to use is to create xml file separately for your views and then this file put into your main XML file which is registered with activity.

1) Create Xml file and put your cardView and recyclerview design in it.

<LinearLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:showIn="@layout/HomeReplies">

<androidx.cardview.widget.CardView
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginRight="8dp">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp">

        <ImageView
            android:id="@+id/profilePic"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true" />

        <TextView
            android:id="@+id/userName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@+id/profilePic"
            android:maxLines="1"
            android:text="userName"
            android:textAppearance="@style/TextAppearance.AppCompat.Large" />

        <TextView
            android:id="@+id/post"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/userName"
            android:layout_toRightOf="@+id/profilePic"
            android:text="test" />

    </RelativeLayout>

</androidx.cardview.widget.CardView>

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/recycle"
    android:layout_width="344dp"
    android:layout_height="match_parent"
    android:layout_below="@+id/cardView"
    android:layout_marginLeft="25dp"
    android:layout_marginTop="10dp"
    android:layout_marginRight="25dp"
    android:layout_marginBottom="50dp" />
</LinearLayout>

Note: make sure in above xml file you have added this two lines app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:showIn="@layout/HomeReplies" in your parent layout.

2) Add above layout file into your main XML file using include tag.

<androidx.coordinatorlayout.widget.CoordinatorLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FF9D9D"
    tools:context=".HomeReplies">

       <include layout="@layout/content" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>
Related