Home » Android » android – Display view above status bar?

android – Display view above status bar?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I recently saw an image of an app that was capable of displaying a view above the status bar and was also able to cover it with a view.

I know you can get a view right below the status bar from a view with align parent top. But how would you get a view on top of the status bar??

Example

How to&Answers:

The answer by @Sadeshkumar is incorrect for ICS and above (perhaps GB as well).

A view created with TYPE_SYSTEM_ALERT and FLAG_LAYOUT_IN_SCREEN is covered by the StatusBar.

To get an overlay on top of the StatusBar, you need to use TYPE_SYSTEM_OVERLAY instead of TYPE_SYSTEM_ALERT.

The problem being then, how to get clicks/touches?

Answer:

Disable the System Status Bar – Without Root


After two full days of searching through SO posts and reading the Android docs over and over.. Here is the solution that I came up with. (tested)

    mView= new TextView(this);                                       
    mView.setText(".........................................................................");                       

    mLP = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            100,
            // Allows the view to be on top of the StatusBar
            WindowManager.LayoutParams.TYPE_SYSTEM_ERROR,
            // Keeps the button presses from going to the background window
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
            // Enables the notification to recieve touch events
            WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
            // Draws over status bar
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN,
            PixelFormat.TRANSLUCENT);

    mLP.gravity =  Gravity.TOP|Gravity.CENTER;      

    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mWindowManager.addView(mView, mLP);

Dont forget the permissions:

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

Note:
Tested upto kitkat.

Answer:

A view created with TYPE_SYSTEM_ERROR and FLAG_LAYOUT_IN_SCREEN is covered by the StatusBar.

Answer:

 int statusBarHeight = (int) Math.ceil(25 * getResources().getDisplayMetrics().density);


  View statusBarView = new View(MyActivity.this);
  statusBarView.setBackgroundColor(Color.GREEN);


  WindowManager.LayoutParams params = null;
  params = new WindowManager.LayoutParams(WindowManager.LayoutParams.FILL_PARENT,statusBarHeight,WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN, PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.RIGHT | Gravity.TOP;
    WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
    wm.addView(statusBarView, params);