Easter Eggs in Android

Easter Eggs in Android

Easter eggs, as far as software is concerned, are hidden messages, jokes, or games hidden in an application—intended to be a delightful surprise to anyone who finds them. Easter eggs can put smiles on users faces, lead to free marketing, increased interest in your application, and quality reviews.

I’m sure you have heard of Google’s “do a barrel roll” Easter egg, but did you know that YouTube can do the harlem shake? On a Mac you can watch Star Wars: Episode IV in ASCII characters by running telnet towel.blinkenlights.nl in Terminal. Doing a search for “snake game” with Bing brings up the game in your search results. There are hundreds of fun hidden Easter eggs in popular software.

I’ve hidden some Easter eggs in apps I have developed in the past. My first was in December of 2010 when I was working on a project, Droid Overclock, with Matthew Oaks. We hid several golden eggs, as found in Angry Birds, inside our app. Our utility-based app became a mini-game for a week as our users tried to find all our hidden golden eggs. We received a lot of good feedback from users and Rovio Mobile started following my Twitter account.

ANDROID LOVES EASTER EGGS

Starting with Android version 2.3 (Gingerbread) Google started adding an Easter egg into each release. You can find the Easter egg by going into Settings, then About phone, and then rapidly tapping on Android version.

I enjoyed the Android 5.0 Easter egg so much that I backported it to Android 4.1, added a few more features, and published it as a game to Google Play. The game still has a couple thousand daily active users.

TUTORIAL

Adding an Easter egg to your Android app is easy and fun. I created a sample app that shows a random Chuck Norris joke after rapidly tapping on the app version. Feel free to add it to your app and make improvements.

To add the Easter egg in your app, first, you will need to create a layout to display the app version. In the example, I have chosen to do this in a PreferenceFragment. The user will need to click on the app version 7 times to unlock the Easter egg:

private static final int MAX_CLICKS_TO_UNLOCK_EGG = 7;

private Preference prefVersion;
private int numTimesVersionClicked;

/* .. */

@Override public boolean onPreferenceClick(Preference preference) {
  if (preference == prefVersion) {
    if (++numTimesVersionClicked == MAX_CLICKS_TO_UNLOCK_EGG) {
      numTimesVersionClicked = 0;
      // The user unlocked our Easter egg!
      new EasterEggTask(getActivity()).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
    }
  }
  return true;
}

The class EasterEggTask gets a random Chuck Norris –joke– fact:

/**
 * Loads a random Chuck Norris nerdy joke and displays a toast message.
 */
public class EasterEggTask extends AsyncTask<Void, Void, String> {

  private static final OkHttpClient CLIENT = new OkHttpClient();

  private final WeakReference<Activity> weakActivity;

  public EasterEggTask(Activity activity) {
    weakActivity = new WeakReference<>(activity);
  }

  @Override protected String doInBackground(Void... params) {
    // Request a Chuck Norris joke. See http://www.icndb.com/api/
    Request request = new Request.Builder().url("http://api.icndb.com/jokes/random?limitTo=[nerdy]").build();
    try {
      Response response = CLIENT.newCall(request).execute();
      if (response.isSuccessful()) {
        return new JSONObject(response.body().string()).getJSONObject("value").getString("joke");
      }
    } catch (Exception e) {
      Log.e(TAG, "Error getting a random nerdy joke", e);
    }
    return null;
  }

  @Override protected void onPostExecute(String joke) {
    Activity activity = weakActivity.get();
    if (activity != null && !activity.isFinishing() && !TextUtils.isEmpty(joke)) {
      Toast.makeText(activity.getApplicationContext(), Html.fromHtml(joke), Toast.LENGTH_LONG).show();
    }
  }

}

That was easy and fun. Now you have a simple Easter egg that displays a random Chuck Norris joke. You can download the project from my GitHub. Also, check out these other Android Easter eggs. Happy coding.

Comments