In my previous blog, i have explained how to set up a project in Android studio for Google Glass. In this blog, we’ll dive into more practical stuff than theoretical stuff and we’ll learn how to create live and static cards and push them into Timeline for user to see.
If you have read my first blog on Google Glass GDK, you should be aware of live and static cards. But still before we start the coding, let’s talk in brief about both.
Live cards are for displaying the live data to user for example, weather or live score. Live cards are usually accept user input. User can interact with them by tapping on touch pad. Live card normally disappear automatically after a while, normally when data is less relevant or not latest anymore. It can also be removed using card id, which we should save somewhere while creating the card.
Static cards on the other hand does not accept user input and shows information that is less likely to change in future. Once you publish it on timeline, it is next to impossible to change the information displayed on it. And it also impossible to remove from timeline if your app haven’t saved the card id. I made the same mistake and end up adding tons of cards to timeline which i was unable to remove at all and i had to factory reset it.
TimelineManager is a place where you publish your cards for user to see. In other words, TimelineManager is a collection of live and static cards. From a coding point of view, it has mainly 4 main functions – insert, update, query and delete. They do exactly what their name indicates.
Enough talking, let’s code now –
Create a static card –
Step 1 – Get a timeline manager object first
TimelineManager timelineManager = TimelineManager.from(this);
Step 2 – Create a simple card object
Card staticCard = new Card();
Now Card class has three main function for setting an image, main text and foot note. These are –
staticCard.setImageLayout(); staticCard.setFootnote(); staticCard.setText();
Remember these things are optional. You can set all or none.
Step 3 – Add card to timeline
After this you should be able to see a card on your Timeline. But make sure you save the card id, in case you want to delete this card in future. If you don’t save the card id, you might never be able to delete the card as it is not supported by GDK at the moment. But i feel we will definitely get some sort of function in TimeLineManager class in future.
Create a live card –
String TimelineManager timelineManager = TimelineManager.from(this); LiveCard liveCard = timelineManager.createLiveCard("sample_card");
Now that you have created the card, its time to add some content to it. You can create your own custom view and add it to glass.
liveCard.setViews(new RemoteViews(this.getPackageName(), R.layout.your_custom_view));
Time to publish it now –
As i already mention, you can also make it accept user input. To do so –
Intent intent = new Intent(this, ActivityYouWantToLaunch.class); mLiveCard.setAction(PendingIntent.getActivity(this, 0, intent, 0));
Hope it helps.