How can I declare a Fragment as hidden in an XML layout How can I declare a Fragment as hidden in an XML layout xml xml

How can I declare a Fragment as hidden in an XML layout


I faced a similar situation, where I had to hide a fragment.

I simply included the fragment inside a LinearLayout and marked the layout to be visible/gone.

<LinearLayout    android:id="@+id/map_layout"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:visibility="visible" >    <fragment        android:id="@+id/map"        android:layout_width="match_parent"        android:layout_height="match_parent"        class="com.google.android.gms.maps.MapFragment" /></LinearLayout>


Based off Jyo's post, use this:

FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();fragmentTransaction.hide(mFragment);fragmentTransaction.commit();

This has worked for me on API Level 23. mFragment is the fragment that you want to hide.


This answer is a tad late thought it may be helpful for future reference. Visibility is part of the View class - Fragment extends object though not having access to the visibility values. A possibility is making the Fragment a child of a FrameLayout and calling invisible or gone on the layout. This will cause the fragment to appear to be hidden.

Hope it helps!