AppCompatDelegate
public
abstract
class
AppCompatDelegate
extends Object
java.lang.Object | |
↳ | androidx.appcompat.app.AppCompatDelegate |
This class represents a delegate which you can use to extend AppCompat's support to any
Activity
.
When using an AppCompatDelegate
, you should call the following methods instead of the
Activity
method of the same name:
addContentView(android.view.View, android.view.ViewGroup.LayoutParams)
setContentView(int)
setContentView(android.view.View)
setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
requestWindowFeature(int)
hasWindowFeature(int)
invalidateOptionsMenu()
startSupportActionMode(androidx.appcompat.view.ActionMode.Callback)
setSupportActionBar(androidx.appcompat.widget.Toolbar)
getSupportActionBar()
getMenuInflater()
findViewById(int)
The following methods should be called from the Activity
method of the
same name:
onCreate(android.os.Bundle)
onPostCreate(android.os.Bundle)
onConfigurationChanged(android.content.res.Configuration)
onStart()
onStop()
onPostResume()
onSaveInstanceState(Bundle)
setTitle(CharSequence)
onStop()
onDestroy()
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 | |
---|---|
int |
FEATURE_ACTION_MODE_OVERLAY
Flag for specifying the behavior of action modes when an Action Bar is not present. |
int |
FEATURE_SUPPORT_ACTION_BAR
Flag for enabling the support Action Bar. |
int |
FEATURE_SUPPORT_ACTION_BAR_OVERLAY
Flag for requesting an support Action Bar that overlays window content. |
int |
MODE_NIGHT_AUTO
This constant is deprecated.
Use |
int |
MODE_NIGHT_AUTO_BATTERY
Night mode which uses a dark mode when the system's 'Battery Saver' feature is enabled, otherwise it uses a 'light mode'. |
int |
MODE_NIGHT_AUTO_TIME
This constant is deprecated.
Automatic switching of dark/light based on the current time is deprecated.
Considering using an explicit setting, or |
int |
MODE_NIGHT_FOLLOW_SYSTEM
Mode which uses the system's night mode setting to determine if it is night or not. |
int |
MODE_NIGHT_NO
Night mode which uses always uses a light mode, enabling |
int |
MODE_NIGHT_UNSPECIFIED
An unspecified mode for night mode. |
int |
MODE_NIGHT_YES
Night mode which uses always uses a dark mode, enabling |
Public methods | |
---|---|
abstract
void
|
addContentView(View v, ViewGroup.LayoutParams lp)
Should be called instead of
|
abstract
boolean
|
applyDayNight()
Applies the currently selected night mode to this delegate's host component. |
void
|
attachBaseContext(Context context)
Should be called from |
static
AppCompatDelegate
|
create(Activity activity, AppCompatCallback callback)
Create an |
static
AppCompatDelegate
|
create(Dialog dialog, AppCompatCallback callback)
Create an |
static
AppCompatDelegate
|
create(Context context, Activity activity, AppCompatCallback callback)
Create an |
static
AppCompatDelegate
|
create(Context context, Window window, AppCompatCallback callback)
Create an |
abstract
View
|
createView(View parent, String name, Context context, AttributeSet attrs)
This should be called from a
|
abstract
<T extends View>
T
|
findViewById(int id)
Finds a view that was identified by the id attribute from the XML that
was processed in |
static
int
|
getDefaultNightMode()
Returns the default night mode. |
abstract
ActionBarDrawerToggle.Delegate
|
getDrawerToggleDelegate()
Returns an |
int
|
getLocalNightMode()
Returns the night mode previously set via |
abstract
MenuInflater
|
getMenuInflater()
Return the value of this call from your |
abstract
ActionBar
|
getSupportActionBar()
Support library version of |
abstract
boolean
|
hasWindowFeature(int featureId)
Query for the availability of a certain feature. |
abstract
void
|
installViewFactory()
Installs AppCompat's |
abstract
void
|
invalidateOptionsMenu()
Should be called from |
static
boolean
|
isCompatVectorFromResourcesEnabled()
Returns whether vector drawables on older platforms (< API 21) can be accessed from within resources. |
abstract
boolean
|
isHandleNativeActionModesEnabled()
Returns whether AppCompat handles any native action modes itself. |
abstract
void
|
onConfigurationChanged(Configuration newConfig)
Should be called from
|
abstract
void
|
onCreate(Bundle savedInstanceState)
Should be called from |
abstract
void
|
onDestroy()
Should be called from |
abstract
void
|
onPostCreate(Bundle savedInstanceState)
Should be called from |
abstract
void
|
onPostResume()
Should be called from |
abstract
void
|
onSaveInstanceState(Bundle outState)
Allows AppCompat to save instance state. |
abstract
void
|
onStart()
Should be called from |
abstract
void
|
onStop()
Should be called from |
abstract
boolean
|
requestWindowFeature(int featureId)
Enable extended window features. |
static
void
|
setCompatVectorFromResourcesEnabled(boolean enabled)
Sets whether vector drawables on older platforms (< API 21) can be used within
|
abstract
void
|
setContentView(View v)
Should be called instead of |
abstract
void
|
setContentView(int resId)
Should be called instead of |
abstract
void
|
setContentView(View v, ViewGroup.LayoutParams lp)
Should be called instead of
|
static
void
|
setDefaultNightMode(int mode)
Sets the default night mode. |
abstract
void
|
setHandleNativeActionModesEnabled(boolean enabled)
Whether AppCompat handles any native action modes itself. |
abstract
void
|
setLocalNightMode(int mode)
Override the night mode used for this delegate's host component. |
abstract
void
|
setSupportActionBar(Toolbar toolbar)
|
void
|
setTheme(int themeResId)
This should be called from |
abstract
void
|
setTitle(CharSequence title)
Should be called from |
abstract
ActionMode
|
startSupportActionMode(ActionMode.Callback callback)
Start an action mode. |
Inherited methods | |
---|---|
Constants
FEATURE_ACTION_MODE_OVERLAY
public static final int FEATURE_ACTION_MODE_OVERLAY
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.
Constant Value: 10 (0x0000000a)
FEATURE_SUPPORT_ACTION_BAR
public static final int FEATURE_SUPPORT_ACTION_BAR
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.
Constant Value: 108 (0x0000006c)
FEATURE_SUPPORT_ACTION_BAR_OVERLAY
public static final int FEATURE_SUPPORT_ACTION_BAR_OVERLAY
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.
Constant Value: 109 (0x0000006d)
MODE_NIGHT_AUTO
public static final int MODE_NIGHT_AUTO
This constant is deprecated.
Use MODE_NIGHT_AUTO_TIME
instead
Constant Value: 0 (0x00000000)
MODE_NIGHT_AUTO_BATTERY
public static final int MODE_NIGHT_AUTO_BATTERY
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.
See also:
Constant Value: 3 (0x00000003)
MODE_NIGHT_AUTO_TIME
public static final int MODE_NIGHT_AUTO_TIME
This constant is deprecated.
Automatic switching of dark/light based on the current time is deprecated.
Considering using an explicit setting, or MODE_NIGHT_AUTO_BATTERY
.
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.
Constant Value: 0 (0x00000000)
MODE_NIGHT_FOLLOW_SYSTEM
public static final int MODE_NIGHT_FOLLOW_SYSTEM
Mode which uses the system's night mode setting to determine if it is night or not.
See also:
Constant Value: -1 (0xffffffff)
MODE_NIGHT_NO
public static final int MODE_NIGHT_NO
Night mode which uses always uses a light mode, enabling notnight
qualified
resources regardless of the time.
See also:
Constant Value: 1 (0x00000001)
MODE_NIGHT_UNSPECIFIED
public static final int MODE_NIGHT_UNSPECIFIED
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.
See also:
Constant Value: -100 (0xffffff9c)
MODE_NIGHT_YES
public static final int MODE_NIGHT_YES
Night mode which uses always uses a dark mode, enabling night
qualified
resources regardless of the time.
See also:
Constant Value: 2 (0x00000002)
Public methods
addContentView
public abstract void addContentView (View v, ViewGroup.LayoutParams lp)
Should be called instead of
Activity.addContentView(android.view.View, android.view.ViewGroup.LayoutParams)
}
Parameters | |
---|---|
v |
View |
lp |
ViewGroup.LayoutParams |
applyDayNight
public abstract boolean applyDayNight ()
Applies the currently selected night mode to this delegate's host component.
This enables the
Theme.AppCompat.DayNight
family of themes to work, using the specified mode.
You can be notified when the night changes by overriding the
AppCompatActivity.onNightModeChanged(int)
method.
Returns | |
---|---|
boolean |
true if the night mode was applied, false if not |
attachBaseContext
public void attachBaseContext (Context context)
Should be called from Activity.attachBaseContext(Context)
Parameters | |
---|---|
context |
Context |
create
public static AppCompatDelegate create (Activity activity, AppCompatCallback callback)
Create an AppCompatDelegate
to use with activity
.
Parameters | |
---|---|
activity |
Activity |
callback |
AppCompatCallback : An optional callback for AppCompat specific events
|
Returns | |
---|---|
AppCompatDelegate |
create
public static AppCompatDelegate create (Dialog dialog, AppCompatCallback callback)
Create an AppCompatDelegate
to use with dialog
.
Parameters | |
---|---|
dialog |
Dialog |
callback |
AppCompatCallback : An optional callback for AppCompat specific events
|
Returns | |
---|---|
AppCompatDelegate |
create
public static AppCompatDelegate create (Context context, Activity activity, AppCompatCallback callback)
Create an AppCompatDelegate
to use with a context
and hosted by an Activity
.
Parameters | |
---|---|
context |
Context |
activity |
Activity |
callback |
AppCompatCallback : An optional callback for AppCompat specific events
|
Returns | |
---|---|
AppCompatDelegate |
create
public static AppCompatDelegate create (Context context, Window window, AppCompatCallback callback)
Create an AppCompatDelegate
to use with a context
and a window
.
Parameters | |
---|---|
context |
Context |
window |
Window |
callback |
AppCompatCallback : An optional callback for AppCompat specific events
|
Returns | |
---|---|
AppCompatDelegate |
createView
public abstract View createView (View parent, String name, Context context, AttributeSet attrs)
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()
.
Parameters | |
---|---|
parent |
View |
name |
String |
context |
Context |
attrs |
AttributeSet |
Returns | |
---|---|
View |
findViewById
public abstract T findViewById (int id)
Finds a view that was identified by the id attribute from the XML that
was processed in onCreate(Bundle)
.
Parameters | |
---|---|
id |
int |
Returns | |
---|---|
T |
The view if found or null otherwise. |
getDefaultNightMode
public static int getDefaultNightMode ()
Returns the default night mode.
Returns | |
---|---|
int |
See also:
getDrawerToggleDelegate
public abstract ActionBarDrawerToggle.Delegate getDrawerToggleDelegate ()
Returns an ActionBarDrawerToggle.Delegate
which can be returned from your Activity
if it implements ActionBarDrawerToggle.DelegateProvider
.
Returns | |
---|---|
ActionBarDrawerToggle.Delegate |
getLocalNightMode
public int getLocalNightMode ()
Returns the night mode previously set via getLocalNightMode()
.
Returns | |
---|---|
int |
getMenuInflater
public abstract MenuInflater getMenuInflater ()
Return the value of this call from your Activity.getMenuInflater()
Returns | |
---|---|
MenuInflater |
getSupportActionBar
public abstract ActionBar getSupportActionBar ()
Support library version of Activity.getActionBar()
.
Returns | |
---|---|
ActionBar |
AppCompat's action bar, or null if it does not have one. |
hasWindowFeature
public abstract boolean hasWindowFeature (int featureId)
Query for the availability of a certain feature.
This should be called instead of Window.hasFeature(int)
.
Parameters | |
---|---|
featureId |
int : The feature ID to check |
Returns | |
---|---|
boolean |
true if the feature is enabled, false otherwise. |
installViewFactory
public abstract void installViewFactory ()
Installs AppCompat's 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.
invalidateOptionsMenu
public abstract void invalidateOptionsMenu ()
Should be called from Activity.invalidateOptionsMenu()
} or
FragmentActivity.supportInvalidateOptionsMenu()
.
isCompatVectorFromResourcesEnabled
public static boolean isCompatVectorFromResourcesEnabled ()
Returns whether vector drawables on older platforms (< API 21) can be accessed from within resources.
Returns | |
---|---|
boolean |
isHandleNativeActionModesEnabled
public abstract boolean isHandleNativeActionModesEnabled ()
Returns whether AppCompat handles any native action modes itself.
Returns | |
---|---|
boolean |
true if AppCompat should handle native action modes. |
onConfigurationChanged
public abstract void onConfigurationChanged (Configuration newConfig)
Should be called from
Activity.onConfigurationChanged(Configuration)
Parameters | |
---|---|
newConfig |
Configuration |
onCreate
public abstract void onCreate (Bundle savedInstanceState)
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); // ... }
Parameters | |
---|---|
savedInstanceState |
Bundle |
onPostCreate
public abstract void onPostCreate (Bundle savedInstanceState)
Should be called from Activity.onPostCreate(android.os.Bundle)
Parameters | |
---|---|
savedInstanceState |
Bundle |
onSaveInstanceState
public abstract void onSaveInstanceState (Bundle outState)
Allows AppCompat to save instance state.
Parameters | |
---|---|
outState |
Bundle |
onStart
public abstract void onStart ()
Should be called from Activity.onStart()
Activity.onStart()}
requestWindowFeature
public abstract boolean requestWindowFeature (int featureId)
Enable extended window features. This should be called instead of
Activity.requestWindowFeature(int)
or
getWindow().requestFeature()
.
Parameters | |
---|---|
featureId |
int : The desired feature as defined in Window . |
Returns | |
---|---|
boolean |
Returns true if the requested feature is supported and now enabled. |
setCompatVectorFromResourcesEnabled
public static void setCompatVectorFromResourcesEnabled (boolean enabled)
Sets whether vector drawables on older platforms (< API 21) can be used within
DrawableContainer
resources.
When enabled, AppCompat can intercept some drawable inflation from the framework, which
enables implicit inflation of vector drawables within
DrawableContainer
resources. You can then use those
drawables in places such as android:src
on ImageView
,
or android:drawableLeft
on 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
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.
Parameters | |
---|---|
enabled |
boolean |
setContentView
public abstract void setContentView (View v)
Should be called instead of Activity.setContentView(android.view.View)
}
Parameters | |
---|---|
v |
View |
setContentView
public abstract void setContentView (int resId)
Should be called instead of Activity.setContentView(int)
}
Parameters | |
---|---|
resId |
int |
setContentView
public abstract void setContentView (View v, ViewGroup.LayoutParams lp)
Should be called instead of
Activity.setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
}
Parameters | |
---|---|
v |
View |
lp |
ViewGroup.LayoutParams |
setDefaultNightMode
public static void setDefaultNightMode (int mode)
Sets the default night mode. This is the default value used for all components, but can
be overridden locally via setLocalNightMode(int)
.
This is the primary method to control the DayNight functionality, since it allows the delegates to avoid unnecessary recreations when possible.
If this method is called after any host components with attached
AppCompatDelegate
s have been 'started', a uiMode
configuration change
will occur in each. This may result in those components being recreated, depending
on their manifest configuration.
Defaults to MODE_NIGHT_FOLLOW_SYSTEM
.
Parameters | |
---|---|
mode |
int |
setHandleNativeActionModesEnabled
public abstract void setHandleNativeActionModesEnabled (boolean enabled)
Whether AppCompat handles any native action modes itself.
This methods only takes effect on
Build.VERSION_CODES.ICE_CREAM_SANDWICH
and above.
Parameters | |
---|---|
enabled |
boolean : whether AppCompat should handle native action modes.
|
setLocalNightMode
public abstract void setLocalNightMode (int mode)
Override the night mode used for this delegate's host component.
When setting an mode to be used across an entire app, the
setDefaultNightMode(int)
method is preferred.
If this is called after the host component has been created, a uiMode
configuration change will occur, which may result in the component being recreated.
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.
Parameters | |
---|---|
mode |
int |
setSupportActionBar
public abstract void setSupportActionBar (Toolbar toolbar)
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
|
setTheme
public void setTheme (int themeResId)
This should be called from Activity.setTheme(int)
to notify AppCompat of what
the current theme resource id is.
Parameters | |
---|---|
themeResId |
int |
setTitle
public abstract void setTitle (CharSequence title)
Should be called from Activity.onTitleChanged(CharSequence, int)
}
Parameters | |
---|---|
title |
CharSequence |
startSupportActionMode
public abstract ActionMode startSupportActionMode (ActionMode.Callback callback)
Start an action mode.
Parameters | |
---|---|
callback |
ActionMode.Callback : Callback that will manage lifecycle events for this context mode |
Returns | |
---|---|
ActionMode |
The ContextMode that was started, or null if it was canceled |