For an application developer it is important to have total freedom in shaping the result of his work.
Even if Android OS has been designed without the mouse cursor in mind, there are plenty of situations where a developer needs a cursor on top of other applications installed.

We can draw the cursor at the framebuffer level, and while this provides optimum performance, it requires special privileges that regular apps don't have.
The second approach , and the subject of this article, is drawing a cursor using an Android Overlay cursor.

What is an overlay?
The SDK defines them as: "Window type: system overlay windows, which need to be displayed on top of everything else. These windows must not take input focus, or they will interfere with the keyguard. "

So what we are about to do is have a small view created, make it show a cursor image, and move it around.
To make things easier, I created a special service class, since the code that displays a cursor would probably be a service , running in the background, and accepting cursor position updates.

Sample application
You can download the sample application here. You can use the code in free or non commercial applications.

Unless your AndroidManifest.xml contains the SYSTEM_ALERT_WINDOW permission, the code will fail:

  2. WindowManager.LayoutParams params = new WindowManager.LayoutParams(
  3. WindowManager.LayoutParams.WRAP_CONTENT,
  4. WindowManager.LayoutParams.WRAP_CONTENT,
  5. WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
  6. WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
  7. WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
  8. WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
  9. PixelFormat.TRANSLUCENT);

To make the cursor be able to cover the status bar, and have it's coordinates starting from screen's (0,0) leftmost and topmost point, you will need to specify the .FLAG_LAYOUT_IN_SCREEN flag.

Hope this code proves useful. Looking forward to your comments, questions and feedback,

Customizing the system's pointer

It is possible to detect the system mouse coordinates and draw a custom overlay on top of the system mouse cursor. This way we can increase the pointer image size, a very useful enhancement for those that have problems seeing the tiny default cursor. Here is a demo showing a bluetooth mouse cursor and our bigger custom mouse cursor overlay.

Demo #1

Demo #2

Demo app on Google play

Based on popular request, I finally developed a freeware application to customize the mouse cursor and provide a bigger cursor on Android OS, that runs on Android 4.0 or newer and doesn't need root!. It's named BigMouse and you can find it on Google Play here.
This app can be used both with Bluetooth Mouses and with wired mouses connected over OTG enabled USB.

Related Post