Having application running above other app
Asked Answered
O

2

9

I want to make an activity that can be opened above ANY app.

Normally, even when the activity is set as dialog, when you switch to my app, you see my app, and in the background you see the launcher:

BUT, I want the app will go above any app like this: (made in photoshop):

I did see this question Creating a system overlay window (always on top), but in ICS there is no functionallity to the layout. Furthermore, I want to give a dialog box from my app without minimizing the other app...

Ozell answered 22/4, 2012 at 9:55 Comment(1)
have you found the solution. If yes please help me on this.Bufordbug
D
23

there are plenty of apps that show a floating view on top of everything like : airbrowser , LilyPad , Stick it , AirTerm , Smart Taskbar , aircalc ...

anyway , in order to achieve this feature , you must have a special permission called "android.permission.SYSTEM_ALERT_WINDOW" , and use something like that:

final WindowManager.LayoutParams param=new WindowManager.LayoutParams();
param.flags=WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
final View view=findViewById(R.id.my_floating_view);
final ViewGroup parent=(ViewGroup)view.getParent();
if(parent!=null)
  parent.removeView(view);
param.format=PixelFormat.RGBA_8888;
param.type=WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
param.gravity=Gravity.TOP|Gravity.LEFT;
param.width=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().width;
param.height=parent!=null?LayoutParams.WRAP_CONTENT:view.getLayoutParams().height;
final WindowManager wmgr=(WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
wmgr.addView(view,param);
// TODO handle overlapping title bar and/or action bar
// TODO you must add logic to remove the view
// TODO you must use a special permission to use this method :android.permission.SYSTEM_ALERT_WINDOW
// TODO if you wish to let the view stay when leaving the app, make sure you have a foreground service running.
Deity answered 22/4, 2012 at 11:0 Comment(20)
And, will the view be functional even in ICS?Ozell
sure . i've even tested it about a month ago . it's not that it's deprecated or anything . just be sure to handle the special cases i've written about . note that some of the things i've written are optional (like the "param.format" part, which makes the background transparent ) . feel free to play with them . about the effect of the blurring , i'm not familiar with that , but surely you can search how to do that.Deity
I have only one problem. I want to create an floating EditText, but when you click it, it doesn't show the keyboard...Ozell
well that's interesting . if you solve this please write it down too. i have a workaround for this: upon clicking on the editText , start an invisible activity that will let you edit the editText. i think it should work.Deity
I thought on even better idea, showing the keyboard when the EditText gets focus, and it does work, but entered text is not shown in the EditText...Ozell
try this: start an activity (which is used only for the editing part) using "startActivityForResult" , expecting a string as a result , and when you get it , update the editText .Deity
Did anybody manage to handle the overlapping of the title bar and/or action bar?Pulverize
What is my_floating_view?Hortatory
@MaksimDmitriev it's the view that you wish to become a floating one. in my sample, it will get detached from the layout that contains it and become above everything on the screen, even if you go to the launcher app (assuming the app won't get killed before). it can be any view you wish.Deity
Couldn't this be abused to steel credentials? What if you overlayed a textbox over another app's username and password fields?Purcell
you don't need to have an on top view in order to fake another app's login screen. it's enough to show up with the same UI, just like email phishing scams work ... of course, apps don't usually need to enter a password each time you use them.Deity
Now is the background activity paused? I would like it to function normally.Gusgusba
@Campiador I don't understand the question.Deity
OK my most important question is: I have no EditText in my overlay window, so how can I intercept keyboard events?Gusgusba
@Campiador I didn't try it, but if it causes problems for you, you can always open a new activity and show the exact content there, allowing the user to type the text, and faking the behavior you wanted. Of course, this might be a bit hard to do, but maybe it's worth to try.Deity
@androiddeveloper i am able to show the window in device. But the problem is my view has edit text and Button. How to get the control over button and get the value from edit text. And also i want to close the window when user presses on button. Please help me on thisBufordbug
@rup35h That's very basic. You call findViewById.(R.id.yourButtonId).setOnClickListener(...) , and inside you call dismiss() in case you used a dialog, or finish() in case you used an activity. To get the EditText's data, you call ((EditText)findViewById(R.id.yourEditTextId)).getText().toString().Deity
@androiddeveloper where should i call this. All windowManager code i have writtein inside service onCreate() method.Bufordbug
@rup35h It depends on what you did. If you started an activity, call it in the activity. If you successfully created a dialog, call it in the same place you've created/shown the dialog.Deity
@androiddeveloper thanks for replying. I am trying to do that i'll let you know in case any help will be needed. thanks a lotBufordbug
A
10

I'm one of the developers of the Tooleap SDK, and we also dealt with this issue. Basically, you don't need to use the SYSTEM_ALERT_WINDOW to display an activity on top of another one. You can just display a regular "shrinked" Activity with a transparent background.

To make a "shrinked Activity, change the activity window layout params of height and width:

WindowManager.LayoutParams params = getWindow().getAttributes(); 
params.x = ...;
params.y = ...;
params.width = ...;
params.height = ...;

this.getWindow().setAttributes(params);

To make a transparent background add to your activity definition in the manifest file:

android:theme="@android:style/Theme.Translucent"

That way, you can create the illusion of a floating activity:

Note that only the foreground activity will be resumed, while the background one is paused. But for most apps this shouldn't be an issue.

Now all that remains is when to launch the floating activity.

Here is an example of a "floating" calculator app using a regular activity. Note that the activity below the calculator belongs to another app.

Tooleap Calculator Screenshot

Agretha answered 8/5, 2014 at 10:4 Comment(3)
But this means that all you did is putting an activity on top of another app's activity, so if you click the back button, it will affect your activity and not the one behind. In order to make something that really floats and doesn't handle what should be handled behind, this method won't work...Deity
It all depends on the kind of user experience you are trying to achieve. If you want to display a small floating button that doesn't interfere with the activity underneath it, then yes, that won't be a good solution. But if you want to display a small screen that the user needs to attend to before continuing with his work (something like a dialog), then a small activity might be a good solution.Agretha
hmmmm... now that I've re-read the question, it isn't clear what exactly is the needed behavior. So you are correct.Deity

© 2022 - 2024 — McMap. All rights reserved.