I am using Android window-manager to display an overlay image onto the users phone, but it is not setting the ImageView in the correct location, it always defaults to the center of the screen. I know the location is correct cause I have used accessibility services to touch the location and with show touches turned on in my phone it is touching the correct location. So why will the imageview not go to this location?
I get the location of the where I want to place the image from and imageview in my app:
Point centerPoint = getCenterPointOfView(myImageView);
int xImageLocation = centerPoint.x;
int yImageLocation = centerPoint.y;
And then I open the new overlay with this:
//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);
params.x = xImageLocation;
params.y = yImageLocation;
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
if (mWindowManager != null) {
mWindowManager.addView(mFloatingView, params);
}else {
return;
}
You need to set gravity of WindowManager.LayoutParams to Gravity.TOP | Gravity.LEFT.
As you've noticed by default its set to Gravity.CENTER, so with x=0 and y=0 your mFloatingView should be exactly in the middle of the screen.
Related
I made overlay view and I set this layout params like that.
DisplayMetrics matrix = new DisplayMetrics();
mManager.getDefaultDisplay().getMetrics(matrix);
mParams = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT
);
mParams.alpha = 0.8f;
mParams.gravity = Gravity.TOP | Gravity.LEFT;
mParams.x = 0;
mParams.y = 0;
But in this case, in tablet or fold model, overlay view is drawn from status bar.
Any other phone are not.. It didn't approach status bar..
how do i solve it??
I want to make overlay view is not drawn at status bar. Only under the status bar.
I put params.y to specific value but all tablet and foldable model have not same status bar.
So It is not what i want.
I am using window manager to show a popup from service, but my popup view is not visible. It is blank. Does someone have any idea why this is happening?
I have written android.permission.SYSTEM_ALERT_WINDOW permission in manifest too. My code looks like this.
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= 26) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else if (Build.VERSION.SDK_INT >= 22) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_TOAST;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
WindowManager.LayoutParams p = new WindowManager.LayoutParams(
// Shrink the window to wrap the content rather than filling the screen
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
// Display it on top of other application windows, but only for the current user
LAYOUT_FLAG,
// Don't let it grab the input focus
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
// Make the underlying application window visible through any transparent parts
PixelFormat.TRANSLUCENT);
// Define the position of the window within the screen
p.gravity = Gravity.TOP | Gravity.RIGHT;
p.x = 0;
p.y = 100;
windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
LayoutInflater layoutInflater =
(LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
binding = DataBindingUtil.inflate(layoutInflater, R.layout.activity_lock, null, false);
windowManager.addView(binding.getRoot(), p);
It requires 'draw over other apps' permission by the user. Please search in the settings for this permission and allow it for your app.
My Android (8.0+) app is implementing a narrow overlay along the bottom of the screen, but when a user sets an overscan value, it disappears. How can I make my view respect what is actually visible so Gravity.BOTTOM will not be clipped? I have tried countless combinations of flags, but nothing seems to ackknowledge the overscan value.
Point screen_size = new Point();
WindowManager.LayoutParams mParams;
WindowManager mWindowManager = getSystemService(WINDOW_SERVICE);
mWindowManager.getDefaultDisplay().getRealSize(screen_size);
layout = new LinearLayout(getApplicationContext());
layout.setOnTouchListener(this);
// color for debug.
layout.setBackgroundColor(0x55ff0000);
mParams = new WindowManager.LayoutParams(
screen_size.x,
40,
0,
0,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
mParams.gravity = Gravity.BOTTOM;
mWindowManager.addView(layout, mParams);
Hello i have this code to show popup window...
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
(width/100)*50, height, WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = (width/100)*50;
parameters.y = 0;
But i need to show it out of screen...
When i set x position to more then windows size it's stuck at corner and not go over screen...
Thanks.
Edit: Because some people don't know what i mean.. i mean half outside screen not full..
You need to add one more flag which is flag_layout_no_limits
final WindowManager.LayoutParams parameters = new WindowManager.LayoutParams(
(width/100)*50, height,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
parameters.gravity = Gravity.CENTER | Gravity.CENTER;
parameters.x = (width/2);
parameters.y = 0;
Any idea how to implement animation in status bar like in Mailbox app?
Link to the app
What I'm curious the most is the fade in & fade out animation and that the list can be scrolled during it.
Any help appreciated. Thanks in advance.
This works for me in android 4.4.4 and 5.0.
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);
TextView textView = new TextView(this);
textView.setText("I am on top of status bar");
textView.setBackgroundColor(Color.BLACK);
textView.setGravity(Gravity.CENTER);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
statusBarHeight,
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE|
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL|
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.TOP | Gravity.RIGHT;
windowManager.addView(textView, params);