Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make transparent activity window always on top in android?

I have created one transparent activity A. In this, I'm using surface view to play the videos. I'm able to touch the apps and activities behind this activity A. But when I click another app, the activity A goes to background and closed in an improper way. I want to make activity A always stay on top of the screen if u click another apps. I tried with many flags but no one is working correct. If want to know how to make activity window always stay on top ?

Please share your suggestions and ideas. Any help would be highly appreciated.

like image 232
user2155454 Avatar asked Apr 16 '13 09:04

user2155454


People also ask

How do I keep apps on top of Android?

Tap on Display. Tap on Always-on Panel. Tap the toggle at the top to enable the feature. Tap on “Always-on” at the bottom.

What is Android windowIsTranslucent?

android:windowIsTranslucent indicates weather the window in which the activity is present in translucent state or not.


1 Answers

You can use WindowManager.addView() to add your customized View to the Window, and your can set WindowManager.LayoutParams attributes for your View.

Here is a sample:

private WindowManager wm=null;
private WindowManager.LayoutParams wmParams=null;

private MyCustomView myView=null;

private void createView(){
    myView = new MyCustomView(getApplicationContext());
    wm = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
    wmParams = new WindowManager.LayoutParams();

    /**
     * Window type: phone.  These are non-application windows providing
     * user interaction with the phone (in particular incoming calls).
     * These windows are normally placed above all applications, but behind
     * the status bar.
     */
    wmParams.type=LayoutParams.TYPE_PHONE;
    wmParams.flags=LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE;
    wmParams.gravity=Gravity.LEFT|Gravity.TOP; 
    wmParams.x=0;
    wmParams.y=0;
    wmParams.width=40;
    wmParams.height=40;
    wm.addView(myView, wmParams);
}

Don't forget add permission in AndroidManifest.xml:

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>

Then, you can do anything you want in MyCustomView out of all applications.

like image 117
iStar Avatar answered Sep 28 '22 08:09

iStar