Damirchi Damirchi - 1 month ago 10
Android Question

Change fragment background on create view dynamically

I want to change fragment background on load dynamically, Below code do that for me, But I have an issue on run-time and that is background changes happen after showing the fragment with default background.

for example when I want to swipe to load another fragment, On transition fragment appears with default background setting and after loading fragment and waiting a little moment that codes work at first time load.

I want to know where I'm wrong

@Override
public void setUserVisibleHint(boolean visible){
super.setUserVisibleHint(visible);
View view=RegisterFragment.this.getView();
if (view!=null)
{
RelativeLayout layout= (RelativeLayout) view.findViewById(R.id.back_layout);
layout.setBackgroundResource(Application.color);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_register, container, false);
view.findViewById(R.id.back_layout).setBackgroundResource(Application.color);
return view;
}

Jai Jai
Answer

You could override below method which will help you to update UI run-time while loading fragment:

@Override
    public void setMenuVisibility(boolean menuVisible) {

       layout.setBackgroundResource(Application.color);

}