Alex
Alex

Reputation: 2233

How to make an activity window stay always on top

I want to create an activity that stays always on top (like the modal windows or task manager in Windows) of the others activities. How can I do this on Android? Thanks

Upvotes: 9

Views: 34654

Answers (4)

dilanMD
dilanMD

Reputation: 95

Follow the steps to achieve your requirement

  1. Create an activity which is going to be the top activity
  2. Add the following code in the manifest

    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    
  3. Use the following code to get overlay permission from user

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (!Settings.canDrawOverlays(this)) {
            new AlertDialog.Builder(this)
                .setTitle("Permission Request")
                .setMessage("This app needs your permission to overlay the system apps")
                .setPositiveButton("Open settings", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        Intent myIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
                        startActivityForResult(myIntent, 101);
                    }
                })
                .setNegativeButton(android.R.string.no, null)
                .show();
        }
    }
    

Upvotes: 0

bdevay
bdevay

Reputation: 161

You can use the following code in the overridden onStop method of your activity:

@Override
protected void onStop(){
    super.onStop();
    Intent intent = new Intent(this, ClassNameOfYourActivity.class);
    startActivity(intent);
}

Beauty problem: your activity will pop-up again if any other activity trying to claim the focus. So it's not a modal window.

And it's dangerous! You wont be able to handle the Android GUI, you'll be able to control only your application GUI. For instance, switching debug mode on-off, killing application (only over ADB), reaching system settings, etc. will be impossible. If you switch off the ADB and combine it with the auto start mechanism then you'll be in trap.

So you won't be popular if you share it with Play :)

Upvotes: 12

Richard Le Mesurier
Richard Le Mesurier

Reputation: 29762

Depending on what exactly you are trying to do, you might be happy with windows that stay on top of other Activities.

Apps like Super Video client & Floating Notes are able to display floating windows above other activities. These questions might point you in the right direction:

Upvotes: 5

Sebastian Roth
Sebastian Roth

Reputation: 11537

You can't. As this is defined in the Android system.

Upvotes: 5

Related Questions