22

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?

Example:

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.

Daniel
  • 2,355
  • 9
  • 23
  • 30
Allahjane
  • 1,920
  • 3
  • 24
  • 42
  • See answer to this question: http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android – Jules Apr 28 '13 at 15:52
  • So transparent wont consume all touch events? – Allahjane Apr 28 '13 at 15:56
  • Yes, but that's usually what you want if you're drawing over the top of another activity. Dismiss your view if the user touches outside it would be my advice. – Jules Apr 28 '13 at 15:58
  • Did you saw the example app link I attached ? It consumes only those event that happens on touching it all other apps behave normally – Allahjane Apr 28 '13 at 16:00
  • 1
    Any success? I also need it. – Vitali Pom Aug 24 '13 at 19:46
  • 1
    http://stackoverflow.com/questions/16559642/android-how-to-draw-free-on-top-of-anything-any-activity asked by the same person, but it does have the answer. – sam Feb 14 '14 at 05:45

5 Answers5

12

What you are looking for is System Alert Window.

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

rhoadster91
  • 354
  • 2
  • 12
12

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_LAYOUT_IN_SCREEN
                    | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED;
    layoutParams.token = getWindow().getDecorView().getRootView().getWindowToken();

    //Feel free to inflate here
    mTestView = new View(this);
    mTestView.setBackgroundColor(Color.RED);

    //Must wire up back button, otherwise it's not sent to our activity
    mTestView.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                onBackPressed();
            }
            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()) {
            windowManager.removeViewImmediate(mTestView);
       }
   }
Joel Teply
  • 3,260
  • 1
  • 31
  • 21
9

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) { 
        super(activity);
    }

    @Override 
    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));
mDraw.bringToFront();

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

Stochastically
  • 7,616
  • 5
  • 30
  • 58
  • I mean Like an Icon that can be clicked or dragged no matter whether you are on homescreen or app menus or other apps – Allahjane Apr 28 '13 at 15:58
  • I've use what I've posted to draw text and bitmaps on top of the main view in an activity. My guess is that you could use it to do what you're suggesting as well. – Stochastically Apr 28 '13 at 16:04
  • Sorry I think I put my question wrong way! I want to draw over other apps not on my own – Allahjane Apr 28 '13 at 16:05
  • @Stochastically, You don't know where there is s ample implementation of your code do you? I've been trying my own version for ages and can't get it to draw on top of the view. – Gurfuffle Jun 27 '14 at 11:35
  • @Gurfuffle I've not been involved with Android coding since last year so not sure where the code is any more. Sorry! – Stochastically Jun 30 '14 at 15:16
  • Question asks how to draw over everything including other apps, not just the current Activity. Your example is only for drawing over the current Activity. – user1608385 Feb 09 '23 at 18:00
1

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.

Example:

<RelativeLayout>
  <LinearLayout>
    ... Main Layout here ...
  </LinearLayout>

  <TextView left="20dip" top="20dip" text="Overlay" alpha="0.7" />
</RelativeLayout>
Knossos
  • 15,802
  • 10
  • 54
  • 91
0

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.

ALL SET 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.

Sujith Royal
  • 762
  • 10
  • 9