Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

AppCompatDelegate

abstract class AppCompatDelegate
kotlin.Any
   ↳ androidx.appcompat.app.AppCompatDelegate

This class represents a delegate which you can use to extend AppCompat's support to any android.app.Activity.

When using an AppCompatDelegate, you should call the following methods instead of the android.app.Activity method of the same name:

The following methods should be called from the android.app.Activity method of the same name:

An Activity can only be linked with one AppCompatDelegate instance, therefore the instance returned from create(Activity, AppCompatCallback) should be retained until the Activity is destroyed.

Summary

Constants
static Int

Mode which uses the system's night mode setting to determine if it is night or not.

static Int

Night mode which switches between dark and light mode depending on the time of day (dark at night, light in the day).

static Int

static Int

Night mode which uses always uses a light mode, enabling notnight qualified resources regardless of the time.

static Int

Night mode which uses always uses a dark mode, enabling night qualified resources regardless of the time.

static Int

Night mode which uses a dark mode when the system's 'Battery Saver' feature is enabled, otherwise it uses a 'light mode'.

static Int

An unspecified mode for night mode.

static Int

Flag for enabling the support Action Bar.

static Int

Flag for requesting an support Action Bar that overlays window content.

static Int

Flag for specifying the behavior of action modes when an Action Bar is not present.

Public methods
abstract Unit

Should be called instead of Activity#setContentView(android.view.View)}

abstract Unit
setContentView(@LayoutRes resId: Int)

Should be called instead of Activity#setContentView(int)}

abstract Unit

Should be called instead of Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}

abstract Unit

Should be called from Activity#onPostResume()

abstract Unit

Allows AppCompat to save instance state.

abstract Unit

Should be called from Activity.onStop()

abstract T?
findViewById(@IdRes id: Int)

Finds a view that was identified by the id attribute from the XML that was processed in onCreate.

abstract Boolean

Enable extended window features.

abstract View!
createView(@Nullable parent: View?, name: String!, @NonNull context: Context, @NonNull attrs: AttributeSet)

This should be called from a LayoutInflater.Factory2 in order to return tint-aware widgets.

abstract Unit
setSupportActionBar(@Nullable toolbar: Toolbar?)

Set a Toolbar to act as the ActionBar for this delegate.

abstract Unit
onCreate(savedInstanceState: Bundle!)

Should be called from Activity.onCreate().

abstract Unit

Should be called instead of Activity#addContentView(android.view.View, android.view.ViewGroup.LayoutParams)}

open Int

Returns the night mode previously set via getLocalNightMode().

abstract Unit

Should be called from Activity#onConfigurationChanged

abstract Unit
setTitle(@Nullable title: CharSequence?)

Should be called from Activity#onTitleChanged(CharSequence, int)}

abstract Unit

Whether AppCompat handles any native action modes itself.

abstract Boolean
hasWindowFeature(featureId: Int)

Query for the availability of a certain feature.

abstract Unit

Should be called from Activity#onStart() Activity.

abstract Unit
onPostCreate(savedInstanceState: Bundle!)

Should be called from Activity#onPostCreate(android.os.Bundle)

abstract Boolean

Returns whether AppCompat handles any native action modes itself.

abstract Unit

Should be called from Activity#invalidateOptionsMenu()} or FragmentActivity#supportInvalidateOptionsMenu().

abstract MenuInflater!

Return the value of this call from your Activity#getMenuInflater()

abstract Unit

Override the night mode used for this delegate's host component.

open Unit

Should be called from Activity#attachBaseContext(Context)

abstract ActionMode?

Start an action mode.

abstract ActionBar?

Support library version of Activity#getActionBar.

abstract Unit

Installs AppCompat's android.view.LayoutInflater Factory so that it can replace the framework widgets with compatible tinted versions.

abstract Boolean

Allow AppCompat to apply the night and notnight resource qualifiers.

abstract ActionBarDrawerToggle.Delegate?

Returns an ActionBarDrawerToggle.Delegate which can be returned from your Activity if it implements ActionBarDrawerToggle.DelegateProvider.

open Unit
setTheme(@StyleRes themeResId: Int)

This should be called from Activity#setTheme(int) to notify AppCompat of what the current theme resource id is.

abstract Unit

Should be called from Activity#onDestroy()

open static AppCompatDelegate
create(@NonNull activity: Activity, @Nullable callback: AppCompatCallback?)

Create an androidx.appcompat.app.AppCompatDelegate to use with activity.

open static AppCompatDelegate
create(@NonNull dialog: Dialog, @Nullable callback: AppCompatCallback?)

Create an androidx.appcompat.app.AppCompatDelegate to use with dialog.

open static AppCompatDelegate
create(@NonNull context: Context, @NonNull window: Window, @Nullable callback: AppCompatCallback?)

