public class

StandaloneActionMode

extends ActionMode

implements MenuBuilder.Callback

 java.lang.Object

androidx.appcompat.view.ActionMode

↳androidx.appcompat.view.StandaloneActionMode

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.StandaloneActionMode android.support.v7.view.StandaloneActionMode

Summary

Constructors
publicStandaloneActionMode(Context context, ActionBarContextView view, ActionMode.Callback callback, boolean isFocusable)

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 abstract java.lang.CharSequencegetTitle()

Returns the current title of this action mode.

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 voidonCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)

public voidonCloseSubMenu(SubMenuBuilder menu)

public booleanonMenuItemSelected(MenuBuilder menu, MenuItem item)

public voidonMenuModeChange(MenuBuilder menu)

public booleanonSubMenuSelected(SubMenuBuilder subMenu)

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 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 ActionModegetTag, getTitleOptionalHint, setTag
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public StandaloneActionMode(Context context, ActionBarContextView view, ActionMode.Callback callback, boolean isFocusable)

Methods

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 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 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(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 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 onMenuItemSelected(MenuBuilder menu, MenuItem item)

public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing)

public boolean onSubMenuSelected(SubMenuBuilder subMenu)

public void onCloseSubMenu(SubMenuBuilder menu)

public void onMenuModeChange(MenuBuilder menu)

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) 2014 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.content.Context;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.RestrictTo;
import androidx.appcompat.view.menu.MenuBuilder;
import androidx.appcompat.view.menu.MenuPopupHelper;
import androidx.appcompat.view.menu.SubMenuBuilder;
import androidx.appcompat.widget.ActionBarContextView;

import java.lang.ref.WeakReference;

/**
 */
@RestrictTo(LIBRARY_GROUP_PREFIX)
public class StandaloneActionMode extends ActionMode implements MenuBuilder.Callback {
    private Context mContext;
    private ActionBarContextView mContextView;
    private ActionMode.Callback mCallback;
    private WeakReference<View> mCustomView;
    private boolean mFinished;
    private boolean mFocusable;

    private MenuBuilder mMenu;

    public StandaloneActionMode(Context context, ActionBarContextView view,
            ActionMode.Callback callback, boolean isFocusable) {
        mContext = context;
        mContextView = view;
        mCallback = callback;

        mMenu = new MenuBuilder(view.getContext()).setDefaultShowAsAction(
                MenuItem.SHOW_AS_ACTION_IF_ROOM);
        mMenu.setCallback(this);
        mFocusable = isFocusable;
    }

    @Override
    public void setTitle(CharSequence title) {
        mContextView.setTitle(title);
    }

    @Override
    public void setSubtitle(CharSequence subtitle) {
        mContextView.setSubtitle(subtitle);
    }

    @Override
    public void setTitle(int resId) {
        setTitle(mContext.getString(resId));
    }

    @Override
    public void setSubtitle(int resId) {
        setSubtitle(mContext.getString(resId));
    }

    @Override
    public void setTitleOptionalHint(boolean titleOptional) {
        super.setTitleOptionalHint(titleOptional);
        mContextView.setTitleOptional(titleOptional);
    }

    @Override
    public boolean isTitleOptional() {
        return mContextView.isTitleOptional();
    }

    @Override
    public void setCustomView(View view) {
        mContextView.setCustomView(view);
        mCustomView = view != null ? new WeakReference<View>(view) : null;
    }

    @Override
    public void invalidate() {
        mCallback.onPrepareActionMode(this, mMenu);
    }

    @Override
    public void finish() {
        if (mFinished) {
            return;
        }
        mFinished = true;

        mCallback.onDestroyActionMode(this);
    }

    @Override
    public Menu getMenu() {
        return mMenu;
    }

    @Override
    public CharSequence getTitle() {
        return mContextView.getTitle();
    }

    @Override
    public CharSequence getSubtitle() {
        return mContextView.getSubtitle();
    }

    @Override
    public View getCustomView() {
        return mCustomView != null ? mCustomView.get() : null;
    }

    @Override
    public MenuInflater getMenuInflater() {
        return new SupportMenuInflater(mContextView.getContext());
    }

    @Override
    public boolean onMenuItemSelected(@NonNull MenuBuilder menu, @NonNull MenuItem item) {
        return mCallback.onActionItemClicked(this, item);
    }

    public void onCloseMenu(MenuBuilder menu, boolean allMenusAreClosing) {
    }

    public boolean onSubMenuSelected(SubMenuBuilder subMenu) {
        if (!subMenu.hasVisibleItems()) {
            return true;
        }

        new MenuPopupHelper(mContextView.getContext(), subMenu).show();
        return true;
    }

    public void onCloseSubMenu(SubMenuBuilder menu) {
    }

    @Override
    public void onMenuModeChange(@NonNull MenuBuilder menu) {
        invalidate();
        mContextView.showOverflowMenu();
    }

    @Override
    public boolean isUiFocusable() {
        return mFocusable;
    }
}