Reputation: 2351
I need one view to always be at the forefront, even over alert dialogs. I figured there might be two ways to achieve this:
Is this possible? If so how?
Upvotes: 1
Views: 622
Reputation: 2838
WindowManager with LayoutParams and Flag as TYPE_SYSTEM_ERROR
. Shows over lockscreens too.
WindowManager windowManager = (WindowManager)getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.LEFT;
params.x = 100; // position on screen (optional)
params.y = 100;
Add Views:
windowManager.addView(yourView, params);
Edit:
To avoid covering LockScreens: remove TYPE_SYSTEM_ERROR
flag and add following:
WindowManager.LayoutParams.TYPE_PHONE
For this to work, you will need to add the following permission to your AndroidManifest.xml too:
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
You don't need to request it at Runtime on most of devices except Android Marshmallow 6.0.0. There is a special way to request it than regular requests. It is checked using:
if (Settings.canDrawOverlays()) {
// you have permission
} else {
// send an intent with action "ACTION_MANAGE_OVERLAY_PERMISSION"
}
Upvotes: 1