android / wear-os-samples

Multiple samples showing best practices in app and watch face development on Wear OS.
https://developer.android.com/wear
Apache License 2.0
1.08k stars 561 forks source link

Disabling Complications #1140

Closed 3llomi closed 2 weeks ago

3llomi commented 3 weeks ago

Is there a way to enable/disable Watch Face complications based on a condition?

I'm working on a Watch Face where the user can customize it and I want to add the option for the user to enable or disable Complications.

Currently I have this code where I check if the complications are enabled, if so, then I can render (draw) complications, otherwise just don't render it.

 override fun render(
        canvas: Canvas,
        bounds: Rect,
        zonedDateTime: ZonedDateTime,
        sharedAssets: AnalogSharedAssets
    ) {
        if(complicationsEnabled){
            for ((id, complication) in complicationSlotsManager.complicationSlots) {
                complication.render(canvas, zonedDateTime, renderParameters)
        }
    }
        //other drawing code
}

Now this works, but the problem is that the watch face still detects the tap gesture on the complication itself even though the complication is not visible at all.

garanj commented 2 weeks ago

For this, you can use ComplicationSlotsUserStyleSetting:

https://developer.android.com/reference/androidx/wear/watchface/style/UserStyleSetting.ComplicationSlotsUserStyleSetting

This allows you to configure changes to the complications, such as positioning or visibility and should work for your use case.

3llomi commented 2 weeks ago

I'm not sure if I can control visibility using ComplicationSlotsUserStyleSetting. Is there any sample for this?

garanj commented 2 weeks ago

You should be able to set enabled accordingly : https://developer.android.com/reference/androidx/wear/watchface/style/UserStyleSetting.ComplicationSlotsUserStyleSetting.ComplicationSlotOverlay#ComplicationSlotOverlay(kotlin.Int,kotlin.Boolean,androidx.wear.watchface.complications.ComplicationSlotBounds,kotlin.Int,kotlin.Int,kotlin.Int)

3llomi commented 2 weeks ago

Thanks! it worked.