How to handle tap events for an interactive watch faces with androidx.wear?

589 views Asked by At

What is the correct way of handling tap events with the new androidx.wear.watchface libraries? I was doing this with the now deprecated android.support:wearable libraries without any problem with Java (using onTapCommand). Now, everything seems to be quite different, especially since the documentation and examples seem to be available only in Kotlin. Also, I can't find any example which properly shows how the functions are used.

The documentation mentions setTapListener, TapListener. And then there are the functions onTap, onTapEvent and onTapCommand. This seems very confusing.

Could somebody put here a small example? Or point me to a working example on the internet?

Any help much appreciated!

1

There are 1 answers

0
Roy On

Implementing this seemed to work for me

https://developer.android.com/reference/androidx/wear/watchface/WatchFace.TapListener

My code:

class WatchFaceRenderer(...): Renderer.CanvasRenderer(...), WatchFace.TapListener {

     override fun onTapEvent(tapType: Int, tapEvent: TapEvent, complicationSlot: ComplicationSlot?) {

        if (tapType == TapType.UP)
            // do something
        invalidate()
    }
}

class Service : WatchFaceService() {

    override suspend fun createWatchFace(
        surfaceHolder: SurfaceHolder,
        watchState: WatchState,
        complicationSlotsManager: ComplicationSlotsManager,
        currentUserStyleRepository: CurrentUserStyleRepository
    ): WatchFace {

        val renderer = WatchFaceRenderer(
            context = applicationContext,
            surfaceHolder = surfaceHolder,
            watchState = watchState,
            currentUserStyleRepository = currentUserStyleRepository,
            canvasType = CanvasType.SOFTWARE,
        )

        return WatchFace(WatchFaceType.ANALOG, renderer).setTapListener(renderer)
    }
}