AjayR
AjayR

Reputation: 4179

Android Recyclerview not showing if placed inside the other layouts

I am using recycle view to show the list from Android studio and its working fine when it is only element like below.

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.RecyclerView 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:id="@+id/list"
    android:name="com.example.ItemFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    app:layoutManager="LinearLayoutManager"

    tools:context="com.example.ItemFragment"
    tools:listitem="@layout/fragment_item" />

But I want to show Google Ads below the recyclerview, so here is the modified code, which is not showing the recyclerview at all but displaying only Ads.

<RelativeLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <android.support.v7.widget.RecyclerView 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:id="@+id/list"
        android:name="com.example.ItemFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        app:layoutManager="LinearLayoutManager"

        tools:context="com.example.ItemFragment"
        tools:listitem="@layout/fragment_item" />

        <com.google.android.gms.ads.AdView
            android:id="@+id/adView1"
            ads:adSize="BANNER"
            ads:adUnitId="@string/banner_ad_unit_id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            />

    </RelativeLayout>

I am wondering, is it not possible to add sibling elements to Recyclerview, or its not possible to place Recyclerview inside other layouts.

Upvotes: 0

Views: 737

Answers (3)

Nithin V Gopal
Nithin V Gopal

Reputation: 11

Check the condition in the onCreateView()

If it contains a condition like ->

if (view instanceof RecyclerView) {
    ... }

Just remove it or use appropriate condition.

Upvotes: 0

Ayaanp
Ayaanp

Reputation: 327

Try This. You also need to set adapter to recyclerview

<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">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="@string/banner_ad_unit_id"/>

    <android.support.v7.widget.RecyclerView
        android:id="@+id/list"
        android:name="com.example.ItemFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        android:layout_above="@id/adView1"
        app:layoutManager="LinearLayoutManager"
        tools:context="com.example.ItemFragment"/>

</RelativeLayout>

Upvotes: 1

Michael Evans
Michael Evans

Reputation: 495

It's possible to add sibling elements to a RecyclerView, but it looks like you have an invalid Layout. You need to try something like:

<LinearLayout>
    <RecyclerView/>
    <AdView/>
</LinearLayout>

Right now, you have a closing RelativeLayout tag, but no starting one.

Upvotes: 0

Related Questions