PocketMagic

PocketMagic

Where Technology meets magic


Android
45 Posts
BlackBerry
4 Posts
Electronics
68 Posts
Hardware
120 Posts
High Voltage
49 Posts
iPhone
4 Posts
Linux
2 Posts
Nuclear
20 Posts
Optics
11 Posts
Photography
7 Posts
Photoshop
3 Posts
Research
19 Posts
Reviews
18 Posts
Robotics
8 Posts
Security
7 Posts
Software
73 Posts
Symbian
2 Posts
Tubes
18 Posts
Windows
3 Posts
Windows Mobile
11 Posts

Top Articles!       See PocketMagic on Facebook


uRADMonitor - Online Radiation monitoring station | 14797 Views | Rate 70.46
uRADMonitor - Online Radiation monitoring station
Bluetooth and iOS - Use Bluetooth in your iPhone apps | 17697 Views | Rate 57.64
Bluetooth and iOS - Use Bluetooth in your iPhone apps
NMEA GPS Library for AVR Microcontrollers | 4768 Views | Rate 57.45
NMEA GPS Library for AVR Microcontrollers
Developing for Blackberry 10 | 54 Views | Rate 54
Developing for Blackberry 10
Building a robot – Part 2 | 4525 Views | Rate 44.8
Building a robot – Part 2
Programmatically Injecting Events on Android - Part 2 | 4836 Views | Rate 44.37
Programmatically Injecting Events on Android - Part 2
Simple Switched power Supplies | 15827 Views | Rate 41.32
Simple Switched power Supplies
Repairing a Victoreen CDV-700 6B Dosimeter  | 123 Views | Rate 41
Repairing a Victoreen CDV-700 6B Dosimeter

 
  

Android Overlay Mouse Cursor


By Radu Motisan Posted on July 27th, 2012 , 4468 Views (Rate 15.09)



  




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 Overlay.

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.

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

  1.  
  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);
  10.  

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.






  

More on PocketMagic:

How to make metallic sodium | 665 Views | Rate 30.23
How to make metallic sodium
Tube: USN-5J29 | 309 Views | Rate 28.09
Tube: USN-5J29
Programmatically Injecting Events on Android - Part 1 | 9961 Views | Rate 25.87
Programmatically Injecting Events on Android - Part 1
Atmega8 and enc28J60 for ethernet support | 7109 Views | Rate 24.26
Atmega8 and enc28J60 for ethernet support
How to set the AVR Fusebits | 1647 Views | Rate 23.87
How to set the AVR Fusebits
Tube: 1B40 | 235 Views | Rate 23.5
Tube: 1B40

