public abstract class

ActionMode

extends java.lang.Object

 java.lang.Object

↳androidx.appcompat.view.ActionMode

Subclasses:

SupportActionModeWrapper, StandaloneActionMode, WindowDecorActionBar.ActionModeImpl

Gradle dependencies

compile group: 'androidx.appcompat', name: 'appcompat', version: '1.7.0'

  • groupId: androidx.appcompat
  • artifactId: appcompat
  • version: 1.7.0

Artifact androidx.appcompat:appcompat:1.7.0 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.appcompat:appcompat com.android.support:appcompat-v7

Androidx class mapping:

androidx.appcompat.view.ActionMode android.support.v7.view.ActionMode

Overview

Represents a contextual mode of the user interface. Action modes can be used to provide alternative interaction modes and replace parts of the normal UI until finished. Examples of good action modes include text selection and contextual actions.

Developer Guides

For information about how to provide contextual actions with ActionMode, read the Menus developer guide.

Summary

Constructors
publicActionMode()

Methods
public abstract voidfinish()

Finish and close this action mode.

public abstract ViewgetCustomView()

Returns the current custom view for this action mode.

public abstract MenugetMenu()

Returns the menu of actions that this action mode presents.

public abstract MenuInflatergetMenuInflater()

Returns a with the ActionMode's context.

public abstract java.lang.CharSequencegetSubtitle()

Returns the current subtitle of this action mode.

public java.lang.ObjectgetTag()

Retrieve the tag object associated with this ActionMode.

public abstract java.lang.CharSequencegetTitle()

Returns the current title of this action mode.

public booleangetTitleOptionalHint()

public abstract voidinvalidate()

Invalidate the action mode and refresh menu content.

public booleanisTitleOptional()

public booleanisUiFocusable()

Returns whether the UI presenting this action mode can take focus or not.

public abstract voidsetCustomView(View view)

Set a custom view for this action mode.

public abstract voidsetSubtitle(java.lang.CharSequence subtitle)

Set the subtitle of the action mode.

public abstract voidsetSubtitle(int resId)

Set the subtitle of the action mode.

public voidsetTag(java.lang.Object tag)

Set a tag object associated with this ActionMode.

public abstract voidsetTitle(java.lang.CharSequence title)

Set the title of the action mode.

public abstract voidsetTitle(int resId)

Set the title of the action mode.

public voidsetTitleOptionalHint(boolean titleOptional)

Set whether or not the title/subtitle display for this action mode is optional.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public ActionMode()

Methods

public void setTag(java.lang.Object tag)

Set a tag object associated with this ActionMode.

Like the tag available to views, this allows applications to associate arbitrary data with an ActionMode for later reference.

Parameters:

tag: Tag to associate with this ActionMode

See also: ActionMode.getTag()

public java.lang.Object getTag()

Retrieve the tag object associated with this ActionMode.

Like the tag available to views, this allows applications to associate arbitrary data with an ActionMode for later reference.

Returns:

Tag associated with this ActionMode

See also: ActionMode.setTag(Object)

public abstract void setTitle(java.lang.CharSequence title)

Set the title of the action mode. This method will have no visible effect if a custom view has been set.

Parameters:

title: Title string to set

See also: ActionMode.setTitle(int), ActionMode.setCustomView(View)

public abstract void setTitle(int resId)

Set the title of the action mode. This method will have no visible effect if a custom view has been set.

Parameters:

resId: Resource ID of a string to set as the title

See also: ActionMode.setTitle(CharSequence), ActionMode.setCustomView(View)

public abstract void setSubtitle(java.lang.CharSequence subtitle)

Set the subtitle of the action mode. This method will have no visible effect if a custom view has been set.

Parameters:

subtitle: Subtitle string to set

See also: ActionMode.setSubtitle(int), ActionMode.setCustomView(View)

public abstract void setSubtitle(int resId)

Set the subtitle of the action mode. This method will have no visible effect if a custom view has been set.

Parameters:

resId: Resource ID of a string to set as the subtitle

See also: ActionMode.setSubtitle(CharSequence), ActionMode.setCustomView(View)

public void setTitleOptionalHint(boolean titleOptional)

Set whether or not the title/subtitle display for this action mode is optional.

In many cases the supplied title for an action mode is merely meant to add context and is not strictly required for the action mode to be useful. If the title is optional, the system may choose to hide the title entirely rather than truncate it due to a lack of available space.

Note that this is merely a hint; the underlying implementation may choose to ignore this setting under some circumstances.

Parameters:

titleOptional: true if the title only presents optional information.

public boolean getTitleOptionalHint()

Returns:

true if this action mode has been given a hint to consider the title/subtitle display to be optional.

