Android Live Wallpaper using Processing in Android Studio

We've recently wanted to create an Android Live Wallpaper using Processing, which is an ideal language for writing these as it makes it easy to create exciting animations using a Java-like language

As it turns out, creating a live wallpaper from some existing Processing code is surprisingly simple

You can, of course, do this using the Processing environment as shown in the tutorial over on processing.org. This covers the basics but we wanted to incorporate this into our Android Studio app which took a few extra steps

Step 1 - Create a Processing sketch in your Android app

If you don't already have a Processing sketch, now's the time to write one! If you've written this using the Processing environment, you'll need to create a new class that extends processing.core.PApplet and import the processing-android library, similarly to the processing.org tutorial on creating a processing app in Android Studio

import processing.core.PApplet;

public class YourSketch extends PApplet {
  // your processing code
}

Step 2 - Create a Wallpaper Class

Next you need to create an Android wallpaper class that extends PWallpaper, a Processing wallpaper class. Tell your class to create a new instance of the sketch you create in Step 1

import processing.core.PWallpaper;

public class YourWallpaper extends PWallpaper {

  @override
  public PApplet createSketch() {
    return new YourSketch();
  }

}

This tells the wallpaper service to run your sketch when the wallpaper is accessed


Step 3 - Add your Wallpaper to the Manifest

Inside your AndroidManifest.xml, which you should have created as part if step 1, you need to add the wallpaper as a service

        <service
            android:label="@string/short_app_name"
            android:name="<package.class of your wallpaper>"
            android:permission="android.permission.BIND_WALLPAPER">
            <intent-filter>
                <action android:name=
                    "android.service.wallpaper.WallpaperService" />
            </intent-filter>
            <meta-data android:name="android.service.wallpaper"
                android:resource="@xml/wallpaper" />

        </service>

This tells Android that you want to create a wallpaper service and that you'll define it inside another xml file which we'll create in step 4

You need to bind the wallpaper permission using BIND_WALLPAPER. You also need tell Android what to display when the user views the list of wallpapers which we have done using a string resource called short_app_name but you can also just type the name in here


Step 4 - Define the Wallpaper

In step 3 we told the manifest that we'd define the wallpaper in another file under @xml/wallpaper

To do this

1. create a folder (if it doesn't exist) in your Android res folder called xml
2. create a file called wallpaper.xml
3. add the below to the file

<?xml version="1.0" encoding="utf-8"?>
<wallpaper
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:thumbnail="@drawable/icon"
    android:description="@string/short_app_name"

    />

This tells Android the name of the wallpaper and the icon to display in the wallpaper list. This is also where you would add any wallpaper settings if you want them

And you're done!

Comments

Popular posts from this blog

Launch a website as a mobile app using PhoneGap/Apache Cordova

How to manually add Cordova plugins to your app

Adding Multiple Google Accounts to Kindle Fire