Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class Haptics

  1. Object
  2. Haptics

Typed haptic feedback: impacts, selection ticks and notification feedback, mapped to the platform’s own effects.

On iOS these are the UIImpactFeedbackGenerator, UISelectionFeedbackGenerator and UINotificationFeedbackGenerator effects. On Android they are the view’s haptic feedback constants. Where a port has no typed haptics the call falls back to Display#vibrate(int) with a duration that matches the type, and where the device has no vibrator nothing happens.

Referencing this class makes the Android build declare the VIBRATE permission.

Button save = new Button("Save");
save.addActionListener(e -> Haptics.notification(Haptics.SUCCESS));

Fields

public static final int LIGHT = 1A light impact, for small elements snapping into place.
public static final int MEDIUM = 2A medium impact.
public static final int HEAVY = 3A heavy impact, for large elements colliding.
public static final int SOFT = 4A soft, damped impact.
public static final int RIGID = 5A rigid, sharp impact.
public static final int SELECTION = 10A selection change tick, such as a picker moving one row.
public static final int SUCCESS = 20A task completed successfully.
public static final int WARNING = 21A task produced a warning.
public static final int ERROR = 22A task failed.

Methods

public static boolean isSupported()True when the platform plays typed haptics.
public static void impact(int style)Plays an impact.
public static void selection()Plays a selection change tick.
public static void notification(int type)Plays notification feedback.
public static void pattern(long[] timings)Plays a vibration pattern.
public static int fallbackDuration(int type)The vibration length, in milliseconds, used for a type when the platform has no typed haptics.

Inherited methods

Field details

LIGHT

public static final int LIGHT = 1
A light impact, for small elements snapping into place.

MEDIUM

public static final int MEDIUM = 2
A medium impact.

HEAVY

public static final int HEAVY = 3
A heavy impact, for large elements colliding.

SOFT

public static final int SOFT = 4
A soft, damped impact.

RIGID

public static final int RIGID = 5
A rigid, sharp impact.

SELECTION

public static final int SELECTION = 10
A selection change tick, such as a picker moving one row.

SUCCESS

public static final int SUCCESS = 20
A task completed successfully.

WARNING

public static final int WARNING = 21
A task produced a warning.

ERROR

public static final int ERROR = 22
A task failed.

Method details

isSupported

public static boolean isSupported()
True when the platform plays typed haptics. When false, every method here still works and falls back to a short vibration.

Returns

true when typed haptics are available

impact

public static void impact(int style)
Plays an impact.

Parameters

style int
LIGHT, MEDIUM, HEAVY, SOFT or RIGID

selection

public static void selection()
Plays a selection change tick.

notification

public static void notification(int type)
Plays notification feedback.

Parameters

type int
SUCCESS, WARNING or ERROR

pattern

public static void pattern(long[] timings)
Plays a vibration pattern. Android plays it as a waveform; iOS plays an impact at the start of every “on” segment; elsewhere the first “on” segment becomes a plain vibration.

Parameters

timings long[]
alternating off and on durations in milliseconds, starting with an off duration, such as {0, 40, 60, 40}

fallbackDuration

public static int fallbackDuration(int type)
The vibration length, in milliseconds, used for a type when the platform has no typed haptics.

Parameters

type int
a type constant of this class

Returns

the fallback duration in milliseconds