public class

Action

extends java.lang.Object

 java.lang.Object

↳androidx.leanback.widget.Action

Subclasses:

PlaybackControlsRow.MultiAction, PlaybackControlsRow.PlayPauseAction, PlaybackControlsRow.FastForwardAction, PlaybackControlsRow.RewindAction, PlaybackControlsRow.SkipNextAction, PlaybackControlsRow.SkipPreviousAction, PlaybackControlsRow.PictureInPictureAction, PlaybackControlsRow.MoreActions, PlaybackControlsRow.ThumbsAction, PlaybackControlsRow.ThumbsUpAction, PlaybackControlsRow.ThumbsDownAction, PlaybackControlsRow.RepeatAction, PlaybackControlsRow.ShuffleAction, PlaybackControlsRow.HighQualityAction, PlaybackControlsRow.ClosedCaptioningAction, GuidedAction, GuidedDatePickerAction

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback', version: '1.2.0-alpha04'

  • groupId: androidx.leanback
  • artifactId: leanback
  • version: 1.2.0-alpha04

Artifact androidx.leanback:leanback:1.2.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback com.android.support:leanback-v17

Androidx class mapping:

androidx.leanback.widget.Action android.support.v17.leanback.widget.Action

Overview

An action contains one or two lines of text, an optional image and an optional id. It may also be invoked by one or more keycodes.

Summary

Fields
public static final longNO_ID

Indicates that an id has not been set.

Constructors
publicAction(long id)

Constructor for an Action.

publicAction(long id, java.lang.CharSequence label)

Constructor for an Action.

publicAction(long id, java.lang.CharSequence label1, java.lang.CharSequence label2)

Constructor for an Action.

publicAction(long id, java.lang.CharSequence label1, java.lang.CharSequence label2, Drawable icon)

Constructor for an Action.

Methods
public final voidaddKeyCode(int keyCode)

Adds a keycode used to invoke this Action.

public final DrawablegetIcon()

Returns the icon drawable for this Action.

public final longgetId()

Returns the id for this Action.

public final java.lang.CharSequencegetLabel1()

Returns the first line label for this Action.

public final java.lang.CharSequencegetLabel2()

Returns the second line label for this Action.

public final voidremoveKeyCode(int keyCode)

Removes a keycode used to invoke this Action.

public final booleanrespondsToKeyCode(int keyCode)

Returns true if the Action should respond to the given keycode.

public final voidsetIcon(Drawable icon)

Sets the icon drawable for this Action.

public final voidsetId(long id)

Sets the id for this Action.

public final voidsetLabel1(java.lang.CharSequence label)

Sets the first line label for this Action.

public final voidsetLabel2(java.lang.CharSequence label)

Sets the second line label for this Action.

public java.lang.StringtoString()

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

Fields

public static final long NO_ID

Indicates that an id has not been set.

Constructors

public Action(long id)

Constructor for an Action.

Parameters:

id: The id of the Action.

public Action(long id, java.lang.CharSequence label)

Constructor for an Action.

Parameters:

id: The id of the Action.
label: The label to display for the Action.

public Action(long id, java.lang.CharSequence label1, java.lang.CharSequence label2)

Constructor for an Action.

Parameters:

id: The id of the Action.
label1: The label to display on the first line of the Action.
label2: The label to display on the second line of the Action.

public Action(long id, java.lang.CharSequence label1, java.lang.CharSequence label2, Drawable icon)

Constructor for an Action.

Parameters:

id: The id of the Action.
label1: The label to display on the first line of the Action.
label2: The label to display on the second line of the Action.
icon: The icon to display for the Action.

Methods

public final void setId(long id)

Sets the id for this Action.

public final long getId()

Returns the id for this Action.

public final void setLabel1(java.lang.CharSequence label)

Sets the first line label for this Action.

public final java.lang.CharSequence getLabel1()

Returns the first line label for this Action.

public final void setLabel2(java.lang.CharSequence label)

Sets the second line label for this Action.

public final java.lang.CharSequence getLabel2()

Returns the second line label for this Action.

public final void setIcon(Drawable icon)

Sets the icon drawable for this Action.

public final Drawable getIcon()

