Search code examples
androidwear-osandroid-wear-complication

How do you make a complication adaptive?


I was following this tutorial on how to add complications to a watch face using ComplicationDrawable. Something I can't figure out is how do I make a ComplicationDrawable responsive to the content? For example, if the data type for the bottom complication is ComplicationData.TYPE_LONG_TEXT, how do I make my ComplicationDrawable wider to adjust for the text?

Here is my code for how I'm drawing the bounds for the complications.

I can't find an example of this anywhere so maybe it's not possible.


Solution

  • You've got everything you need in your code already. The only thing missing is to set the bounds based on the complication type. This is how I do it:

    // Create a ComplicationDrawable object, and give it a Context.
    ComplicationDrawable complicationDrawable = new ComplicationDrawable();
    complicationDrawable.setContext(context);
    
    // Set the ComplicationData from the onComplicationDataUpdate(int id, ComplicationData data) callback in your WatchFaceService.Engine class.
    complicationDrawable.setComplicationData(data); 
    
    // Set the bounds based on the current complication type.
    Rect bounds = new Rect();
    if (data.getType() == ComplicationData.TYPE_LONG_TEXT) {
        bounds.set(getLongTextBounds());
    } else {
        bounds.set(getShortTextBounds());
    }
    complicationDrawable.setBounds(bounds);
    
    // Set all other customization options, and draw the ComplicationDrawable to your canvas.