13

Let's say I have 2 fragments, one contain a list view and another contain a loading text. I want when I click on one list item, the loading text fragment appears on top of the list view. I have adjusted the opacity of the loading text background to: android:background="#33FFFFFF" . But still it just shows the loading text on a solid grey background.

<?xml version="1.0" encoding="utf-8"?>
<ListView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/list"
    android:background="#e8e9ee"
    android:divider="#ccc"
    android:dividerHeight="1dp"/>

Fragment that contains a textview:

<?xml version="1.0" encoding="utf-8"?>
<TextView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:text="@string/loadingText"
    android:id="@+id/loadingText"
    android:textColor="#e8e9ee"
    android:background="#33FFFFFF"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

My java code is basically something like this: onItemClick:

 FragmentTransaction transaction=manager.beginTransaction();
 transaction.show(loadingFragment);
 transaction.commit();
philomath
  • 2,209
  • 6
  • 33
  • 45

2 Answers2

13

I did it and it works perfectly but instead of using .show I used .add:

Activity layout:

<RelativeLayout
    android:id="@+id/layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginRight="5dp"
    android:layout_marginEnd="5dp">

    <FrameLayout
        android:id="@+id/list_view_container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true">
    </FrameLayout>

    <FrameLayout
        android:id="@+id/loading_text_fragment_container"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true">
    </FrameLayout>

</RelativeLayout>

In the Activity:

First add your List View:

ListViewFragment listViewFragment = new ListViewFragment();
fragmentManager.beginTransaction().add(R.id.list_view_container, listViewFragment).commit();

Then the loading text:

// Create a new Fragment to be placed in the activity layout
YourFragment yourFragment = new YourFragment();

// Add the fragment to the 'loading_text_fragment_container' FrameLayout
fragmentManager.beginTransaction().add(R.id.loading_text_fragment_container, yourFragment).commit();

In YourFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.your_fragment_layout, container, false);
}

And the your_fragment_layout.xml has a common TextView without any special attribute.

Hope it to be useful,

regards!

Community
  • 1
  • 1
Nahuel Barrios
  • 1,870
  • 19
  • 22
  • secondly I get error 'fragmentManager.beginTransaction() call required API 11 or before', so please provide versions too. – ishandutta2007 Sep 12 '17 at 03:36
  • Hi! No, ListViewFragment is just the name of my LisView (I don't have the code [2014] but it has no special behavior. About the required API level, try using the Support Library. Regards! – Nahuel Barrios Sep 25 '17 at 12:13
2

I think you are actually replacing the original fragment rather than overlaying it. You should create two framelayouts that cover the full screen and then assign the loading fragment to the overlaying frame layout

in you activity

<FrameLayout id="@+id/ListFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
</FrameLayout>
<FrameLayout id="@+id/LoadingFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
</FrameLayout>

and then to load them

FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.ListFragment, listFragment);
transaction.commit();

FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.LoadingFragment, loadingFragment);
transaction.commit();
Kelly
  • 164
  • 2
  • 6
  • 1
    cant u do this using one frame layout and just add two other container layouts to the view with each reprenting the fragment? – Jono Apr 15 '15 at 13:20
  • 1
    Not really, frame layout is the best for adding fragments dynamically. – Kelly Apr 16 '15 at 13:40