Reputation: 607
I try hard to develop plugin which can listen to orientation change to proceed necessary changes without essential added javascript code or necessity to edit activity java code.
There are option to use orientationChangeListener, but I need only listen to switching between portrait and landscape modes, not heading of device. This is usable, but too much monstrous for this little task.
Are there any other options to resolve it? Is it possible to modify onConfigurationChanged method of main activity or create some new onCofigurationChange listener? Or in a fact CordovaPlugin doesn't know what is happening around him?
Upvotes: 0
Views: 1744
Reputation: 607
I have found solution to this (thanks to Andrew Grieve at Cordova mailing list), so I post it here, if somebody will have similar problem. Key is onLayoutChangeListener of view (WebView or other view, if plugin add any).
Put below code inside some method of plugin:
webView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right,
int bottom, int oldLeft, int oldTop, int oldRight,
int oldBottom) {
// some code
}
});
However, there is one big BUT. This listener is available in API11+, so do not function on Android 2.2 or 2.3...
Upvotes: 1
Reputation: 313
Your Cordova plugin requires writing of both Javascript and Java code. Being as your'e going to be writing Javascript anyway wouldn't this suffice?
window.addEventListener("orientationchange", function() {
if (Math.abs(window.orientation) === 90) {
// landscape view
} else {
// portrait view
}
}, false);
Upvotes: 2