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?
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
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With