ContextThemeWrapper

public class ContextThemeWrapper
extends ContextWrapper

java.lang.Object
   ↳ android.content.Context
     ↳ android.content.ContextWrapper
       ↳ androidx.appcompat.view.ContextThemeWrapper


A context wrapper that allows you to modify or replace the theme of the wrapped context.

Summary

Inherited constants

Public constructors

ContextThemeWrapper()

Creates a new context wrapper with no theme and no base context.

ContextThemeWrapper(Context base, int themeResId)

Creates a new context wrapper with the specified theme.

ContextThemeWrapper(Context base, Resources.Theme theme)

Creates a new context wrapper with the specified theme.

Public methods

void applyOverrideConfiguration(Configuration overrideConfiguration)

Call to set an "override configuration" on this context -- this is a configuration that replies one or more values of the standard configuration that is applied to the context.

AssetManager getAssets()
Resources getResources()
Object getSystemService(String name)
Resources.Theme getTheme()
int getThemeResId()

Returns the resource ID of the theme that is to be applied on top of the base context's theme.

void setTheme(int resid)

Protected methods

void attachBaseContext(Context newBase)
void onApplyThemeResource(Resources.Theme theme, int resid, boolean first)

Called by setTheme(int) and getTheme() to apply a theme resource to the current Theme object.

Inherited methods

Public constructors

ContextThemeWrapper

public ContextThemeWrapper ()

Creates a new context wrapper with no theme and no base context.

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

ContextThemeWrapper

public ContextThemeWrapper (Context base, 
                int 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 Context: the base context

themeResId int: 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, int), the theme passed to this constructor will completely replace the base context's theme.

Parameters
base Context: the base context

theme Resources.Theme: the theme against which resources should be inflated

Public methods

applyOverrideConfiguration

public void applyOverrideConfiguration (Configuration overrideConfiguration)

Call to set an "override configuration" on this context -- this is a configuration that replies one or more values of the standard configuration that is applied to the context. See Context.createConfigurationContext(Configuration) for more information.

This method can only be called once, and must be called before any calls to getResources() or getAssets() are made.

Parameters
overrideConfiguration Configuration

getAssets

public AssetManager getAssets ()

Returns
AssetManager

getResources

public Resources getResources ()

Returns
Resources

getSystemService

public Object getSystemService (String name)

Parameters
name String

Returns
Object

getTheme

public Resources.Theme getTheme ()

Returns
Resources.Theme

getThemeResId

public int getThemeResId ()

Returns the resource ID of the theme that is to be applied on top of the base context's theme.

Returns
int

setTheme

public void setTheme (int resid)

Parameters
resid int

Protected methods

attachBaseContext

protected void attachBaseContext (Context newBase)

Parameters
newBase Context

onApplyThemeResource

protected void onApplyThemeResource (Resources.Theme theme, 
                int resid, 
                boolean first)

Called by setTheme(int) and getTheme() to apply a theme resource to the current Theme object. Can override to change the default (simple) behavior. This method will not be called in multiple threads simultaneously.

Parameters
theme Resources.Theme: The Theme object being modified.

resid int: The theme style resource being applied to theme.

first boolean: Set to true if this is the first time a style is being applied to theme.