alexfu
3/3/2015 - 12:12 PM

Change View styles on the fly with ContextThemeWrapper

Change View styles on the fly with ContextThemeWrapper

<resources>
  <style name="Theme.Main" parent="Theme.AppCompat.Light.NoActionBar"/>
  <style name="Theme.Main.Inverse" parent="Theme.AppCompat.NoActionBar"/>
</resources>
public class MyFragment {
  
  private Context context;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    // Where the magic happens
    context = new ContextThemeWrapper(getActivity(), R.style.Theme_Main_Inverse);
  }
  
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
    inflater = LayoutInflater.from(context);
    View view = inflater.inflate(R.layout.fragment_myprofile, container, false);
    ButterKnife.inject(this, view);
    bindViews();
    return view;
  }
}