Home » Android » java – Having application running above other app

java – Having application running above other app

Posted by: admin June 15, 2020 Leave a comment

Questions:

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…

How to&Answers:

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.

Answer:

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