Skip to content

React To Card Peek Events

Mitch Todd edited this page Jan 30, 2015 · 1 revision

You may want your watch face to react to a notification card appearing on the screen by adapting the layout. You can override the onCardPeek() method to be notified of this event and can further utilize the card size parameter for more precise adaptation. onCardPeek() will be called both when notification cards appear as well as when they are dismissed. When a card is dismissed, the 'rect' parameter will be populated with all 0's.

@Override
protected void onCardPeek(Rect rect) {
    // get the current formatted time string
    String timeText = getCurrentTimeText();

    // get height of visible area (not including card)
    float visibleHeight = getHeight() - rect.height();
    
    // get center point for time text
    float centerX = getWidth() / 2f;
    float centerY = visibleHeight / 2f;
    
    // draw time text in center of visible area
    mTimeTextPaint.getTextBounds(timeText, 0, timeText.length(), mTextBounds);
    mTimeTextCanvas.drawText(timeText, centerX, centerY +
            (mTextBounds.height() / 2), mTimeTextPaint);
}

Calls to onCardPeek() will be followed by calls to onTimeChanged() and onDraw() so any changes will be immediately recognized.