Android set transparent background for a fragment

amodkanthe picture amodkanthe · Feb 25, 2018 · Viewed 14.3k times · Source

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?

Answer

AndroidGeek picture AndroidGeek · Mar 6, 2018

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