Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android set transparent background for a fragment

Tags:

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?

like image 582
amodkanthe Avatar asked Feb 25 '18 13:02

amodkanthe


2 Answers

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 
like image 194
Manohar Avatar answered Sep 17 '22 13:09

Manohar


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); 
like image 36
Zar E Ahmer Avatar answered Sep 19 '22 13:09

Zar E Ahmer