Class Haptics

java.lang.Object
com.codename1.ui.Haptics

public final class Haptics extends Object

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));
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final int
    A task failed.
    static final int
    A heavy impact, for large elements colliding.
    static final int
    A light impact, for small elements snapping into place.
    static final int
    A medium impact.
    static final int
    A rigid, sharp impact.
    static final int
    A selection change tick, such as a picker moving one row.
    static final int
    A soft, damped impact.
    static final int
    A task completed successfully.
    static final int
    A task produced a warning.
  • Method Summary

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

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • 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: 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: 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: 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: a type constant of this class
      Returns

      the fallback duration in milliseconds