Allow setting the statusbar backgroundcolor on Android

This commit is contained in:
EddyVerbruggen 2015-02-27 10:29:39 +01:00
parent dff669ece8
commit 43c8c15bf4
2 changed files with 29 additions and 0 deletions

View File

@ -41,6 +41,10 @@ Preferences
<preference name="StatusBarBackgroundColor" value="#000000" />
- __AndroidStatusBarBackgroundColor__ (color hex string, defaults to the Android theme default). On Android 5 and up, the background color can be set by a hex string (#RRGGBB) at startup. We don't use the same property as for iOS because on iOS you typically want the statusbar to have the same color as the app background, but the Android 5+ guidelines specify using a different color than you apps main color, so the value of this property is typically different than the one specified by StatusBarBackgroundColor.
<preference name="AndroidStatusBarBackgroundColor" value="#000000" />
- __StatusBarStyle__ (status bar style, defaults to lightcontent). On iOS 7, set the status bar style. Available options default, lightcontent, blacktranslucent, blackopaque.
<preference name="StatusBarStyle" value="lightcontent" />

View File

@ -20,6 +20,8 @@
package org.apache.cordova.statusbar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;
@ -54,6 +56,7 @@ public class StatusBar extends CordovaPlugin {
// by the Cordova.
Window window = cordova.getActivity().getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setStatusBarBackgroundColor();
}
});
}
@ -98,4 +101,26 @@ public class StatusBar extends CordovaPlugin {
return false;
}
/**
* Read 'AndroidStatusBarBackgroundColor' from config.xml. We expect a hex #RRGGBB string.
*/
private void setStatusBarBackgroundColor() {
if (Build.VERSION.SDK_INT >= 21) {
final String colorPref = preferences.getString("AndroidStatusBarBackgroundColor", null);
if (colorPref != null) {
final Window window = cordova.getActivity().getWindow();
// Method and constants not available on all SDKs but we want to be able to compile this code with any SDK
window.clearFlags(0x04000000); // SDK 19: WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(0x80000000); // SDK 21: WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
try {
// Using reflection makes sure any 5.0+ device will work without having to compile with SDK level 21
window.getClass().getDeclaredMethod("setStatusBarColor", int.class).invoke(window, Color.parseColor(colorPref));
} catch (Exception ignore) {
// this should not happen, only in case Android removes this method in a version > 21
Log.w(TAG, "Method window.setStatusBarColor not found for SDK level " + Build.VERSION.SDK_INT);
}
}
}
}
}