Jay Lieske
Jay Lieske

Reputation: 4858

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

My activity declares all of its GUI fragments in a single XML layout. It only needs to display a few of the fragments at launch time; the rest get shown as the user interacts with the app. A portion of the layout is as follows:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <fragment
        android:id="@+id/map_panel"
        android:name="com.example.MapPanel"
        android:layout_width="match_parent"
        android:layout_height="@dimen/map_panel_height" />
    <fragment
        android:id="@+id/list_panel"
        android:name="com.example.ListPanel"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/map_panel" />
    <fragment
        android:id="@+id/detail_panel"
        android:name="com.example.DetailPanel"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/map_panel"
        android:visibility="gone" />

My intention is that the list_panel fragment is visible at startup, and the detail_panel fragment is hidden until the user selects something from the list.

By default, a fragment starts out with the isHidden attribute as false. That means my activity has to iterate through the loaded fragments and manually call isHidden(true) on fragments like detail_panel at startup time.

I would prefer to declare the isHidden status in the XML layout. However, setting android:visibility="gone" in a <fragment> declaration does not change the isHidden status, and I can't find any documentation on another attribute that would do the trick.

Is it possible to set an XML attribute on a <fragment> to cause it to be hidden?

Note: I'm not concerned with view visibility, I'm concerned with the fragment.isHidden() value. That affects how FragmentManager manipulates the back stack and performs animations. If you call transaction.show(fragment) on a fragment whose view is invisible or gone, but the fragment.isHidden() value is false, then the FragmentManager will not make the view visible. See http://developer.android.com/reference/android/app/Fragment.html#isHidden() for reference.

Upvotes: 19

Views: 19125

Answers (5)

Neil P.
Neil P.

Reputation: 1114

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.

Upvotes: 10

Vikas
Vikas

Reputation: 4301

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>

Upvotes: 15

Jyo the Whiff
Jyo the Whiff

Reputation: 839

public void showHideFrgament(final Fragment fragment){

    FragmentTransaction ft = getFragmentManager().beginTransaction();
                    ft.setCustomAnimations(android.R.animator.fade_in,
                            android.R.animator.fade_out);

    if (fragment.isHidden()) {
                    ft.show(fragment);
                    Log.d("hidden","Show");
                } else {
                    ft.hide(fragment);
                    Log.d("Shown","Hide");                        
                }
                ft.commit();

 }

Upvotes: 0

Clocker
Clocker

Reputation: 1336

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!

Upvotes: 0

SHASHIDHAR MANCHUKONDA
SHASHIDHAR MANCHUKONDA

Reputation: 3322

we have isVisible Method for fragment seeting visibilty to Gone does not take any space Where as Invisble takes the actual view space.

Upvotes: -1

Related Questions