Display view above status bar?

19,075

Solution 1

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?

Solution 2

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.

Solution 3

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

Solution 4

 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);  
Share:
19,075
user1190019
Author by

user1190019

Updated on July 07, 2022

Comments

  • user1190019
    user1190019 almost 2 years

    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

  • om252345
    om252345 over 10 years
    and TYPE_SYSTEM_ERROR + FLAG_LAYOUT_IN_SCREEN will be touchable.
  • amalBit
    amalBit over 9 years
    Is there w way to get the touch events using system overlay?
  • user2486322
    user2486322 over 4 years
    WindowManager shoud be taken like this getApplicationContext().getSystemService(Context.WINDOW_SERV‌​ICE);
  • user2486322
    user2486322 over 4 years
    Need this permission tooo <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>