4

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!

KasparTr
  • 2,328
  • 5
  • 26
  • 55
  • the preferred way is to call `inflate` – njzk2 May 19 '16 at 14:51
  • Thanks, I edited the question. In short: Nothing changed – KasparTr May 19 '16 at 15:09
  • did you inspect the content of the return value from the `inflate` call? – njzk2 May 19 '16 at 15:12
  • Yes, I printed out View.getTag() of the returned View by ViewStub.inflate() and it was the tag I set for the intro_landing1 RelativeLayout. – KasparTr May 19 '16 at 15:30
  • can you add a minimal version of the file you load (remove as much as you can from it, but make sure it still reproduces the issue. Ideally, just the container view group with a background image or something like that) – njzk2 May 19 '16 at 16:20
  • @njzk2 Please refer to Edit3 – KasparTr May 19 '16 at 17:09
  • try to show hide view stub. coz view stub are visible when the are inflated or the set to visible – Pramod mishra May 26 '16 at 10:47
  • Could you make your question less confusing? A continuous and ordered text without edit marks would make it easier to read. Versioning is done automatically and everybody who cares can see, what you changed. – tynn May 28 '16 at 10:27
  • 1
    @tynn Thanks for the feedback, I rewrote the question. Hope it is more easy to follow. – KasparTr May 29 '16 at 15:26
  • If I were you I'd run a bare minimum demo just to see if it works there and then go look for the differences – Alex.F May 29 '16 at 16:06

3 Answers3

2

So I couldn't reproduce your problem. I'll share here my bare code as it works on my setup.
I suspect that you might have some extra code somewhere that might wrongly interact with what you tried to do. (I tried to set it up as closely as possible to the question)

activity_main.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="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.myexample.helloworld.MainActivity">

    <ViewStub
        android:id="@+id/myViewStub"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:inflatedId="@+id/myInflatedViewStub"
        android:visibility="gone"
        android:layout="@layout/stub"/>
</FrameLayout>

stub.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="100dp"
        android:text="Large Text"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

MainActivity.java

public class MainActivity
        extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((ViewStub) findViewById(R.id.myViewStub)).setVisibility(View.VISIBLE);
    }
}

P.S. I usually link to a demo project but in this case there is not much code and it won't depend on the link in the future if anybody ever sees the answer. Hope it helps

Alex.F
  • 5,648
  • 3
  • 39
  • 63
0

May this help you to understand ViewStub behaviour
2. ViewStub can not change visibility without setting layout resource and inflation. so once you set layout resource then you can inflate or setVisibility(View.VISIBLE);
3. When the method inflate() is invoked the ViewStub is removed from its parent and replaced with the right View (the root view of your layout).
for more How to use View Stub in android

Community
  • 1
  • 1
Qamar
  • 4,959
  • 1
  • 30
  • 49
0

It turns out that my implementation was correct but here is what happened:

On booting up my app opened the tutorial view (inflated the ViewStub) and wrote the previously started into shared prefrences (SP) and immediatly after that restarted the activity (ran the onCreated again) but this time the tutorial view was not inflated because the if sentense failed.

This happens only the first time the app is opened and thus it wasn't immedialty clear that this was the problem

I still don't know why it restarts the acitivty on first time opening the app but I believe its due to a memory issue.

I solved it by writing the tutorial inflation flag into SP only after the users has followed the steps.

Since @Alex.F answer was practically a correct one, I will mark it as the awnser.

KasparTr
  • 2,328
  • 5
  • 26
  • 55