I am using ViewStub inside FrameLayout so that I can inflate it with a tutorial view the first time the app is opened.
My Activity.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<ViewStub
android:id="@+id/introHolder"
android:inflatedId="@+id/introHolder"
android:layout="@layout/intro_landing1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
the view I am inflating it is called intro_landing1.xml
and it is a RelativeLayout.
intro_landing1.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:background="@color/opaqBlack30"
android:id="@+id/introView1"
android:tag="RelativeLayoutIntroViewTag">
<!--...-->
</RelativeLayout>
In my Activity onCreate I used two different approaches to inflate the ViewStub but neither of them work (I can't see the intro_landing1 view).
1st approach - setting visibility:
if(!previouslyStarted){
((ViewStub) findViewById(R.id.introHolder)).setVisibility(View.VISIBLE);
}
2nd approach - inflating the ViewStub:
ViewStub introStub = (ViewStub) findViewById(R.id.introHolder);
introStub.setLayoutResource(R.layout.intro_landing1);
View inflatedView = introStub.inflate();
With the 2nd approach I logged the returned View (inflatedView) by doing inflatedView.getTag and it returned the intro_landing1 RelativeLayout's tag "RelativeLayoutIntroViewTag" so the view is actually returned but I dont see it.
To make sure I positioned the ViewStub correctly in the view tree hierarchy I used the <include/>
tag in the Activity.xml instead of the ViewStub like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
...
<!-- <ViewStub
android:id="@+id/introHolder"
android:inflatedId="@+id/introHolder"
android:layout="@layout/intro_landing1"
android:layout_width="match_parent"
android:layout_height="match_parent" />-->
<include layout="@layout/intro_landing1"/>
</FrameLayout>
And this works.
Why is it not showing the ViewStub after visibility change or inflation?
Thanks!