Change theme on Fragment change

Viewed 1605

I need to change fragment theme dynamically, i try this code but it not work on onCreate or onCreateView()

getActivity().setTheme(R.style.ActionMode);

How i can set theme from fragment?

1 Answers

In the onCreateView() method, you have to create ContextThemeWrapper and inflate style of theme from it, like below:

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    //create ContextThemeWrapper from the original Activity Context with the custom theme
    Context context = new ContextThemeWrapper(getActivity(), R.style.your_Custom_Theme);
    //clone the inflater using the ContextThemeWrapper
    LayoutInflater localInflater = inflater.cloneInContext(context);
    //inflate using the cloned inflater, not the passed in default
    return localInflater.inflate(R.layout.your_layout, container, false);
}
Related