0

How to set a theme to a fragment?

The codes does not work for me below, or somewhere I'm wrong?

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);  
} 
YiFeng
  • 367
  • 2
  • 4
  • 15
  • 1
    Possible duplicate of [Set theme for a Fragment](http://stackoverflow.com/questions/9469174/set-theme-for-a-fragment) – Janki Gadhiya Jun 07 '16 at 07:14

1 Answers1

0

The above code will change the theme of widgets inside your fragment layout but it will not change the theme of ActionBar and Status Bar.

Add a SeekBar and a Switch to your fragment layout and see it's theme has been changed or not.

Adnan Khan
  • 463
  • 3
  • 12