diff --git a/README.md b/README.md index aaca95a8..35ea9e20 100644 --- a/README.md +++ b/README.md @@ -45,11 +45,63 @@ Report issues on the [Apache Cordova issue tracker](https://issues.apache.org/ji ## Methods +- `navigator.notification.setAndroidStyleName` - `navigator.notification.alert` - `navigator.notification.confirm` - `navigator.notification.prompt` - `navigator.notification.beep` +## navigator.notification.setAndroidStyleName + +For Android platforms, allows specifying a custom style name to use with the dialogs. +The style name can be defined within a `styles.xml` file. The `stles.xml` file +is referenced in your config.xml file as a ``. + + navigator.notification.setAndroidStyleName(styleName) + +- __styleName__: Name of Android style. _(String)_ + + +### Example + +1. Create a `style.xml` with a custom dialog style (notice we give the style a name of `AlertDialogCustom` which will be used in our code later)(this file can go anywhere in your project, this example places it in the root): + + + + #0000ff + #ffffff + #000000 + + +2. Reference your `styles.xml` file in your `config.xml` file so it gets copied to the correct location (since we placed `styles.xml` in our root, we just put the filename with no path, otherwise `src` is relative to your `config.xml` directory) + + + + + + + + +3. In your code, set the dialog style name to the one you used in `styles.xml`: + + + navigator.notification.setAndroidStyleName( + 'AlertDialogCustom' // styleName + ); + +### Supported Platforms + +- Android + ## navigator.notification.alert Shows a custom alert or dialog box. Most Cordova implementations use a native diff --git a/src/android/Notification.java b/src/android/Notification.java index f19bc888..8a28247e 100644 --- a/src/android/Notification.java +++ b/src/android/Notification.java @@ -51,7 +51,9 @@ Licensed to the Apache Software Foundation (ASF) under one public class Notification extends CordovaPlugin { private static final String LOG_TAG = "Notification"; - + + private String androidStyleName = ""; + public int confirmResult = -1; public ProgressDialog spinnerDialog = null; public ProgressDialog progressDialog = null; @@ -78,8 +80,11 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo * be returned in the event of an invalid action. */ if(this.cordova.getActivity().isFinishing()) return true; - - if (action.equals("beep")) { + + if (action.equals("setAndroidStyleName")) { + this.setAndroidStyleName(args.getString(0)); + } + else if (action.equals("beep")) { this.beep(args.getLong(0)); } else if (action.equals("alert")) { @@ -122,6 +127,15 @@ else if (action.equals("progressStop")) { // LOCAL METHODS //-------------------------------------------------------------------------- + /** + * Beep plays the default notification ringtone. + * + * @param styleName The name of the android xml defined style to override the alert dialog theme (default: THEME_DEVICE_DEFAULT_LIGHT) + */ + public void setAndroidStyleName(final String styleName) { + androidStyleName = styleName; + } + /** * Beep plays the default notification ringtone. * @@ -484,7 +498,9 @@ public synchronized void progressStop() { private AlertDialog.Builder createDialog(CordovaInterface cordova) { int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Build.VERSION_CODES.HONEYCOMB) { - return new AlertDialog.Builder(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); + int id = androidStyleName == null || androidStyleName.isEmpty() ? AlertDialog.THEME_DEVICE_DEFAULT_LIGHT : cordova.getActivity().getResources().getIdentifier(androidStyleName, "style", cordova.getActivity().getPackageName()); + + return new AlertDialog.Builder(cordova.getActivity(), id); } else { return new AlertDialog.Builder(cordova.getActivity()); } @@ -494,7 +510,9 @@ private AlertDialog.Builder createDialog(CordovaInterface cordova) { private ProgressDialog createProgressDialog(CordovaInterface cordova) { int currentapiVersion = android.os.Build.VERSION.SDK_INT; if (currentapiVersion >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - return new ProgressDialog(cordova.getActivity(), AlertDialog.THEME_DEVICE_DEFAULT_LIGHT); + int id = androidStyleName == null || androidStyleName.isEmpty() ? AlertDialog.THEME_DEVICE_DEFAULT_LIGHT : cordova.getActivity().getResources().getIdentifier(androidStyleName, "style", cordova.getActivity().getPackageName()); + + return new ProgressDialog(cordova.getActivity(), id); } else { return new ProgressDialog(cordova.getActivity()); } diff --git a/www/notification.js b/www/notification.js index 4a428d7d..f51941a6 100644 --- a/www/notification.js +++ b/www/notification.js @@ -28,6 +28,20 @@ var platform = require('cordova/platform'); module.exports = { + /** + * Open a native alert dialog, with a customizable title and button text. + * + * @param {String} styleName The name of the android xml defined style to override the alert dialog theme (default: THEME_DEVICE_DEFAULT_LIGHT) + */ + setAndroidStyleName: function (styleName) { + if (platform.id !== 'android') { + return; + } + + const _styleName = (styleName || ''); + exec(null, null, 'Notification', 'setAndroidStyleName', [_styleName]); + }, + /** * Open a native alert dialog, with a customizable title and button text. *