Is there a way to show a preview of a RecyclerView's contents in the Android Studio editor?

AndroidAndroid StudioAndroid RecyclerviewAndroid Tools-Namespace

Android Problem Overview


When I add the RecyclerView to the layout, it shows up as a blank screen. Is there a way, such as through the tools namespace, to show a preview of the content of the RecyclerView?

Android Solutions


Solution 1 - Android

@oRRs is right !

I'm using Android Studio 1.4 RC2 and you can now specify any custom layout.

I tried a custom CardView and it works.

tools:listitem="@android:layout/simple_list_item_checked"

Solution 2 - Android

Android tools and LayoutManager

>tools namespace enables design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources) It is really powerful feature that is developing and allows you not compile code every time to see changes

AndroidX[About] and GridLayoutManager

implementation 'androidx.recyclerview:recyclerview:1.1.0'
<androidx.recyclerview.widget.RecyclerView
    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:layoutManager="androidx.recyclerview.widget.GridLayoutManager"
    tools:listitem="@layout/item"
    tools:itemCount="10"
    tools:orientation="vertical"
    tools:scrollbars="vertical"
    tools:spanCount="3"/>

Support library and LinearLayoutManager

implementation 'com.android.support:recyclerview-v7:28.0.0'

<android.support.v7.widget.RecyclerView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"
    android:layout_height="wrap_content"

    tools:layoutManager="android.support.v7.widget.LinearLayoutManager"
    tools:listitem="@layout/item"
    tools:itemCount="3"
    tools:orientation="horizontal"
    tools:scrollbars="horizontal" />

Another cool feature that was introduced in Android studio 3.0 is predefining a data through the tools attributes, to visualised easily your layout structure using @tools:sample/* resources

item.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="100dp"
    android:layout_height="150dp"
    android:layout_marginRight="15dp"
    android:layout_marginBottom="10dp"
    android:orientation="vertical"
    tools:background="@tools:sample/backgrounds/scenic">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/colorWhite"
        tools:text="@tools:sample/first_names" />

</FrameLayout>

Simulator results:

https://i.stack.imgur.com/vP3Hl.png" height="330">https://i.stack.imgur.com/PUPZ6.png" height="330">https://i.stack.imgur.com/LopuJ.png" height="330">

Solution 3 - Android

First, add the following line in your item XML to made a preview of your list while you edit your item:

tools:showIn="@layout/activity_my_recyclerview_item"

And them, add the following line in your RecyclerView XML to preview how your item will look in your list:

tools:listitem="@layout/adapter_item"

Solution 4 - Android

As of Android Studio 1.3.1 it shows default list items in the preview but it doesn't let yout specify your own yet. Hopefully, it will come.

Solution 5 - Android

If you have already a custom_item layout:

<androidx.recyclerview.widget.RecyclerView
             android:id="@+id/recyclerView"
             ...
             ...
             **tools:listitem="@layout/name_of_your_custom_item_view"**
             ...>
</androidx.recyclerview.widget.RecyclerView>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionnclarkcltView Question on Stackoverflow
Solution 1 - AndroidPhilippe DavidView Answer on Stackoverflow
Solution 2 - AndroidyoAlex5View Answer on Stackoverflow
Solution 3 - AndroidÂngelo PolottoView Answer on Stackoverflow
Solution 4 - AndroidGáborView Answer on Stackoverflow
Solution 5 - AndroidRodrigo LossView Answer on Stackoverflow