Create an androidx.appcompat.app.AppCompatDelegate to use with a context and a window.

open static Unit

Sets the default night mode.

open static Int

Returns the default night mode.

open static Unit

Sets whether vector drawables on older platforms (< API 21) can be used within android.graphics.drawable.DrawableContainer resources.

open static Boolean

Returns whether vector drawables on older platforms (< API 21) can be accessed from within resources.

Constants

MODE_NIGHT_FOLLOW_SYSTEM

static val MODE_NIGHT_FOLLOW_SYSTEM: Int

Mode which uses the system's night mode setting to determine if it is night or not.

Value: -1

MODE_NIGHT_AUTO_TIME

static val MODE_NIGHT_AUTO_TIME: Int

Night mode which switches between dark and light mode depending on the time of day (dark at night, light in the day).

The calculation used to determine whether it is night or not makes use of the location APIs (if this app has the necessary permissions). This allows us to generate accurate sunrise and sunset times. If this app does not have permission to access the location APIs then we use hardcoded times which will be less accurate.

Value: 0

MODE_NIGHT_AUTO

static val MODE_NIGHT_AUTO: Int
Value: MODE_NIGHT_AUTO_TIME

MODE_NIGHT_NO

static val MODE_NIGHT_NO: Int

Night mode which uses always uses a light mode, enabling notnight qualified resources regardless of the time.

Value: 1

MODE_NIGHT_YES

static val MODE_NIGHT_YES: Int

Night mode which uses always uses a dark mode, enabling night qualified resources regardless of the time.

Value: 2

MODE_NIGHT_AUTO_BATTERY

static val MODE_NIGHT_AUTO_BATTERY: Int

Night mode which uses a dark mode when the system's 'Battery Saver' feature is enabled, otherwise it uses a 'light mode'. This mode can help the device to decrease power usage, depending on the display technology in the device. Please note: this mode should only be used when running on devices which do not provide a similar device-wide setting.

Value: 3

MODE_NIGHT_UNSPECIFIED

static val MODE_NIGHT_UNSPECIFIED: Int

An unspecified mode for night mode. This is primarily used with setLocalNightMode(int), to allow the default night mode to be used. If both the default and local night modes are set to this value, then the default value of MODE_NIGHT_FOLLOW_SYSTEM is applied.

Value: -100

FEATURE_SUPPORT_ACTION_BAR

static val FEATURE_SUPPORT_ACTION_BAR: Int

Flag for enabling the support Action Bar.

This is enabled by default for some devices. The Action Bar replaces the title bar and provides an alternate location for an on-screen menu button on some devices.

Value: 100 + WindowCompat.FEATURE_ACTION_BAR

FEATURE_SUPPORT_ACTION_BAR_OVERLAY

static val FEATURE_SUPPORT_ACTION_BAR_OVERLAY: Int

Flag for requesting an support Action Bar that overlays window content. Normally an Action Bar will sit in the space above window content, but if this feature is requested along with FEATURE_SUPPORT_ACTION_BAR it will be layered over the window content itself. This is useful if you would like your app to have more control over how the Action Bar is displayed, such as letting application content scroll beneath an Action Bar with a transparent background or otherwise displaying a transparent/translucent Action Bar over application content.

This mode is especially useful with View.SYSTEM_UI_FLAG_FULLSCREEN, which allows you to seamlessly hide the action bar in conjunction with other screen decorations. When an ActionBar is in this mode it will adjust the insets provided to View.fitSystemWindows(Rect) to include the content covered by the action bar, so you can do layout within that space.

Value: 100 + WindowCompat.FEATURE_ACTION_BAR_OVERLAY

FEATURE_ACTION_MODE_OVERLAY

static val FEATURE_ACTION_MODE_OVERLAY: Int

Flag for specifying the behavior of action modes when an Action Bar is not present. If overlay is enabled, the action mode UI will be allowed to cover existing window content.

Value: WindowCompat.FEATURE_ACTION_MODE_OVERLAY

Public methods

setContentView

abstract fun setContentView(v: View!): Unit

Should be called instead of Activity#setContentView(android.view.View)}

setContentView

abstract fun setContentView(@LayoutRes resId: Int): Unit

Should be called instead of Activity#setContentView(int)}

setContentView

abstract fun setContentView(v: View!, lp: LayoutParams!): Unit

Should be called instead of Activity#setContentView(android.view.View, android.view.ViewGroup.LayoutParams)}

onPostResume

abstract fun onPostResume(): Unit

Should be called from Activity#onPostResume()

onSaveInstanceState

abstract fun onSaveInstanceState(outState: Bundle!): Unit

Allows AppCompat to save instance state.

onStop

abstract fun onStop(): Unit

