Class ContextThemeWrapper


public class ContextThemeWrapper extends ContextWrapper
A context wrapper that allows you to modify or replace the theme of the wrapped context.
  • Constructor Details

    • ContextThemeWrapper

      public ContextThemeWrapper()
      Creates a new context wrapper with no theme and no base context.

      Note: A base context must be attached using ContextWrapper.attachBaseContext(Context) before calling any other method on the newly constructed context wrapper.

    • ContextThemeWrapper

      public ContextThemeWrapper(Context base, @StyleRes ResourceId themeResId)
      Creates a new context wrapper with the specified theme.

      The specified theme will be applied on top of the base context's theme. Any attributes not explicitly defined in the theme identified by themeResId will retain their original values.

      Parameters:
      base - the base context
      themeResId - the resource ID of the theme to be applied on top of the base context's theme
    • ContextThemeWrapper

      public ContextThemeWrapper(Context base, Resources.Theme theme)
      Creates a new context wrapper with the specified theme.

      Unlike ContextThemeWrapper(Context, ResourceId), the theme passed to this constructor will completely replace the base context's theme and the Resources object.

      Parameters:
      base - the base context
      theme - the theme against which resources should be inflated
  • Method Details

    • getResources

      public Resources getResources()
      Description copied from class: Context
      Returns a Resources instance for the application's environment.

      Note: For a Context object, the implementation of this method should always return the same Resources object, even if a resource reload occurs.

      Overrides:
      getResources in class ContextWrapper
      Returns:
      a Resources instance for the application's environment
    • setTheme

      public void setTheme(@Nullable @StyleRes ResourceId resId)
      Description copied from class: Context
      Reset the base theme for this context. Note that this should be called before any views are instantiated in the Context.
      Overrides:
      setTheme in class ContextWrapper
      Parameters:
      resId - The style resource describing the theme.
    • getTheme

      public Resources.Theme getTheme()
      Description copied from class: Context
      Return the Theme object associated with this Context.

      Note: For a Context object, the implementation of this method should always return the same Theme object, even if a resource reload or theme reset occurs. And the object returned by Resources.Theme.getResources() should be consistent with Context.getResources(). Calling this method is not particularly fast, so it is recommended to cache it in a local variable.

      Overrides:
      getTheme in class ContextWrapper
    • onApplyThemeResource

      protected void onApplyThemeResource(@NonNull Resources.Theme theme, @Nullable @StyleRes ResourceId resId, boolean first)
      Called by setTheme(icyllis.modernui.resources.ResourceId) and getTheme() to apply a theme resource to the current Theme object. May be overridden to change the default (simple) behavior. This method will not be called in multiple threads simultaneously.
      Parameters:
      theme - the theme being modified
      resId - the style resource being applied to theme
      first - true if this is the first time a style is being applied to theme