Sticky overlay without WindowManager.LayoutParams.TYPE_PHONE

By sticky I mean a window that doesn't get closed by calling the launcher intent (intent.addCategory(Intent.CATEGORY_HOME).

Previously this was done with WindowManager.LayoutParams.TYPE_PHONE, but this type is now deprecated and throws an exception on api 28:

WindowManager$BadTokenException ... permission denied for window type 2002

The behavious is still possible since Facebook's Messenger does it with its chat "Heads", based on the assumption that facebook doesn't get system app permissions since it's pre-installed on a lot of roms.

Using WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY doesn't work (i.e. pressing the home button also hides the overlay window).

Edit: The question is how to have an overlay window that doesn't get removed when user clicks the home button / calling the launcher intent. It's not the case for TYPE_APPLICATION_OVERLAY, it was the case for TYPE_PHONE but that's deprecated.

Edit 2: Apparently this does work for some people, this is the code I'm running:

class MyClass {

    var params: WindowManager.LayoutParams = WindowManager.LayoutParams(
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            else WindowManager.LayoutParams.TYPE_SYSTEM_DIALOG,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT
    ).apply {
        windowAnimations = android.R.style.Animation_Dialog
        gravity = Gravity.CENTER or Gravity.CENTER
        x = 0
        y = 0
    }
    var windowManager: WindowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

    init {
        val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
        rootView = layoutInflater.inflate(R.layout.view_overlay, null)
        windowManager.addView(rootView, params)
    }

}

WindowManager.LayoutParams, By sticky I mean a window that doesn't get closed by calling the launcher intent ( intent.addCategory(Intent.CATEGORY_HOME ). Previously� 8 Sticky overlay without WindowManager.LayoutParams.TYPE_PHONE; 6 Mandatory Manifest-Version attribute missing; 5 4 pin jack not detecting microphone on Ubuntu;

In Manifest add permission

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

In the launcher activity you should ask for canDrawOverlays Permissions`

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && 
 !Settings.canDrawOverlays(this)) {


        //If the draw over permission is not available open the settings screen
        //to grant the permission.
        Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
                Uri.parse("package:" + getPackageName()));
        startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
    } else {
        initializeView();
    }

Then inside your service

 //Inflate the floating view layout
    mFloatingView = 
    LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);

    int LAYOUT_TYPE;
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
        LAYOUT_TYPE = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
    } else {
        LAYOUT_TYPE = WindowManager.LayoutParams.TYPE_PHONE;
    }
    //Add the view to the window.
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            LAYOUT_FLAG ,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    //Specify the view position
    params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
    params.x = 0;
    params.y = 100;

    //Add the view to the window
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mWindowManager.addView(mFloatingView, params);

This won't get dismissed as the user press Home Key.

Make sure that your floating widget is defined as service otherwise it can't survive when there is memory low/ configuration change kind of issues. Also register the service inside the manifest

   <service
            android:name=".MyWidget"
            android:enabled="true"
            android:exported="false"/>

Android displaying floating widget overlay not working, Window flag: this window won't ever get key input focus, so the user can not send key or other button events to it. Window type: Application overlay windows are displayed above all activity An alpha value to apply to this entire window. 8 Sticky overlay without WindowManager.LayoutParams.TYPE_PHONE; 6 Mandatory Manifest-Version attribute missing; View more network posts → Top tags (7) headphones

Solution: Don't inflate your overlay directly over other apps.

Normally, using TYPE_APPLICATION_OVERLAY still behaves as TYPE_PHONE, the issue with my case was that I was displaying my view (windowManager.addView(rootView, params)) when an activity from another app was in the foreground, and Android attaches the view to the current foreground activity, so when the user exits that activity (by pressing the home button for example), the system also kills the overlay from my app. A solution for me was very specific, I don't have a general solution but with this information should help people finding one.

Floating Chat head like facebook in android using Kotlin, Android displaying floating widget overlay not working LayoutParams( WindowManager. TYPE_PHONE, WindowManager. setText(data); return START_NOT_STICKY; } private boolean isViewCollapsed() { return� 8 Sticky overlay without WindowManager.LayoutParams.TYPE_PHONE Mar 19 '19 8 Android: invert RecyclerView positions Apr 18 '16 8 What is orientation constants for views?

User John Sardinha, TODO("not implemented") //To change body of created functions use File | Settings override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int { Also, we need to initialize the Window Manager layout params. Now we can achieve the overlay but for handling the touch event on that popup� Video Overlay. implement video overlay with exoPlayer in your application step by step , WindowManager. LayoutParams. TYPE_PHONE return START_NOT_STICKY

core/java/android/view/WindowManagerPolicy.java, 8 Sticky overlay without WindowManager.LayoutParams.TYPE_PHONE � 6 Mandatory 2 4 pin jack not detecting microphone on Ubuntu May 10. 0 Static� An element with position: sticky; is positioned based on the user's scroll position.. A sticky element toggles between relative and fixed, depending on the scroll position.It is positioned relative until a given offset position is met in the viewport - then it "sticks" in place (like position:fixed).

Get view of AccessibilityNodeInfo to create overlay, you may not use this file except in compliance with the License. * You may obtain a copy of WindowManager.LayoutParams. These flags are also defined in frameworks/base/include/ui/Input.h. Sticky broadcast of the current HDMI plugged state. */ @param overlayFrame The frame within the display that is inside. A few CSS declarations and I was able to have a sticky header. Here are three simple steps: Find the correct style so you can declare the element as sticky using position:sticky; (don’t forget browser prefixes like position: -webkit-sticky;). Choose the “sticky edge” (top, right, bottom, or left) for the item to “stick” to.

Comments
  • User needs to grant permission to "draw over other apps" per application basis. It's up to ROM implementation to put in exceptions like granting it to Messenger by default. You can quickly check if it's granted for your app by using Settings.canDrawOverlays().
  • @ManojPerumarath 28
  • Duplicate: stackoverflow.com/questions/46208897/…
  • Hey, try my updated answer
  • @Maxouille I'm not getting the "permission denied" error, nor any error, it's not the same question as that.
  • This isn't different from what I have.
  • @JohnSardinha did you tried this code ? and if its not working let me see bcz i have an application which is runnning on playstore with this code
  • I tried it, it didn't work, what sdk version are you targeting?
  • compileSdkVersion 28 minSdkVersion 19 targetSdkVersion 28
  • When you have your overlay being displayed, and you press the home button, it doesn't close your overlay?
  • Tried this on API 28 and it doesn't work, i.e. it gets dismissed when pressing Home
  • I tried this on Oreo and Pie, still works for me. Please post your code
  • This does not work for soft home button. For example pixel devices.
  • @user2511882 I had tested it on Pixel and POCO F1, still working
  • Adding android:enabled="true" and android:exported="false" to the service doesn't work