Should be called from Activity.onStop()

findViewById

@Nullable abstract fun <T : View!> findViewById(@IdRes id: Int): T?

Finds a view that was identified by the id attribute from the XML that was processed in onCreate.

Return
T?: The view if found or null otherwise.

requestWindowFeature

abstract fun requestWindowFeature(featureId: Int): Boolean

Enable extended window features. This should be called instead of android.app.Activity#requestWindowFeature(int) or getWindow().requestFeature().

Parameters
featureId Int: The desired feature as defined in android.view.Window.
Return
Boolean: Returns true if the requested feature is supported and now enabled.

createView

abstract fun createView(@Nullable parent: View?, name: String!, @NonNull context: Context, @NonNull attrs: AttributeSet): View!

This should be called from a LayoutInflater.Factory2 in order to return tint-aware widgets.

This is only needed if you are using your own LayoutInflater factory, and have therefore not installed the default factory via installViewFactory().

setSupportActionBar

abstract fun setSupportActionBar(@Nullable toolbar: Toolbar?): Unit

Set a Toolbar to act as the ActionBar for this delegate.

When set to a non-null value the getSupportActionBar() ()} method will return an ActionBar object that can be used to control the given toolbar as if it were a traditional window decor action bar. The toolbar's menu will be populated with the Activity's options menu and the navigation button will be wired through the standard home menu select action.

In order to use a Toolbar within the Activity's window content the application must not request the window feature FEATURE_SUPPORT_ACTION_BAR.

Parameters
toolbar Toolbar?: Toolbar to set as the Activity's action bar, or null to clear it

onCreate

abstract fun onCreate(savedInstanceState: Bundle!): Unit

Should be called from Activity.onCreate().

This should be called before super.onCreate() as so:

 protected void onCreate(Bundle savedInstanceState) { getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); // ... } 

addContentView

abstract fun addContentView(v: View!, lp: LayoutParams!): Unit

Should be called instead of Activity#addContentView(android.view.View, android.view.ViewGroup.LayoutParams)}

getLocalNightMode

open fun getLocalNightMode(): Int

Returns the night mode previously set via getLocalNightMode().

onConfigurationChanged

abstract fun onConfigurationChanged(newConfig: Configuration!): Unit

Should be called from Activity#onConfigurationChanged

setTitle

abstract fun setTitle(@Nullable title: CharSequence?): Unit

Should be called from Activity#onTitleChanged(CharSequence, int)}

setHandleNativeActionModesEnabled

abstract fun setHandleNativeActionModesEnabled(enabled: Boolean): Unit

Whether AppCompat handles any native action modes itself.

This methods only takes effect on android.os.Build.VERSION_CODES#ICE_CREAM_SANDWICH and above.

Parameters
enabled Boolean: whether AppCompat should handle native action modes.

hasWindowFeature

abstract fun hasWindowFeature(featureId: Int): Boolean

Query for the availability of a certain feature.

This should be called instead of android.view.Window#hasFeature(int).

Parameters
featureId Int: The feature ID to check
Return
Boolean: true if the feature is enabled, false otherwise.

onStart

abstract fun onStart(): Unit

Should be called from Activity#onStart() Activity.onStart()}

onPostCreate

abstract fun onPostCreate(savedInstanceState: Bundle!): Unit

Should be called from Activity#onPostCreate(android.os.Bundle)

isHandleNativeActionModesEnabled

abstract fun isHandleNativeActionModesEnabled(): Boolean

Returns whether AppCompat handles any native action modes itself.

Return
Boolean: true if AppCompat should handle native action modes.

invalidateOptionsMenu

abstract fun invalidateOptionsMenu(): Unit

Should be called from Activity#invalidateOptionsMenu()} or FragmentActivity#supportInvalidateOptionsMenu().

getMenuInflater

abstract fun getMenuInflater(): MenuInflater!

Return the value of this call from your Activity#getMenuInflater()

setLocalNightMode

abstract fun setLocalNightMode(mode: Int): Unit

Override the night mode used for this delegate's host component. This method only takes effect for those situations where applyDayNight() works.

As this will call applyDayNight(), the host component might be recreated automatically.

It is not recommended to use this method on a delegate attached to a Dialog. Dialogs use the host Activity as their context, resulting in the dialog's night mode overriding the Activity's night mode.

attachBaseContext

open fun attachBaseContext(context: Context!): Unit

Should be called from Activity#attachBaseContext(Context)

startSupportActionMode

@Nullable abstract fun startSupportActionMode(@NonNull callback: ActionMode.Callback): ActionMode?

Start an action mode.

Parameters
callback ActionMode.Callback: Callback that will manage lifecycle events for this context mode
Return
ActionMode?: The ContextMode that was started, or null if it was canceled

