Search code examples
androidandroid-layoutwear-oswatch-face-api

Alignment on Android Wear Watch Face


I am new to android watch face development and facing some problem with alignment of time text! I am displaying time in two format, How can I display it one at top and one at bottom and center aligned as shown in image below? Is there any pattern or layout for this?

enter image description here


Solution

  • It's up to you to figure out the alignment yourself when calling Canvas.drawText in your onDraw handler. I use code something like this:

    String msg = "Text to display";
    float width = paint.measureText(msg);
    canvas.drawText(msg, centerX - width / 2f, top, paint);
    

    where paint, top, and centerX were all initialized previously. The first two will depend on your face design, while the third is a Point field that can be set in your onCreate handler like so:

    Display display = ((WindowManager) getSystemService(Context.WINDOW_SERVICE))
                    .getDefaultDisplay();
    display.getSize(displaySize);
    centerX = displaySize.x / 2f;
    

    Alternately, you could do all of this in XML with a layout-based watch face, but this may or may not be a good fit for your needs.