Fragment has default white background on replace() instead of transparent one
J

4

13

I am facing a strange problem while using fragments and just cant figure it out.

When I am replacing fragments inside my MainActivity container, some will keep the desired transparent background, and some will show a default-like white one. I actually want all my fragments to be transparent and 'use' the background assigned to the MainActivity.

For example: Fragment A: white background

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:src="@drawable/logo"/>

    <de.mypackage.uielements.MyButton
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:text="@string/button_text"/>    

    **<!-- ListView to show news -->**
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:dividerHeight="1dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginBottom="10dp"/>
</LinearLayout>

Fragment B: transparent background

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

main.xml

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <!-- Background Image to be set programmatically, transparent being default -->
    <ImageView
        android:id="@+id/window_background_image_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"
        android:background="@android:color/transparent"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/transparent">

        <!-- Top ScrollView -->
        <de.opwoco.app.android.apptitan.app.utils.HorizontalListView
            android:id="@+id/navigation_tab_listview"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:visibility="gone"
            android:background="@color/navigation_background"/>

        <!-- FrameLayout container where all other fragments are placed -->
        <FrameLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:background="@android:color/transparent"
            android:layout_below="@+id/navigation_tab_listview"/>

    </RelativeLayout>

    <fragment
        android:id="@+id/navigation_drawer"
        android:layout_width="@dimen/navigation_drawer_width"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:name="de.mypackage.fragment.NavigationDrawerFragment"
        tools:layout="@layout/fragment_navigation_drawer"/>

</android.support.v4.widget.DrawerLayout>

Both fragments are replaced by using following snippet:

android.support.v4.app.FragmentTransaction transaction =     getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.container, fragment);
transaction.commit();

So my question is:

How can I achieve that all fragments in my container have a transparent background, so that I am always showing the background assigned in the MainActivity?

Janey answered 24/6, 2014 at 9:1 Comment(2)
just remove background tag in xml from all fragment layout and give background tag to container (main activity layout)..Ambur
@Ambur Thanks alot, but unfortunately, its not working. I added my main.xml for more informationJaney
B
14

you can use the following property for all your root layouts, views, fragments, containers etc:

android:background="@android:color/transparent"

If you want to display the background of your activity only then all the views should be transparent.

For this you have to set android:background="@android:color/transparent" for each view other than the MainActivity's backgroud. i.e background for

  • the Fragments
  • their child Views
  • the ListView(s)
  • and ListView's items / childs etc.

In short each view must have a transparent background so that the Activities' background is not overlapped and visible to the user.

Bronchiectasis answered 24/6, 2014 at 9:5 Comment(1)
Thanks alot, but unfortunately, its not working. I added my main.xml for more informationJaney
M
0

Set your background to null or color/transparent:

android:background="@null"

or

android:background="@android:color/transparent"
Milling answered 9/4, 2018 at 10:1 Comment(0)
S
0

Use this in xml:

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#00E6CECE"
        android:id="@+id/fragment_container" />

And in java use this:

findViewById(R.id.fragment_container).setBackgroundColor(Color.WHITE);
Sinusoidal answered 5/3, 2020 at 6:7 Comment(0)
P
-2

It too easy, just set background as #00000000.

Or go to design tab and set background to #00000000.

Pharyngitis answered 28/5, 2016 at 3:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.