androidviewstub

Check if my ViewStub is inflated/visible and is null/non null?


I did see a bunch of other posts before asking this question. None seemed to have a definite solution.

I am running into a issue with ViewStub where I want to check if a ViewStub is inflated or not (visible or not). I have a bunch of other things to do based on that.

I am using two ViewStubs and a button in each ViewStub layout namely in layout/abc and layout/def will inflate the other ViewStub.

<ViewStub
 android:id="@+id/abc_stub"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout="@layout/abc" />

 <ViewStub
 android:id="@+id/def_stub"
 android:layout_width="wrap_content"
 android:layout_height="match_parent"
 android:layout="@layout/def" /> 

The issue I am running into is that the ViewStub that is not inflated is not null while I expect it to be null.

Is there anything I am missing ?


Solution

  • So what I ended up doing was to setOnInflateListener (ViewStub.OnInflateListener inflateListener)

    ViewStub stub1 = ((ViewStub)findViewById(KR.get(R.abc_stub)));
    stub1.setOnInflateListener(new ViewStub.OnInflateListener{
         @Override
         public void onInflate(ViewStub paramViewStub, View paramView) {
           // Set a class boolean if it was inflated                             
         }
    });
    

    If the boolean is true stub1 is the screen that is up. Else its not.

    Thanks to @pskink for the help.