Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display ViewStub preview in Android Studio?

I have a ViewStub like this -

<android.support.constraint.ConstraintLayout>
    <ViewStub
        android:id="@+id/photos"
        android:layout="@layout/add_photos"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="visible"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/details"
        tools:visibility="visible" />
 ...
</android.support.constraint.ConstraintLayout>

But the ViewStub is not rendered in the layout preview and design tabs. Using <include/> works though.

I am using Android Studio 3.0 Beta 6.

Is there a way to render preview when using ViewStub?

EDIT:

Looks like there is a feature request in google issue tracker https://issuetracker.google.com/issues/37090997. But no one has looked into it till now. I will still keep the question open if anyone has any workarounds.

like image 370
Vivekananda Athukuri Avatar asked Sep 25 '17 13:09

Vivekananda Athukuri


People also ask

How do I use view stubs on Android?

When the ViewStub is inflated, it replaces itself in its parent view with the inflated layout resource. Its visibility will exist in the view hierarchy only when setVisibility(int) or inflate() is invoked. The inflated view is added to ViewStub's parent using the layout parameter.

How do I use ViewStub?

A ViewStub is a zero-sized invisible View which is used to load "layout resource" at runtime. ViewStub is a zero dimension View, so you will not see anything on the layout pallete. To make parent resource visible, inflate() method is invoked. To make ViewStub visible or invisible, setVisibility(int) method is invoked.


1 Answers

Add this to the ViewStub:

tools:visibility="visible" 

and declare xmlns:tools="http://schemas.android.com/tools" in your layout.

like image 155
Lucas Avatar answered Sep 23 '22 11:09

Lucas