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 4 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
14 changes: 14 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,19 @@ Supported Platforms
- Windows Phone 8
- Windows Phone 8.1

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

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

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

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

- Android

StatusBar.isVisible
=================
Expand Down
21 changes: 21 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 Down Expand Up @@ -203,6 +205,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 +285,13 @@ private void setStatusBarStyle(final String style) {
}
}
}

private int getStatusBarHeight() {
Rect rectangle = new Rect();
Window window = cordova.getActivity().getWindow();
window.getDecorView().getWindowVisibleDisplayFrame(rectangle);
int statusBarHeight = Math.round(rectangle.top / Resources.getSystem().getDisplayMetrics().density);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the app is on the bottom of a split-screen, will this return the offset from the top of the screen? If so, this value will be incorrect.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. I see the issue and im trying to figure out an fix for this.

return statusBarHeight;

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

getStatusBarHeight: function (successCallback, errorCallback) {
exec(function (result) {
successCallback(result);
}, errorCallback, "StatusBar", "getStatusBarHeight", []);
StatusBar.isVisible = true;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it's left over from a copy-paste of show

}

};
Expand Down