Great Question Great Question - 7 days ago 8
Android Question

Android: Fragment overlay another fragment with semi transparent

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();

Answer

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!

Comments