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

Understand Container - Index Page

This is an index page to a series of 8 articles on container implementation. OCI Specification Linux Namespaces Linux Cgroup Linux Capability Mount and Jail User and Root Network and Hook Network and CNI
This page has a very good page view after being created. Then I was thinking if anyone would be interested in a more polished, extended, and easier to read version.
So I started a book called "understand container". Let me know if you will be interested in the work by subscribing here and I'll send the first draft version which will include all the 8 articles here. The free subscription will end at 31th, Oct, 2018.

* Remember to click "Share email with author (optional)", so that I can send the book to your email directly. 


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.

Understand Container: OCI Specification

OCI is the industry collaborated effort to define an open containers specifications regarding container format and runtime - that is the official tone and is true. The history of how it comes to where it stands today from the initial disagreement is a very interesting story or case study regarding open source business model and competition.

But past is past, nowadays, OCI is non-argumentable THE container standard, IMO, as we'll see later in the article it is adopted by most of the mainstream container implementation, including docker, and container orchestration system, such as kubernetes, Plus, it is particularly helpful to anyone trying to understand how the containers works internally. Open source code are awesome but it is double awesome with high quality documentation!