8

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...

Community
  • 1
  • 1
RE6
  • 2,524
  • 3
  • 28
  • 53

2 Answers2

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.
android developer
  • 106,412
  • 122
  • 641
  • 1,128
  • And, will the view be functional even in ICS? – RE6 Apr 22 '12 at 11:04
  • 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. – android developer Apr 22 '12 at 11:27
  • I have only one problem. I want to create an floating EditText, but when you click it, it doesn't show the keyboard... – RE6 Apr 22 '12 at 12:17
  • 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. – android developer Apr 22 '12 at 12:24
  • 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... – RE6 Apr 22 '12 at 16:02
  • 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 . – android developer Apr 22 '12 at 22:01
  • Did anybody manage to handle the overlapping of the title bar and/or action bar? – codeskraps Jun 21 '12 at 12:21
  • What is `my_floating_view`? – Maksim Dmitriev Sep 27 '13 at 12:16
  • @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. – android developer Sep 27 '13 at 13:46
  • Couldn't this be abused to steel credentials? What if you overlayed a textbox over another app's username and password fields? – Didier A. Jan 23 '14 at 22:49
  • 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. – android developer Jan 23 '14 at 23:36
  • Now is the background activity paused? I would like it to function normally. – Behnam Dec 08 '14 at 11:12
  • @Campiador I don't understand the question. – android developer Dec 08 '14 at 17:08
  • OK my most important question is: I have no EditText in my overlay window, so how can I intercept keyboard events? – Behnam Dec 10 '14 at 07:34
  • @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. – android developer Dec 10 '14 at 08:51
  • @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 this – rupesh Sep 03 '15 at 12:17
  • @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(). – android developer Sep 03 '15 at 12:22
  • @androiddeveloper where should i call this. All windowManager code i have writtein inside service onCreate() method. – rupesh Sep 03 '15 at 12:31
  • @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. – android developer Sep 03 '15 at 12:38
  • @androiddeveloper thanks for replying. I am trying to do that i'll let you know in case any help will be needed. thanks a lot – rupesh Sep 03 '15 at 12:41
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

Danny
  • 586
  • 5
  • 14
  • 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... – android developer Dec 10 '14 at 08:54
  • 1
    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. – Danny Dec 10 '14 at 10:16
  • 1
    hmmmm... now that I've re-read the question, it isn't clear what exactly is the needed behavior. So you are correct. – android developer Dec 10 '14 at 12:06