Search code examples
google-glassgdk

Update text of card in Google Glass


My Glass app is very simple. I have one static card and I set its text and display it in the overridden "onCreate()" method of my Activity:

myCard.setText("First text");
View cardView = myCard.getView();
// Display the card we just created
setContentView(cardView);

I want to sleep for 5 seconds then display "Second Text" to the user. An earlier question on StackExchange discussed that you get a new view as above, and call setContentView() again.

This is fine so far, but my naive question is, where do I sleep and reset the content view? Obviously I can't sleep in "onCreate()" or "onStart()" of the activity, because the display has not been rendered for the user yet. I have a simple Service. In the service? Do I create a thread somewhere and use that? Thanks!


Solution

  • No need to start a new thread or sleep. You can do this with Android's Handler.postDelayed method, which posts a task to be executed on the UI thread at a later time.

    public class MyActivity {
        private Handler mHandler = new Handler();
    
        @Override
        protected boolean onCreate() {
            myCard.setText("First text");
            View cardView = myCard.getView();
            // Display the card we just created
            setContentView(cardView);
    
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    updateCard();
                }
            }, 5000 /* 5 sec in millis */);
        }
    
        private void updateCard() {
            // update the card with "Second text"
        }
    }