9

How is it possible to add Views/Images to my homescreen?

The best example is the facebook messenger: If you long-click at a chat item, you can choose "pop out cheat head", then you have a small button overlaying your screen.

There are also apps like screen-breakers, an image from a broken display overlays everything from the phone.

I've searched for it, but I have no idea what it is called.

I hope you guys are able to understand my english.

EM-Creations
  • 4,195
  • 4
  • 40
  • 56
user2988203
  • 125
  • 1
  • 6

1 Answers1

13

Answer is SYSTEM_ALERT_WINDOW, this give you capability to draw over any thing in android, same feature facebook is using for drawing its chat heads.

http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW

Sample Code:

    private WindowManager windowManager;
    private ImageView imageView;
   // Get window manager reference

    windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

    imageView= new ImageView(YOUR_CONTEXT_HERE);
    imageView.setImageResource(R.drawable.android_head);

    // Setup layout parameter
    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.WRAP_CONTENT,
        WindowManager.LayoutParams.TYPE_PHONE,
        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
        PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.TOP | Gravity.LEFT; // Orientation
    params.x = 100; // where you want to draw this, coordinates
    params.y = 100;
    // At it to window manager for display, it will be printed over any thing
    windowManager.addView(chatHead, params);


   // Make sure to remove it when you are done, else it will stick there until you reboot
   // Do keep track of same reference of view you added, don't mess with that
   windowManager.removeView(imageView);
Techfist
  • 4,314
  • 6
  • 22
  • 32
  • Thank you, it works, but if I want to see it after closing the app, I have to use a service?! – user2988203 Dec 18 '13 at 13:25
  • Yes then you have to use it via service, please accept it as an answer then to help others :) – Techfist Dec 18 '13 at 13:55
  • I have another problem, I can remove the view with windowmanager.removeView(view), but it has no effect. A second try says that is is already removed, but I can still see it... – user2988203 Dec 18 '13 at 15:01
  • You might have messed up with instance, window manager is no where attached to lifecycle of activity or any other contextual items, so if you miss removing any attached view before context is removed its gone it will stick there unless u restart your device, this is what would have happened in your case, – Techfist Dec 18 '13 at 17:00