getSupportActionBar

@Nullable abstract fun getSupportActionBar(): ActionBar?

Support library version of Activity#getActionBar.

Return
ActionBar?: AppCompat's action bar, or null if it does not have one.

installViewFactory

abstract fun installViewFactory(): Unit

Installs AppCompat's android.view.LayoutInflater Factory so that it can replace the framework widgets with compatible tinted versions. This should be called before super.onCreate() as so:

 protected void onCreate(Bundle savedInstanceState) { getDelegate().installViewFactory(); getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); // ... } 
If you are using your own Factory or Factory2 then you can omit this call, and instead call createView(android.view.View, String, android.content.Context, android.util.AttributeSet) from your factory to return any compatible widgets.

applyDayNight

abstract fun applyDayNight(): Boolean

Allow AppCompat to apply the night and notnight resource qualifiers.

Doing this enables the androidx.appcompat.R.style#Theme_AppCompat_DayNight family of themes to work, using the computed twilight to automatically select a dark or light theme.

You can override the night mode using setLocalNightMode(int).

This only works on devices running ICE_CREAM_SANDWICH and above.

If this is called after the host component has been created, the component will either be automatically recreated or its Configuration updated. Which one depends on how the component is setup (via android:configChanges or similar).

You can be notified when the night changes by overriding the AppCompatActivity#onNightModeChanged(int) method.

Return
Boolean: true if the night mode was applied, false if not

getDrawerToggleDelegate

@Nullable abstract fun getDrawerToggleDelegate(): ActionBarDrawerToggle.Delegate?

Returns an ActionBarDrawerToggle.Delegate which can be returned from your Activity if it implements ActionBarDrawerToggle.DelegateProvider.

setTheme

open fun setTheme(@StyleRes themeResId: Int): Unit

This should be called from Activity#setTheme(int) to notify AppCompat of what the current theme resource id is.

onDestroy

abstract fun onDestroy(): Unit

Should be called from Activity#onDestroy()

create

@NonNull open static fun create(@NonNull activity: Activity, @Nullable callback: AppCompatCallback?): AppCompatDelegate

Create an androidx.appcompat.app.AppCompatDelegate to use with activity.

Parameters
callback Activity: An optional callback for AppCompat specific events

create

@NonNull open static fun create(@NonNull dialog: Dialog, @Nullable callback: AppCompatCallback?): AppCompatDelegate

Create an androidx.appcompat.app.AppCompatDelegate to use with dialog.

Parameters
callback Dialog: An optional callback for AppCompat specific events

create

@NonNull open static fun create(@NonNull context: Context, @NonNull window: Window, @Nullable callback: AppCompatCallback?): AppCompatDelegate

Create an androidx.appcompat.app.AppCompatDelegate to use with a context and a window.

Parameters
callback Context: An optional callback for AppCompat specific events

setDefaultNightMode

open static fun setDefaultNightMode(mode: Int): Unit

Sets the default night mode. This is used across all activities/dialogs but can be overridden locally via setLocalNightMode(int).

This method only takes effect for those situations where applyDayNight() works. Defaults to MODE_NIGHT_FOLLOW_SYSTEM.

This only takes effect for components which are created after the call. Any components which are already open will not be updated.

getDefaultNightMode

open static fun getDefaultNightMode(): Int

Returns the default night mode.

setCompatVectorFromResourcesEnabled

open static fun setCompatVectorFromResourcesEnabled(enabled: Boolean): Unit

Sets whether vector drawables on older platforms (< API 21) can be used within android.graphics.drawable.DrawableContainer resources.

When enabled, AppCompat can intercept some drawable inflation from the framework, which enables implicit inflation of vector drawables within android.graphics.drawable.DrawableContainer resources. You can then use those drawables in places such as android:src on android.widget.ImageView, or android:drawableLeft on android.widget.TextView. Example usage:

 <selector xmlns:android="..."> <item android:state_checked="true" android:drawable="@drawable/vector_checked_icon" /> <item android:drawable="@drawable/vector_icon" /> </selector> <TextView ... android:drawableLeft="@drawable/vector_state_list_icon" /> 

This feature defaults to disabled, since enabling it can cause issues with memory usage, and problems updating Configuration instances. If you update the configuration manually, then you probably do not want to enable this. You have been warned.

Even with this disabled, you can still use vector resources through androidx.appcompat.widget.AppCompatImageView#setImageResource(int) and its app:srcCompat attribute. They can also be used in anything which AppCompat inflates for you, such as menu resources.

Please note: this only takes effect in Activities created after this call.

isCompatVectorFromResourcesEnabled

open static fun isCompatVectorFromResourcesEnabled(): Boolean

Returns whether vector drawables on older platforms (< API 21) can be accessed from within resources.