android_learners_hub
ActionMode In Android
It is an android view class that represents a contextual mode of the user
interface. Action modes can be used to provide alternative or temporary
interaction modes and replace parts of the normal User Interface until it
finish. When action mode is finish the temporary user interface is closed
and we will get the pervious user interface. Examples of good action modes
include text selection and contextual actions.
To use ActionMode we have to import following statement:-
import androidx.appcompat.view.ActionMode;
Nested classes:-
1.Interface:-
ActionMode.Callback:- Callback interface for action modes. It
configures and handles events raised by a user's interaction with an
action mode. Following are four required functions to override in an
implementation of ActionMode.Callback which are also known as action
mode's lifecycle :-
1.onActionItemClicked(ActionMode mode, MenuItem item):- It is called on
that time when a contextual action button is clicked to report a user
click on an action button.
2.onCreateActionMode(ActionMode mode, Menu menu):- It is called once on
initial creation, when action mode is first created.
3.onDestroyActionMode(ActionMode mode):- It is called when an action mode
is closed or about to be exited and destroyed.
4.onPrepareActionMode(ActionMode mode, Menu menu):- It is called after
creation to refresh an action mode's action menu whenever it is
invalidated.
Example:-
private ActionMode.Callback actionModeCallback = new ActionMode.Callback()
{
// Called when the action mode is created; startActionMode() was
called
@Override
public boolean onCreateActionMode(ActionMode mode, Menu
menu) {
// Inflate a menu resource providing context menu items
MenuInflater inflater =
mode.getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
return true;
}
// Called each time the action mode is shown. Always called after
onCreateActionMode, but
// may be called multiple times if the mode is
invalidated.
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu
menu) {
return false;
// Return false if nothing is done
}
// Called when the user selects a contextual menu item
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem
item) {
switch (item.getItemId()) {
case R.id.menu_share:
shareCurrentItem();
mode.finish();
// Action picked, so close the CAB
return true;
default:
return false;
}
}
// Called when the user exits the action mode
@Override
public void onDestroyActionMode(ActionMode mode) {
actionMode = null;
}
};
Click me to see practical implementation of above methods.
2.Class:-
ActionMode.Callback2:- Extension of ActionMode.Callback to provide
content rect information. This is required for ActionModes with dynamic
positioning such as floating action mode. It has Public
constructors,Callback2() and Public method onGetContentRect(ActionMode
mode, View view, Rect outRect) which is called when an ActionMode needs to
be positioned on screen, potentially when it come in contact with view
content.
Description of parameters used in onGetContentRect(ActionMode mode, View
view, Rect outRect) mehod:-
Parameters |
Description |
---|---|
1.ActionMode | The ActionMode that requires positioning. |
2.View | The View that originated the ActionMode, in whose coordinates the Rect should be provided. |
3.Rect | The Rect to be populated with the content position. Use this to specify where the content in your app lives within the given view. This will be used to avoid occluding the given content Rect with the created ActionMode. |
Example:-
onGetContentRect(mode: ActionMode, view: View, outRect: Rect) {
// Provides content rect information
// Required to dynamically position floating ActionMode,
// such that it doesn't obscure app content
outRect.set(left, top, right, bottom)
}
You could pass in the bounds of your View here, or the bounds of the
content on which you are focusing contextual actions. If you want to show
the floating Action Mode at a specific (x, y) point, you could make left =
right and top = bottom.
Public constructors :-
ActionMode()
Constants:-
It has following 3 constants, that are added in API level 23 (Android version:6 , Version name : Marshmallow) .
Constant | Description | Constant Value |
---|---|---|
1.DEFAULT_HIDE_DURATION | It sets the default value to hide the action mode for ViewConfiguration#getDefaultActionModeHideDuration(). | -1 (0xffffffff) |
2.TYPE_FLOATING | The action mode is treated as a Floating Toolbar. Use with setType(int). | 1 (0x00000001) |
3.TYPE_PRIMARY | The action mode is treated as a Primary mode. This is the default. Use with setType(int). | 0 (0x00000000) |
Public Methods:-
There are many methods associated with action mode from which some are listed below:-
Method |
Description | Method Added In |
---|---|---|
1.finish() | Finish and close this action mode. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
2.getCustomView() | Returns the current custom view for this action mode. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
3.getMenu() | It returns the menu of actions that this action mode presents. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
4.getMenuInflater() | It will return a MenuInflater with the ActionMode's context. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
5.getSubtitle() | It will Returns the current subtitle of this action mode. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
6.getTag () | Retrieve the tag object associated with this ActionMode. | API level 14 (Android version: 4.0.1, Version name : Ice cream sandwich). |
7.getTitle () | Returns the current title of this action mode. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
8.setSubtitle (CharSequence subtitle) | Set the subtitle of the action mode. This method will have no visible effect if a custom view has been set. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
9.setSubtitle (int resId) | Set the subtitle of the action mode. This method will have no visible effect if a custom view has been set. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
10.setTag (Object tag) | Set a tag object associated with this ActionMode. | API level 14 (Android version: 4.0.1, Version name : Ice cream sandwich). |
11.setTitle (CharSequence title) | Set the title of the action mode. This method will have no visible effect if a custom view has been set. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
12.setTitle (int resId) | Set the title of the action mode. This method will have no visible effect if a custom view has been set. | API level 11 (Android version: 3.0, Version name : Honeycomb). |
How to Styling an action mode:-
When we style action mode it draws user attention.ActionMode can be styled in a theme.xml file , that you find in res/value/theme. The base colors and text appearances are also derived from your app/activity theme (eg. Dark background with light text for Theme.AppCompat and the opposite for Theme.AppCompat.Light). So, now we are going to code in between <style></style> tag in theme.xml file to style action mode.
Some of the notable primary ActionMode theme attributes include:
1. actionModeBackground: The color or drawable used for the background of the contextual action bar.
2. actionModeStyle: A reference to a Widget.AppCompat.ActionMode style that includes attributes such as height, titleTextStyle and subtitleTextStyle.
3. windowActionModeOverlay: Flag indicating whether primary ActionMode should overlay window content or resize the content to fit beneath it
Comments
Post a Comment