java – How to draw a view on top of everything?-ThrowExceptions

Exception or error:

I want to make an app that can create notification on the screen on top of anything that is currently being displayed. Something like the Go SMS message popup or something like the ChatHead in the following picture:

enter image description here

It would be even better if it is possible to draw it dynamically including touch events.What is the conventional or standard way to do this?


Like an Icon that can be clicked or dragged no matter whether you are on home screen or app drawer or other apps.Pay attention to the circular icons near the edges of the screen in the picture posted. You can drag them anywhere in any app.

How to solve:

What you are looking for is System Alert Window.

There’s a library called StandOut! which will assist you in creating such apps.


Here is how things like Toast and dialog windows work:

In the case where just adding or bringing to front does not work, say when you are having a service add its own view to another client activity or application (FaceUnlock does this), or you cannot depend on hierarchies, you need to use the window manager and a window token to do the trick. You can then create layouts and take advantage of animations and hardware acceleration as before.

    WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);

    WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(WindowManager.LayoutParams.FIRST_SUB_WINDOW);
    layoutParams.width = 300;
    layoutParams.height = 300;

    layoutParams.format = PixelFormat.RGBA_8888;
    layoutParams.flags =
                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
    layoutParams.token = getWindow().getDecorView().getRootView().getWindowToken();

    //Feel free to inflate here
    mTestView = new View(this);

    //Must wire up back button, otherwise it's not sent to our activity
    mTestView.setOnKeyListener(new View.OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
            return true;
    windowManager.addView(mTestView, layoutParams);

Then be sure to remove the view onDestroy (or onPause) or you will crash

   if (mTestView != null) {
       WindowManager windowManager = (WindowManager) getBaseContext().getSystemService(Context.WINDOW_SERVICE);
       if (mTestView.isShown()) {


You don’t need a new activity to do this. All you need to do is to add another view into your existing activity and bring it to the front, and draw/write the things that you want into that view.

If you want to do special things with this extra view, you could create your own view class

class DrawOnTop extends View { 
    public DrawOnTop(Context activity) { 

    protected void onDraw(Canvas canvas) {
        // put your drawing commands here


and then you do something like

DrawOnTop mDraw = new DrawOnTop(this);
addContentView(mDraw, new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

Then to force it to draw, you need to use mDraw.invalidate();


You could have the parent of your whole layout as RelativeLayout. The first child being the “root” of your main layout. Anything after that can be considered an overlay which is placeable to your whims.


    ... Main Layout here ...

  <TextView left="20dip" top="20dip" text="Overlay" alpha="0.7" />


The best way is to start a service with your application.
Create an ImageView.
Set the LayoutParams of the Image View.
Add the view along with the params to the window manager when the service is created.

Your Image sticks to your window (At any screen over all apps), till you application is closed.

You can even add onclicklisteners and ontouchlisteners to the imageview.
Eg. OnClick listeners to perform some actions and Ontouchlisteners move the image along the screen.

Leave a Reply

Your email address will not be published. Required fields are marked *