Skip to main content

Implement floating apps using WindowManager

Even novice Android developer knows that only foreground app can show and interact with the user and once user switches to another app, the previous one will be paused and hidden. However, what if , for whatever reason, you want develop an app that can always be displayed on top and can be interacted? 

WindowManager will do the job.

Here are the major steps and tricks to get it work properly: 
  • get WindowManager
           mWm = (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);
  • Add your root view as overlay using  WM.addView(), with correct layout parameter
          The trick here is the flags (*note: verify works on 4.3/4.4). Use exactly what I write below, otherwise you won't be able to receive the touch events. Will discuss it in a planed Android-Window-Deep-Dive article. 

           View yourRootView = new YourRootView();
           WindowManager.LayoutParams params = new WindowManager.LayoutParams(w, h,
                 WindowManager.LayoutParams.TYPE_PHONE,   //type                                                      WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH|                  WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,   //flag
                 PixelFormat.TRANSLUCENT);                         //format                                                   mWm.addView(yourRootView, params);
  • Specify overlay's position by setting gravity, x and y
          the (xoffset,yOffset) is absolute coordinate the top/left of your overlay view  

              params.gravity = Gravity.TOP | Gravity.LEFT;
              params.x = xOffset;
              params.y = yOffset;
  • Update overlay's position by set new layout parameters (xOffset,yOffset) using updateViewLayout
                mWm.updateViewLayout(yourRootView, params);
  • event handling :  use absolute value   
          The motion event called back (from either onTouch or onTouchEvent ) contains not only relative coordinate but also absolute coordinate. Knowing the later is a life-saver - I had spent long time trying to achieve a smooth
movement using the relative coordinate and finally failed miserably. 

           float x = event.getRawX();
           float y = event.getRawY();

This is short article but contains lots of hard learned tricks and tips. Hope you find it useful. Stay tuned for more about WindowManager - the not that famous but fundamental components in Android GUI framework.  Probably you already know WindowManager is also used when creating a normal Activity, and the view added is called decroView, which containing the status bar and navigation bar. 

Popular posts from this blog

Android Camera2 API Explained

Compared with the old camera API, the Camera2 API introduced in the L is a lot more complex: more than ten classes are involved, calls (almost always) are asynchronized, plus lots of capture controls and meta data that you feel confused about.

No worries. Let me help you out. Whenever facing a complex system need a little bit effort to understand, I usually turns to the UML class diagram to capture the big picture.

So, here is the class diagram for Camera2 API.

You are encouraged to read this Android document first and then come back to this article, with your questions. I'll expand what is said there, and list the typical steps of using camera2 API. 

1. Start from CameraManager. We use it to iterate all the cameras that are available in the system, each with a designated cameraId. Using the cameraId, we can get the properties of the specified camera device. Those properties are represented by class CameraCharacteristics. Things like "is it front or back camera", "outpu…

Java Collections Framework Cheat Sheet

Java Collections Framework (JCF) implements the Abstract Data Type  for Java platform. Every serious Java programmer should familiar himself on this topic and be able to choose the right class for specific need.  A thorough introduction to JCF is not the target of this small article and to achieve that goal you can start with this excellent tutorial . 

Instead, I'd like to
1) Provide an overview of JCF's classes ,   2) Provide a cheat sheet you can post in your cubicel for daily reference, 3) Underline the relationship between JCF's implementation and the data structure and algorithm you learned in your undergraduate course

With these goals in mind, I came up following diagram - Java Collection Cheat Sheet. You can click it to zoom in. There is no necessity for more explanation once your familiar with UML class diagram and have a basic understanding of common data structures.

Android Security: An Overview Of Application Sandbox

The Problem: Define a policy to control how various clients can access different resources. A solution: Each resource has an owner and belongs to a group.Each client has an owner but can belongs to multiple groups.Each resource has a mode stating the access permissions allowed for its owner, group members and others, respectively. In the context of operating system, or Linux specifically, the resources can be files, sockets, etc; the clients are actually processes; and we have three access permissions:read, write and execute.