Google is committed to advancing racial equity for Black communities. See how.

WearableButtons

class WearableButtons
kotlin.Any
   ↳ androidx.wear.input.WearableButtons

Class containing helpers for managing wearable buttons.

Summary

Nested classes

Metadata for a specific button.

Public methods
static Int
getButtonCount(@NonNull context: Context)

Get the number of hardware buttons available.

static Drawable?
getButtonIcon(@NonNull context: Context, keycode: Int)

Returns an icon that can be used to represent the location of a button.

static WearableButtons.ButtonInfo?
getButtonInfo(@NonNull context: Context, keycode: Int)

Returns a ButtonInfo containing the metadata for a specific button.

static CharSequence?
getButtonLabel(@NonNull context: Context, keycode: Int)

Returns a CharSequence that describes the placement location of a button.

static Unit

Testing call to allow the underlying WearableButtonsProvider to be substituted in test code.

Public methods

getButtonCount

static fun getButtonCount(@NonNull context: Context): Int

Get the number of hardware buttons available. This count includes the primary stem key as well as any secondary stem keys available.

Parameters
context Context: The context of the current activity
Return
Int The number of buttons available, or the information is not available.

getButtonIcon

@Nullable static fun getButtonIcon(
    @NonNull context: Context,
    keycode: Int
): Drawable?

Returns an icon that can be used to represent the location of a button.

Parameters
context Context: The context of the current activity
keycode Int: The keycode associated with the hardware button of interest
Return
Drawable? A drawable representing the location of a button, or null if unavailable

getButtonInfo

@Nullable static fun getButtonInfo(
    @NonNull context: Context,
    keycode: Int
): WearableButtons.ButtonInfo?

Returns a ButtonInfo containing the metadata for a specific button.

The location will be populated in the following manner:

  • The provided point will be on the screen, or more typically, on the edge of the screen.
  • The point won't be off the edge of the screen.
  • The location returned is a screen coordinate. The unit of measurement is in pixels. The coordinates do not take rotation into account and assume that the device is in the standard upright position.

Common keycodes to use are android.view.KeyEvent#KEYCODE_STEM_PRIMARY, , android.view.KeyEvent#KEYCODE_STEM_2, and android.view.KeyEvent#KEYCODE_STEM_3.

Parameters
context Context: The context of the current activity
keycode Int: The keycode associated with the hardware button of interest
Return
WearableButtons.ButtonInfo? A ButtonInfo containing the metadata for the given keycode or null if the information is not available

getButtonLabel

@Nullable static fun getButtonLabel(
    @NonNull context: Context,
    keycode: Int
): CharSequence?

Returns a CharSequence that describes the placement location of a button. An example might be "Top right" or "Bottom".

Parameters
context Context: The context of the current activity
keycode Int: The keycode associated with the hardware button of interest
Return
CharSequence? A CharSequence describing the placement location of the button, or null if no location is available for that button.

setWearableButtonsProvider

static fun setWearableButtonsProvider(@NonNull provider: WearableButtonsProvider): Unit

Testing call to allow the underlying WearableButtonsProvider to be substituted in test code.

Parameters
provider WearableButtonsProvider: The new WearableButtonsProvider to use.