See also: ActionMode.setTitleOptionalHint(boolean), ActionMode.isTitleOptional()

public boolean isTitleOptional()

Returns:

true if this action mode considers the title and subtitle fields as optional. Optional titles may not be displayed to the user.

public abstract void setCustomView(View view)

Set a custom view for this action mode. The custom view will take the place of the title and subtitle. Useful for things like search boxes.

Parameters:

view: Custom view to use in place of the title/subtitle.

See also: ActionMode.setTitle(CharSequence), ActionMode.setSubtitle(CharSequence)

public abstract void invalidate()

Invalidate the action mode and refresh menu content. The mode's ActionMode.Callback will have its ActionMode.Callback.onPrepareActionMode(ActionMode, Menu) method called. If it returns true the menu will be scanned for updated content and any relevant changes will be reflected to the user.

public abstract void finish()

Finish and close this action mode. The action mode's ActionMode.Callback will have its ActionMode.Callback.onDestroyActionMode(ActionMode) method called.

public abstract Menu getMenu()

Returns the menu of actions that this action mode presents.

Returns:

The action mode's menu.

public abstract java.lang.CharSequence getTitle()

Returns the current title of this action mode.

Returns:

Title text

public abstract java.lang.CharSequence getSubtitle()

Returns the current subtitle of this action mode.

Returns:

Subtitle text

public abstract View getCustomView()

Returns the current custom view for this action mode.

Returns:

The current custom view

public abstract MenuInflater getMenuInflater()

Returns a with the ActionMode's context.

public boolean isUiFocusable()

Returns whether the UI presenting this action mode can take focus or not. This is used by internal components within the framework that would otherwise present an action mode UI that requires focus, such as an EditText as a custom view.

Returns:

true if the UI used to show this action mode can take focus

Source

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package androidx.appcompat.view;

import static androidx.annotation.RestrictTo.Scope.LIBRARY_GROUP_PREFIX;

import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.RestrictTo;

/**
 * Represents a contextual mode of the user interface. Action modes can be used to provide
 * alternative interaction modes and replace parts of the normal UI until finished.
 * Examples of good action modes include text selection and contextual actions.
 * <div class="special reference">
 *
 * <h3>Developer Guides</h3>
 * <p>For information about how to provide contextual actions with {@code ActionMode},
 * read the <a href="{@docRoot}guide/topics/ui/menus.html#context-menu">Menus</a>
 * developer guide.</p>
 *
 * </div>
 */
public abstract class ActionMode {

    private Object mTag;
    private boolean mTitleOptionalHint;

    /**
     * Set a tag object associated with this ActionMode.
     *
     * <p>Like the tag available to views, this allows applications to associate arbitrary
     * data with an ActionMode for later reference.
     *
     * @param tag Tag to associate with this ActionMode
     *
     * @see #getTag()
     */
    public void setTag(Object tag) {
        mTag = tag;
    }

    /**
     * Retrieve the tag object associated with this ActionMode.
     *
     * <p>Like the tag available to views, this allows applications to associate arbitrary
     * data with an ActionMode for later reference.
     *
     * @return Tag associated with this ActionMode
     *
     * @see #setTag(Object)
     */
    public Object getTag() {
        return mTag;
    }

    /**
     * Set the title of the action mode. This method will have no visible effect if
     * a custom view has been set.
     *
     * @param title Title string to set
     *
     * @see #setTitle(int)
     * @see #setCustomView(View)
     */
    public abstract void setTitle(CharSequence title);

    /**
     * Set the title of the action mode. This method will have no visible effect if
     * a custom view has been set.
     *
     * @param resId Resource ID of a string to set as the title
     *
     * @see #setTitle(CharSequence)
     * @see #setCustomView(View)
     */
    public abstract void setTitle(int resId);

    /**
     * Set the subtitle of the action mode. This method will have no visible effect if
     * a custom view has been set.
     *
     * @param subtitle Subtitle string to set
     *
     * @see #setSubtitle(int)
     * @see #setCustomView(View)
     */
    public abstract void setSubtitle(CharSequence subtitle);

    /**
     * Set the subtitle of the action mode. This method will have no visible effect if
     * a custom view has been set.
     *
     * @param resId Resource ID of a string to set as the subtitle
     *
     * @see #setSubtitle(CharSequence)
     * @see #setCustomView(View)
     */
    public abstract void setSubtitle(int resId);

    /**
     * Set whether or not the title/subtitle display for this action mode
     * is optional.
     *
     * <p>In many cases the supplied title for an action mode is merely
     * meant to add context and is not strictly required for the action
     * mode to be useful. If the title is optional, the system may choose
     * to hide the title entirely rather than truncate it due to a lack
     * of available space.</p>
     *
     * <p>Note that this is merely a hint; the underlying implementation
     * may choose to ignore this setting under some circumstances.</p>
     *
     * @param titleOptional true if the title only presents optional information.
     */
    public void setTitleOptionalHint(boolean titleOptional) {
        mTitleOptionalHint = titleOptional;
    }

