3

I need to display my view on top of dialer application, so I was using TYPE_PHONE for this purpose which is touchable, but still on some devices like Nexus 5 dialer application of device is hiding it.

Tried using TYPE_SYSTEM_OVERLAY makes view visible on top of dialer application, but touch is not there. Any help regarding this will be great.

patilmandar2007
  • 322
  • 2
  • 11

1 Answers1

12

After lot of searching for above problem, I found solution my self. Here it is how I made view to be on top of everything inside device and also making it touchable which was not possible with TYPE_SYSTEM_OVERLAY.

WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                        WindowManager.LayoutParams.MATCH_PARENT,
                        WindowManager.LayoutParams.WRAP_CONTENT,
                        WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
                        WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
                                WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
                        PixelFormat.TRANSPARENT);

params.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL;
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(view, params);
patilmandar2007
  • 322
  • 2
  • 11
  • Getting an error: "Caused by: android.view.WindowManager$BadTokenException: Unable to add window android.view.ViewRootImpl$W@d46dbf4 -- permission denied for this window type" OS 6.0 Marshmallow – Ahmad Shahwaiz Nov 03 '16 at 14:43
  • 1
    @AhmadShahwaiz For adding view to window we require to mention System level permission android.permission.SYSTEM_ALERT_WINDOW in manifest. And on Os 6.0 Marshmallow, we have to request permissions at Run Time before adding view to window like follows (As this is system permission) `if (!Settings.canDrawOverlays(this)) { Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName())); startActivityForResult(intent, OVERLAY_REQ_CODE); }` – patilmandar2007 Nov 04 '16 at 13:57
  • Yes, I'm doing it but the callback method is not getting called. From where can I check if user has given the permission. The below is not getting called. @TargetApi(Build.VERSION_CODES.M) @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); } – Ahmad Shahwaiz Nov 07 '16 at 07:36
  • 1
    @AhmadShahwaiz Above code is to be called using activity where we get callback in onActivityResult method. If you are checking above permission in Service then you might need to add a dummy transparent activity to be started from service, where you can ask user for Permission and then in onActivityResult dummy activity you will get result from user. Then you will require to notify your service to add view to window or not depending on user response. – patilmandar2007 Nov 08 '16 at 11:17