Returns the icon drawable for this Action.

public final void addKeyCode(int keyCode)

Adds a keycode used to invoke this Action.

public final void removeKeyCode(int keyCode)

Removes a keycode used to invoke this Action.

public final boolean respondsToKeyCode(int keyCode)

Returns true if the Action should respond to the given keycode.

public java.lang.String toString()

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.leanback.widget;

import android.graphics.drawable.Drawable;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

/**
 * An action contains one or two lines of text, an optional image and an optional id. It may also
 * be invoked by one or more keycodes.
 */
public class Action {

    /** Indicates that an id has not been set. */
    public static final long NO_ID = -1;

    private long mId = NO_ID;
    private Drawable mIcon;
    private CharSequence mLabel1;
    private CharSequence mLabel2;
    private ArrayList<Integer> mKeyCodes = new ArrayList<>();

    /**
     * Constructor for an Action.
     *
     * @param id The id of the Action.
     */
    public Action(long id) {
        this(id, "");
    }

    /**
     * Constructor for an Action.
     *
     * @param id The id of the Action.
     * @param label The label to display for the Action.
     */
    public Action(long id, @Nullable CharSequence label) {
        this(id, label, null);
    }

    /**
     * Constructor for an Action.
     *
     * @param id The id of the Action.
     * @param label1 The label to display on the first line of the Action.
     * @param label2 The label to display on the second line of the Action.
     */
    public Action(long id, @Nullable CharSequence label1, @Nullable CharSequence label2) {
        this(id, label1, label2, null);
    }

    /**
     * Constructor for an Action.
     *
     * @param id The id of the Action.
     * @param label1 The label to display on the first line of the Action.
     * @param label2 The label to display on the second line of the Action.
     * @param icon The icon to display for the Action.
     */
    public Action(
            long id,
            @Nullable CharSequence label1,
            @Nullable CharSequence label2,
            @Nullable Drawable icon
    ) {
        setId(id);
        setLabel1(label1);
        setLabel2(label2);
        setIcon(icon);
    }

    /**
     * Sets the id for this Action.
     */
    public final void setId(long id) {
        mId = id;
    }

    /**
     * Returns the id for this Action.
     */
    public final long getId() {
        return mId;
    }

    /**
     * Sets the first line label for this Action.
     */
    public final void setLabel1(@Nullable CharSequence label) {
        mLabel1 = label;
    }

    /**
     * Returns the first line label for this Action.
     */
    @Nullable
    public final CharSequence getLabel1() {
        return mLabel1;
    }

    /**
     * Sets the second line label for this Action.
     */
    public final void setLabel2(@Nullable CharSequence label) {
        mLabel2 = label;
    }

    /**
     * Returns the second line label for this Action.
     */
    @Nullable
    public final CharSequence getLabel2() {
        return mLabel2;
    }

    /**
     * Sets the icon drawable for this Action.
     */
    public final void setIcon(@Nullable Drawable icon) {
        mIcon = icon;
    }

    /**
     * Returns the icon drawable for this Action.
     */
    @Nullable
    public final Drawable getIcon() {
        return mIcon;
    }

    /**
     * Adds a keycode used to invoke this Action.
     */
    public final void addKeyCode(int keyCode) {
        mKeyCodes.add(keyCode);
    }

    /**
     * Removes a keycode used to invoke this Action.
     */
    public final void removeKeyCode(int keyCode) {
        mKeyCodes.remove(keyCode);
    }

    /**
     * Returns true if the Action should respond to the given keycode.
     */
    public final boolean respondsToKeyCode(int keyCode) {
        return mKeyCodes.contains(keyCode);
    }

    @Override
    @NonNull
    public String toString(){
        StringBuilder sb = new StringBuilder();
        if (!TextUtils.isEmpty(mLabel1)) {
            sb.append(mLabel1);
        }
        if (!TextUtils.isEmpty(mLabel2)) {
            if (!TextUtils.isEmpty(mLabel1)) {
                sb.append(" ");
            }
            sb.append(mLabel2);
        }
        if (mIcon != null && sb.length() == 0) {
            sb.append("(action icon)");
        }
        return sb.toString();
    }
}