Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getStatusBarHeight method for Android #98

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Although in the global scope, it is not available until after the `deviceready`
- StatusBar.backgroundColorByHexString
- StatusBar.hide
- StatusBar.show
- StatusBar.getStatusBarHeight

Properties
--------
Expand Down Expand Up @@ -301,6 +302,23 @@ Supported Platforms
- Windows Phone 8
- Windows Phone 8.1

StatusBar.getStatusBarHeight
=================

Gets the current height (in CSS pixels) of system statusbar.

Note that default Android StatusBar height is setup as `24dp` which in practice means `24px` in WebView.

This method works well with Android Split Window mode so when app is at the bottom of split view, it returns `0` as the StatusBar height as there is no bar that overlaps app from the top.

StatusBar.getStatusBarHeight(function(height) {
// height in CSS pixels, i.e. 25
});

Supported Platforms
-------------------

- Android

StatusBar.isVisible
=================
Expand Down
29 changes: 29 additions & 0 deletions src/android/StatusBar.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.graphics.Rect;
import android.content.res.Resources;

import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaArgs;
Expand All @@ -38,6 +40,7 @@

public class StatusBar extends CordovaPlugin {
private static final String TAG = "StatusBar";
private static final int STATUS_BAR_HEIGHT_PX = 24;

/**
* Sets the context of the Command. This can then be used to do things like
Expand Down Expand Up @@ -203,6 +206,16 @@ public void run() {
return true;
}

if ("getStatusBarHeight".equals(action)) {
this.cordova.getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, getStatusBarHeight()));
}
});
return true;
}

return false;
}

Expand Down Expand Up @@ -273,4 +286,20 @@ private void setStatusBarStyle(final String style) {
}
}
}

private int getStatusBarHeight() {

// Get WebView top offset
Rect rectangle = new Rect();
Window window = cordova.getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int webViewTopOffsetPx = Math.round(rectangle.top / Resources.getSystem().getDisplayMetrics().density);

if (webViewTopOffsetPx > STATUS_BAR_HEIGHT_PX) {
// we're in vertical split mode at the bottom so no statusbar overlaying our app
return 0;
}
return STATUS_BAR_HEIGHT_PX;

}
}
6 changes: 6 additions & 0 deletions www/statusbar.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ var StatusBar = {
show: function () {
exec(null, null, "StatusBar", "show", []);
StatusBar.isVisible = true;
},

getStatusBarHeight: function (successCallback, errorCallback) {
exec(function (result) {
successCallback(result);
}, errorCallback, "StatusBar", "getStatusBarHeight", []);
}

};
Expand Down