Android 4.2 – DayDream part 2 – How to create DayDream?

By -

This article is extension of my previous article about DayDream: What exactly DayDream feature is about?. Now i assume you understood about DayDream, so now i am proceeding further for developing a small example of DayDream.

Step 1: Create a sample project to build this demo example

I have created a simple project to give this demo, you can follow below steps to create DayDream for your Android application.

Step 2: Create a class and extends DreamService

FYI, DreamService is added in API Level 17 and you can it extend to create custom DayDream. LifeCycle of the DreamService is as follows:

Lifecyle of DreamService
onAttachedToWindow() Use this for initial setup, such as calling setContentView().
onDreamingStarted() Your dream has started, so you should begin animations or other behaviors here.
onDreamingStopped() Use this to stop the things you started in onDreamingStarted().
onDetachedFromWindow() Use this to dismantle resources your dream set up. For example, detach from handlers and listeners.

Here in this example i have just included TextView to display Hello World message, instead you can create your own XML layout and set it through setContentView() method inside onAttachedWindow() method.

package com.technotalkative.daydreamexample;

import android.service.dreams.DreamService;
import android.widget.TextView;

public class MyDreamService extends DreamService {
   public void onAttachedToWindow() {

       // Allow user touch

       // Hide system UI

       // Set the dream layout
       TextView txtView = new TextView(this);
       txtView.setText("Hello DayDream world from !!");
       txtView.setTextColor(Color.rgb(184, 245, 0));


Step 3: Declare this MyDreamService service in AndroidManifest file

This step is necessary to make our DayDream available to the system.

         android:label="TechnoTalkative - DayDream" >
            <action android:name="android.service.dreams.DreamService" />
            <category android:name="android.intent.category.DEFAULT" />

Step 4: Run this application

TechnoTalkative DayDream Example

Now, lets run this application, if it executes well then your DayDream will become available inside the Settings->Display->DayDream. Yes as we did nothing for Activity screen we will see nothing in app screen.

You can see TechnoTalkative – DayDream is available inside DayDream settings. Now select it and press START NOW it would be saying Hello DayDream world from 🙂


TechnoTalkative - DayDream example output

Download source code of this example: DayDream Example – 1

Paresh Mayani

Lazy android developer, exploring the horizon of android development since 7 years, currently working as a Technical Lead - Android at Simform Solutions, India. Former Application Architect at KarConnect. He is the Head/Organizer of Google Developers Group (GDG), Ahmedabad

Loading Facebook Comments ...
Loading Disqus Comments ...