Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 556
Jay Lieske Avatar asked Nov 15 '13 02:11

Jay Lieske


People also ask

How do you make an invisible fragment?

If you remove layout. setVisibility(View. GONE); from the code then ft. hide(f); will not hide fragment.

What is a fragment in XML?

A Fragment is a combination of an XML layout file and a java class much like an Activity . Using the support library, fragments are supported back to all relevant Android versions. Fragments encapsulate views and logic so that it is easier to reuse within activities.

What object is used to add a fragment to a layout?

To declaratively add a fragment to your activity layout's XML, use a FragmentContainerView element. The android:name attribute specifies the class name of the Fragment to instantiate.

How do I specify a fragment in the XML file programmatically?

specifying a fragment in the XML file using inside the activity's layout file; and programmatically by adding the fragment to an existing ViewGroup. First, let's look at the fragment_layout.xml file below.

How to add the fragment in the layout?

For adding the fragment in the layout please go to the following step. On the main_activity layout file click on the Design view and on the left side please go to the bottom and there will be an option named <fragment> click on that and one box will appear. From that please select FragmentA which we have created.

How to use context in a fragment?

How to use context in a fragment? Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have taken two fragments. Step 3 − Add the following code to src/MainActivity.java

What is a fragment in Android?

As mentioned before a fragment is a portion of the UI associated with an activity overall view hierarchy. In the fragment app example we add fragments to the activity layout in two different ways: specifying a fragment in the XML file using inside the activity's layout file; and programmatically by adding the fragment to an existing ViewGroup.


1 Answers

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>
like image 88
Vikas Avatar answered Oct 14 '22 01:10

Vikas