Search code examples
androidxmlandroid-layoutandroid-xmlviewstub

ViewStub dont show


My ViewStub is not showing

<ViewStub
     android:id="@+id/hint"
     android:layout_width="500dp"
     android:layout_height="500dp"
     android:inflatedId="@+id/main_niht"
     android:layout="@layout/main_hint"
     app:layout_constraintBottom_toBottomOf="parent"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toTopOf="parent" />

Here is my ViewStub It is inside the main layout

In Oncreate I call this

b.hint.inflate()
b.hint.visibility = View.VISIBLE

But ViewStub still doesn't show like it never existed

Are there any tips on how to use it correctly?


Solution

  • inflate replaces the ViewStub with the inflated view, so your call to set the visibility does nothing. If your inflated view starts invisible, that would explain your problem. You need to set the visibility on the inflated view, not the view stub.

    val inflatedView = b.hint.inflate()
    inflatedView.visibility = View.VISIBLE
    

    If that doesn't work, use Layout Inspector or show the layout of the view you're trying to inflate.