    /**
     * @return true if this action mode has been given a hint to consider the
     *         title/subtitle display to be optional.
     *
     * @see #setTitleOptionalHint(boolean)
     * @see #isTitleOptional()
     */
    public boolean getTitleOptionalHint() {
        return mTitleOptionalHint;
    }

    /**
     * @return true if this action mode considers the title and subtitle fields
     *         as optional. Optional titles may not be displayed to the user.
     */
    public boolean isTitleOptional() {
        return false;
    }

    /**
     * Set a custom view for this action mode. The custom view will take the place of
     * the title and subtitle. Useful for things like search boxes.
     *
     * @param view Custom view to use in place of the title/subtitle.
     *
     * @see #setTitle(CharSequence)
     * @see #setSubtitle(CharSequence)
     */
    public abstract void setCustomView(View view);

    /**
     * Invalidate the action mode and refresh menu content. The mode's
     * {@link ActionMode.Callback} will have its
     * {@link Callback#onPrepareActionMode(ActionMode, Menu)} method called.
     * If it returns true the menu will be scanned for updated content and any relevant changes
     * will be reflected to the user.
     */
    public abstract void invalidate();

    /**
     * Finish and close this action mode. The action mode's {@link ActionMode.Callback} will
     * have its {@link Callback#onDestroyActionMode(ActionMode)} method called.
     */
    public abstract void finish();

    /**
     * Returns the menu of actions that this action mode presents.
     *
     * @return The action mode's menu.
     */
    public abstract Menu getMenu();

    /**
     * Returns the current title of this action mode.
     *
     * @return Title text
     */
    public abstract CharSequence getTitle();

    /**
     * Returns the current subtitle of this action mode.
     *
     * @return Subtitle text
     */
    public abstract CharSequence getSubtitle();

    /**
     * Returns the current custom view for this action mode.
     *
     * @return The current custom view
     */
    public abstract View getCustomView();

    /**
     * Returns a {@link MenuInflater} with the ActionMode's context.
     */
    public abstract MenuInflater getMenuInflater();

    /**
     * Returns whether the UI presenting this action mode can take focus or not.
     * This is used by internal components within the framework that would otherwise
     * present an action mode UI that requires focus, such as an EditText as a custom view.
     *
     * @return true if the UI used to show this action mode can take focus
     */
    @RestrictTo(LIBRARY_GROUP_PREFIX)
    public boolean isUiFocusable() {
        return true;
    }

    /**
     * Callback interface for action modes. Supplied to
     * {@link androidx.appcompat.app.AppCompatDelegate#startSupportActionMode(Callback)} (Callback)},
     * a Callback configures and handles events raised by a user's interaction with an action mode.
     *
     * <p>An action mode's lifecycle is as follows:
     * <ul>
     * <li>{@link Callback#onCreateActionMode(ActionMode, Menu)} once on initial
     * creation</li>
     * <li>{@link Callback#onPrepareActionMode(ActionMode, Menu)} after creation
     * and any time the {@link ActionMode} is invalidated</li>
     * <li>{@link Callback#onActionItemClicked(ActionMode, MenuItem)} any time a
     * contextual action button is clicked</li>
     * <li>{@link Callback#onDestroyActionMode(ActionMode)} when the action mode
     * is closed</li>
     * </ul>
     */
    public interface Callback {

        /**
         * Called when action mode is first created. The menu supplied will be used to
         * generate action buttons for the action mode.
         *
         * @param mode ActionMode being created
         * @param menu Menu used to populate action buttons
         * @return true if the action mode should be created, false if entering this
         *              mode should be aborted.
         */
        public boolean onCreateActionMode(ActionMode mode, Menu menu);

        /**
         * Called to refresh an action mode's action menu whenever it is invalidated.
         *
         * @param mode ActionMode being prepared
         * @param menu Menu used to populate action buttons
         * @return true if the menu or action mode was updated, false otherwise.
         */
        public boolean onPrepareActionMode(ActionMode mode, Menu menu);

        /**
         * Called to report a user click on an action button.
         *
         * @param mode The current ActionMode
         * @param item The item that was clicked
         * @return true if this callback handled the event, false if the standard MenuItem
         *         invocation should continue.
         */
        public boolean onActionItemClicked(ActionMode mode, MenuItem item);

        /**
         * Called when an action mode is about to be exited and destroyed.
         *
         * @param mode The current ActionMode being destroyed
         */
        public void onDestroyActionMode(ActionMode mode);
    }
}