I have a WindowManager set up like this.
myWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
myWindowManager.addView(view, params);
I add a View object to a WindowManager like this. I want to move this WindowManager to the TOP Center. I am not sure how I can do this. Any ideas?
You can set Gravity for you view. i.e.
myWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
//edited
params.gravity=Gravity.TOP|Gravity.CENTER_HORIZONTA;
myWindowManager.addView(view, params);
You can also check this for more details.
Related
Goodmorning,
i'm developing an overlay which is posted by a service.
The problem is that the screen stays always on, even after screeen timeout expires.
This is my code:
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.TYPE_STATUS_BAR|WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity= Gravity.TOP | Gravity.LEFT;
overlay.setKeepScreenOn(false);
wm.addView(overlay, params);
I've already tried to add WindowManager.LayoutParams.FLAG_ALLOW_LOCK_WHILE_SCREEN_ON, but no luck.
Thanks for your help.
The solution is not to use WindowManager.LayoutParams.TYPE_STATUS_BAR
but one with lower privileges as this is only used by the system.
I am writing an android library that needs to display an activity on top of the client app. After some reading up it seems that adding a new window is what I want to do so I added in this permission.
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
I then try to add an image view by doing this
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.BOTTOM | Gravity.LEFT;
params.x = 10;
params.y = 100;
ImageView imageview = new ImageView(this);
imageview.setImageResource(R.drawable.avatar);
windowManager.addView( imageview , params);
But what I want is to have my own activity on this new window on top of the client apps window. is this even possible? How would I do this?
Ok the correct approach to this appears to be to inflate the layout with in the service class and add it to the windowmanager that way. Then create a regular java class that can be used as the event listeners for the actions within that layout. I didnt need to use fragments or activities.
mRootLayout = (LinearLayout) LayoutInflater.from(context).
inflate(R.layout.myContainerXML, null);
MyListManager myListManager = new MyListManager(mRootLayout,context);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.MATCH_PARENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
ListView listView = (ListView) mRootLayout.findViewById(R.id.listView);
final MyListAdapter adapter = new MyListAdapter(activity,R.layout.list_row,(ArrayList)myListManager.getList());
listView.setAdapter(adapter);
windowManager.addView(mRootLayout, params);
If anyone has a better way I would love to hear it thanks
LayoutInflater li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.RIGHT | Gravity.TOP;
myview = li.inflate(R.layout.locked_layout, null);
wm.addView(myview);
This is the code I have used for overlaying a layout over a screen. Only the HOME button is working. But the BACK button is not working. I want both to work. Am I missing anything? Please help me!! Thanks in advance
Add this flag as well my friend:
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
This worked for me!!
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
WindowManager windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
WindwoManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_FULLSCREEN |
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.RGBA_8888);
params.gravity = Gravity.RIGHT | Gravity.TOP;
When user start the application, I just want to show a button. User can continiue to use phone .
Example : (problem is icon is not clickable)
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen"
http://developer.android.com/reference/android/Manifest.permission.html#SYSTEM_ALERT_WINDOW
"android.permission.SYSTEM_ALERT_WINDOW" use this permission in main-feast
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
Button btn = new ImageView(this);
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);
windowManager.addView(btn, params);
I add view by addView() and I want to block screen orientation on that view to portrait.
I tried:
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.FILL_PARENT,
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_FULLSCREEN,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER | Gravity.TOP;
params.screenOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT;
LinearLayout glass = new LinearLayout(this);
glass.setBackgroundResource(R.drawable.glass6);
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.addView(glass, params);
It doesn't work.
How can I do it?
Also how can I do that my new view do not dissapear after app close?
Try this
glass.setOrintation(Configuration.ORIENTATION_LANDSCAPE)
Use android:screenOrientation="landscape" or android:screenOrientation="portrait" in your manifest file.To restrict your activity to be viewed in portrait or landscape mode.