41 Responses to “Android Overlay Mouse Cursor”

  1. 1
    Mike:

    Default HVGA? Can it change resolution?

  2. 2
    Radu Motisan:

    ??

  3. 3
    Mike:

    set params.width in service’s onCreate()

  4. 4
    Radu Motisan:

    change it to fit your needs

  5. 5
    Jesper:

    Thanks for a great article!

  6. 6
    Radu Motisan:

    I’m glad this helps.

  7. 7
    Surbhi Jain:

    Great Article ! thanks

  8. 8
    Radu Motisan:

    @Surbhi: you’re welcome. what are you working on?

  9. 9
    Thomas:

    Hello,

    is it possible to ensure that the cursor is positioned at the location where the user touches the screen?

    Thank you in advance

  10. 10
    Radu Motisan:

    @Thomas, yes, it is possible, feel free to contact me for more details.

  11. 11
    Rafael Orágio:

    Is it possible generate a “tap screen” using this cursor?

    Thanks!

  12. 12
    Radu Motisan:

    @Rafy yes, it is possible, see: http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android

  13. 13
    Michael James:

    Just what I need! Thank you for this sample application. It really helps me a lot. I appreciate it so much. You don’t know how much it means to me :D Once again, thanks.
    FYI, I’m creating application to control the mouse with camera for my thesis. Wish me luck :D

  14. 14
    Radu Motisan:

    @Michael: Good luck with your thesis. Would love to see your project . Did you have the chance to see the “HeadMouse” project? It is still available as open source.

  15. 15
    Michael James:

    @Radu: Thank you Radu. I’ll let you see as soon as it finishes :D Haven’t seen the project. Is it yours? Where can I see it? Thanks.

  16. 16
    Programmatically Injecting Events on Android – Part 2 « PocketMagic:

    [...] custom mouse pointer on top of your Android screen, for application control purposes, as presented here, that also needs to be able to inject the touch events when the user clicks. Not to mention various [...]

  17. 17
    Graham:

    Is it possible to get the standard mouse cursor arrow position as in framework and using the overlaying to drawn new cursor over it. I cannot fine any means of getting the mouse coordinates (x,y)

  18. 18
    Radu Motisan:

    @Graham, yes it is possible. I already did this for a client. Drop me a mail for more details.

  19. 19
    Graham:

    Thanks Radu, We are trying to resolve a problem for disabled users to give them a bigger, brighter mouse arrow instead of the stock one which is difficult to see. I would appreciate more details as you have offered. Can you send to my email address, thanks.

  20. 20
    Ragunath Jawahar:

    Hi Radu, I have a question. How do I capture events from an IR remote and deliver them to the overlay?

  21. 21
    Daniel Leal:

    Hello Radu,

    Thanks for showing your work, it’s really great!

    I’m trying to use it to have a remote touchpad app between android devices, and I have a problem. The cursor does not cover the whole screen, it dissapears on the right hand side. What’s strange is that it cover the complete height of the screen, the problem it’s just width-wise. Do you know why this could be happening? I have tried your project from the scratch and also have tried adapting it for my code; same result in both cases.

    Thank you very much in advance!

  22. 22
    Radu Motisan:

    @Ragunath: are you working for Math R.?

    @Daniel: in my code find:

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.WRAP_CONTENT,
    WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,

    Change WRAP_CONTENT to MATCH_PARENT, or the number of pixels representing your Screen_width. How do you plan to inject the clicks?

  23. 23
    Daniel Leal:

    @Radu: Thank you very much, it works! For the click injection I’m using your second method from http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/ . I am working with a rooted device, so I can install the app with system signature, as a system app, and use the INJECT_EVENTS permission.

    By the way, the cursor is not showing above the system bar, though I have the FLAG_LAYOUT_IN_SCREEN flag. These are the params I’m using:

    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
    WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.MATCH_PARENT,
    WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,//TYPE_SYSTEM_ALERT,//TYPE_SYSTEM_OVERLAY,
    WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE |
    WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN //will cover status bar as well!!!
    ,PixelFormat.TRANSLUCENT);

    Really nice work, keep it up!

  24. 24
    Radu:

    @Daniel: double check my code. As you seen in the video, I have no problem in getting the cursor above the system bar.

  25. 25
    Daniel Leal:

    @Radu: I’ve seen the video, but there’s no system bar in it. By system bar I’m refering to the bar that appears from ICS at the bottom of the screen, with the BACK, HOME and RECENT buttons. I can inject events there (click on those buttons), but no pointer is shown.

  26. 26
    Radu:

    @Daniel, are you referring to a tablet? Better drop me a screen capture showing this issue. I didn’t have such an issue, and I recall testing the custom pointer on lock screen (the pull ring screen), on the notifications screen, titlebar, etc. But it is true I don’t remember testing on a tablet.

  27. 27
    Daniel Leal:

    @Radu: sorry, for the delay, took the weekend off, hehehe. Yes, I’m referring to a tablet. Should we communicate through email better? To avoid filling the comments section. It would be easier to send you the image too.

    Oh, by the way, I was trying to inject multitouch events, to simulate a pinch for instance, but although the events are injected (shown in developer mode), no pinch gesture is recognised by, for instance, Google Maps. Do you know why this could happen?

    Thanks!

  28. 28
    Daniel Leal:

    I succeeded with the multitouch injection. The key was to set the ‘pressure’ field from the PointCoords to 1. It seems that by default it is considered as 0, so no “touch” is computed after all.

    Still working on the ‘cursor over system bar’ mission :P

  29. 29
    Ovidiu:

    Buna Radu, mersi de solutie, as avea o intrebare, incercam sa trimitem un event de click din overlay catre android prin partea de software, ai ideie cum am putea asta fara a avea permisiune de root la sistem?

    Mersi mult

  30. 30
    Radu Motisan:

    Salut Ovidiu,

    Citeste http://www.pocketmagic.net/2012/04/injecting-events-programatically-on-android/ si http://www.pocketmagic.net/2013/01/programmatically-injecting-events-on-android-part-2/

    Fara root nu se poate. Ar putea exista alte alternative, dar depinde de ceea ce incerci sa faci. Asa ca poate vrei sa dai mai multe detalii.

    Radu

  31. 31
    Ovidiu:

    Mersi de raspuns Radu! Ce am vrea sa facem e exact ce ai expus aici, o aplicatie software care sa simuleze un mouse pe ecran fara a avea conectat un mouse la exterior, mouse-ul fiind simulat pe ecran de aplicatie si controlat doar prin touch screen

  32. 32
    Radu Motisan:

    Salut Ovidiu,

    Poti folosi codul acesta sau cel pentru injectare de evenimente, gratuit, dar numai in aplicatii necomerciale.
    Din pacate la partea de injectare ai nevoie de root.

    Succes

  33. 33
    Aruna:

    I need to get the TYPE_SYSTEM_ALERT or similar overlay type that stay on top of all the other window and also gets the motion events. At the same time passes the motion events to any overlay under the top layer. Is it possible to do this? If so can you please point me to a good documentation or show me how to do this. Thanks in advance.

  34. 34
    Radu Motisan:

    @Aruna yes it is possible, see the video at the bottom of this article.

  35. 35
    Aruna:

    Thanks for the reply. Is this just by getting the correct LayoutParams combination or does it need event injections as well. I’ve tried different LayoutParams combinations but failed to pass the events to the layers under the top layer as the top layer getting the focus.

  36. 36
    Radu Motisan:

    @Aruna I sent you an email.

  37. 37
    Shawn:

    Hey Mr.Radu. The sample codes you provided seems have some problem with resolution. I assume the overlay width and height is define with params but change it does not affect the cursor reachable area. What I am doing is connect two device (with different resolution) and one device send the co-ordinates to update the position of cursor. However I am not sure why the cursor area is automatically set as client and limit the cursor reachable area (480*800, use to send co-ordinate. and server is 720 * 1280). Do you know what cause this happen and know the possible solutions?
    Sincerely,Shawn

  38. 38
    Shawn:

    And I try connected two devices with (480*800 and 480*840 resolutions) the cursor reachable area at 480*840′s device is 480*800 also

  39. 39
    Shawn:

    I am sorry….it’s my own mistake…..I forgot I have set the update border to avoid cursor update to outside screen and forgot verfiy the algorthm. looks this is the problem. I’m Sorry for disturb.

  40. 40
    wangpeng1:

    can you share the mouse control the cursor overlay?

  41. 41
    Mouse control the mobile Cursor overlay - How-To Video:

    [...] . but the author only make overlay on the mobile screen. the link is http://www.pocketmagic.net/2012/07/an…; . i want to know how to finished it,can you give some advice. if i need some c code of android [...]

Thank you for commenting. Your comment won't show until approved, which can take some time.

Please copy the string vYz6QI to the field below: