Skip to content

Instantly share code, notes, and snippets.

@makario
Created July 16, 2014 15:09
Show Gist options
  • Save makario/08476850ac87758bca2c to your computer and use it in GitHub Desktop.
Save makario/08476850ac87758bca2c to your computer and use it in GitHub Desktop.
Displaying custom notifications for Android Wear
package com.makario.wearplay;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
public class NotificationTest extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// First, create your custom notification activity as directed
// in the documentation. Create a PendingIntent for it.
Intent notificationIntent = new Intent(this, MainNotification.class);
PendingIntent notificationPendingIntent = PendingIntent.getActivity(this, 0, notificationIntent,
PendingIntent.FLAG_UPDATE_CURRENT);
// Here's where the magic lies: Use various Spans to create
// custom peeking notifications. Notification.WearableExtender#setContentIcon
// will also allow you to set an icon on inside of the card.
String title = "Viola!";
Spannable customTitle = new SpannableString(title);
customTitle.setSpan(new RelativeSizeSpan(1.8f), 0, title.length(), 0);
customTitle.setSpan(new ForegroundColorSpan(0xff83ae7b), 0, title.length(), 0);
Notification notificationBuilder = new Notification.Builder(this)
.setSmallIcon(R.drawable.ic_launcher) // you have to include this
.setContentTitle(customTitle) // you can do the same for #setContentText()
.extend(new Notification.WearableExtender()
.setDisplayIntent(notificationPendingIntent)
.setContentIcon(R.drawable.ic_launcher) // see also #setContentIconGravity()
.setHintHideIcon(true) // use this if you want to hide that small icon
.setCustomSizePreset(Notification.WearableExtender.SIZE_XSMALL))
.build();
NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder);
finish();
}
}
@LabInoCyril
Copy link

Hi!

I have two questions :

  • Does this Activity runs on Wearable or handheld device ?
  • Do you confirm that the title of that notification show up with a size of 1.8f on the wearable device ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment