Android set transparent background for a fragment
Asked Answered
H

12

22

In my app I have single activity and and all other fragments

I am setting background for activity from style.xml as below

<item name="android:windowBackground">@color/very_light_gray</item>

Now for only a perticular Fragment I want to set background transparent and I am not able to do that tried below code in Fragment did not work for me

  @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

// create ContextThemeWrapper from the original Activity Context with the custom theme
final Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.yourCustomTheme);

// clone the inflater using the ContextThemeWrapper
LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);

// inflate the layout using the cloned inflater, not default inflater
return localInflater.inflate(R.layout.yourLayout, container, false);
}

Any idea how to do it?

Hazan answered 25/2, 2018 at 13:43 Comment(6)
Want to set transparent background for a backgroundHazan
<item name="android:windowBackground">@color/very_light_gray</item> is set in style and this theme is applied to activity so all fragments have background very_light_gray and now for A perticular fragment I want to set background transparent not able to do thatHazan
background property of xml should make job... show us your fragment and activity layout, pleasePanier
What do you mean? Fragment is transparent unless you inflate a layout with a visible background. So, what exactly are you trying to achieve? A screenshot of what you have and what you want may help.Snailpaced
Alteady inflating fragment with transparent backgroundHazan
have you found a solution?Lampkin
U
7

This is not perfect solution but it works

Instead of using Fragment use DialogFragment

 //exploreCategory is argument
class MyFragment(val exploreCategory: ExploreCategory) : DialogFragment() {

    override fun onStart() {
      super.onStart()
      setWindowParams()
    }

   private fun setWindowParams(){
      dialog?.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
      dialog?.window?.setLayout(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
      )
    }

}

Remain code is same like you write in fragment

For displaying use below code in fragment, if displaying in activity then use fragmentManager

MyFragment(exploreCategory).show(childFragmentManager,null)
//exploreCategory is argument, you can choose to send no arguments
Unify answered 9/6, 2020 at 12:30 Comment(2)
Best solution. Should be renamed from MyFragment to TransparentFragment. As in open class TransparentFragment : DialogFragment() Elburr
It will draw gray above your fragment.Trematode
C
3

create a callback and implement it in Acitvity

interface OnFragmentDisplay{
  onFragmentDisplay();
}

when this fragment displays update Activity background to transparent ..or set it theme there in activity

See this link and this may help

have you tried this

fragment.getView().setBackgroundColor(Color.WHITE);
Clause answered 6/3, 2018 at 9:39 Comment(0)
O
1

use null for color in style app

 <item name="android:windowBackground">null</item>
Operand answered 3/3, 2018 at 9:27 Comment(1)
Please read question again my activity has window background I want to know how can I apply theme to a fragmentHazan
C
1

Your Activity container layout background should be transparent. Then set background color for Fragment's layouts. And for specific fragment's layout, set transparent color to get your required scenarios.

Cerberus answered 6/3, 2018 at 6:12 Comment(1)
Please read question again my activity has window background I want to know how can I apply theme to a fragmentHazan
F
1

Simple way to do is set background color for root layout of that particular fragment as follows

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

Fremantle answered 6/3, 2018 at 9:33 Comment(1)
Please read question again my activity has window background I want to know how can I apply theme to a fragmentHazan
S
1

So, assuming you want the activity and fragment to be transparent for a particular fragment (It's not quite clear if this is what you want from your question, but I'm going to assume it is)..you need to set the "host" activity background to transparent when the "transparent" fragment is attached to it. When any other fragment attaches to it, you will need to reset the activity background or it will stay transparent..

There are a few ways of doing this, but I'm going to pick on something "fairly" straightforward:

in your transparent fragment:

@Override
public void onStart() {
    super.onStart();
    // I'm using null here because drawing nothing is faster than drawing transparent pixels.
    getActivity().getWindow().setBackgroundDrawable(null);
    getView().setBackground(null);
}
    
@Override
public void onStop() {
    super.onStop();
    getActivity().getWindow().setBackgroundDrawable(new ColorDrawable(@color/very_light_gray));
}

Not entirely sure if this is the effect you want.

Good luck. Kind regards, CJ

Satisfy answered 6/3, 2018 at 11:1 Comment(0)
H
0

You have to set your Activity also transparent.

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

Since your activity has @color/very_light_gray, even if you set your Fragment as @android:color/transparent it will not work.

Each view must have a transparent background to avoid overlapping.

Hamner answered 6/3, 2018 at 5:43 Comment(1)
Please read question again my activity has window background I want to know how can I apply theme to a fragmentHazan
V
0

You can use something like the code below in your activity while instantiating the fragment. In place of Color.White, you can use your own color.

fragment.getView().setBackgroundColor(Color.WHITE);
Vacuva answered 6/3, 2018 at 6:13 Comment(1)
Please read question again my activity has window background I want to know how can I apply theme to a fragmentHazan
S
0

Add this single line in your code

getActivity().getWindow().setBackgroundDrawableResource(R.drawable.my_drawable);
Savoie answered 6/3, 2018 at 11:19 Comment(0)
E
0

Set your activity layout's style as

<style name="RootLayout" parent="AppTheme">
    <item name="android:background">@drawable/app_transparent_background</item>
</style>

and set it in activity's parent layout as

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    style="@style/RootLayout"
    ...>

and in other fragments which you want to set white or other colored background, set different style in those fragment's main layout.

It will work

Estremadura answered 6/3, 2018 at 12:19 Comment(0)
D
-1

You've just set background for layout fragment below code:

<LinearLayout
...
android:background="@android:color/transparent"/>
Daff answered 25/2, 2018 at 13:47 Comment(1)
Please read question again my activity has window background I want to know how can I apply theme to a fragmentHazan
S
-1

two steps:

first step - create a view (in an XML file) with a transparent background.

in android studio, locate "res" folder, right click it, new -> android resource file. Than in the appearing dialog put:

File name: {put here anything. for example - "my_fragment" (without the quotes)} notice that only lowercase letters, digits and underscores are ok

Resource type: Layout

Root element: LinearLayout (you can put here anything else after you will learn to know XML layouts better)

Source set: main

Directory name: layout.

In the window that will appear switch to "text" mode instead of "design" (look for the tabs in the lower-left side of the screen).

than copy-paste this instead of the file's content (it's very similar, we just add here the "transparent" background):

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/transparent"
    >

</LinearLayout>

second step - inflate this layout in your fragment.

create your fragment class that extends Fragment and override the method onCreateView(), inside that method inflate your view and return it:

public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    // assuming you named your xml file "my_fragment":
    // for other names replace R.layout.my_fragment with  R.layout.put_your_name_here
    View view = inflater.inflate(R.layout.my_fragment, container, false);
    return view;
}

of course, don't forget to create an instance of your fragment in the activity!

Spadefish answered 25/2, 2018 at 14:7 Comment(1)
Please read question again my activity has window background I want to know how can I apply theme to a fragmentHazan

© 2022 - 2024 — McMap. All rights reserved.