An alert dialog is a modal view that includes an optional title, a message and buttons, positioned in the middle of the display.
An alert dialog is created using Titanium.UI.createAlertDialog.
Although this dialog always appears in the middle of the display (not touching the edges), other aspects of its aesthetics and the way the user interacts with it are different for each platform, as described below.
On Android, the default alert dialog displays text information, via a title and message, without
any buttons. As the user can use the system hardware back button to dismiss it, a button is
optional.
Buttons are shown if the buttonNames property is defined, and are rendered horizontally below
the message.
To create a custom layout, a view may be added and, in turn, a hierarchy of views added to that child view.
On iOS, the default alert dialog displays text information, via a title and message, with a single button to allow it to be dismissed.
Buttons are defined using the buttonNames property and are rendered vertically below
the message.
On iOS 4.0 and later, alert dialogs are automatically cancelled when the application is
paused/suspended. This behavior can avoided by setting persistent property on alert dialog
to be true.
On iOS 5.0 and later, style property can be used to allow the user to enter plain text,
obscured text or login identifier and password. Entered values can be captured with listening
cancel event.
A global method alert() is aliased to this object, and can be invoked with a single message.
For example
alert('this is a message');
This will generate an alert with a title of "Alert" and an "OK" button.
Multiple alerts should not be shown at once.
The title and ok properties cannot be changed while the alert dialog is being displayed. On
Android only, you can change the message property while the alert dialog is being displayed.
Create a single-button alert dialog using the global alert() alias.
Ti.UI.setBackgroundColor('white');
var win = Ti.UI.createWindow({
title: 'Click window to test',
backgroundColor: 'white',
exitOnClose: true,
fullscreen: false
});
win.addEventListener('click', function(e){
alert('The file has been deleted');
});
win.open();
Create a single-button alert dialog, without explicitly defining it using the buttonNames
property, which is invoked when the app window is clicked.
Ti.UI.setBackgroundColor('white');
var win = Ti.UI.createWindow({
title: 'Click window to test',
backgroundColor: 'white',
exitOnClose: true,
fullscreen: false
});
win.addEventListener('click', function(e){
var dialog = Ti.UI.createAlertDialog({
message: 'The file has been deleted',
ok: 'Okay',
title: 'File Deleted'
}).show();
});
win.open();
Create a three-button alert dialog, which is invoked when the app window is clicked. Output a message to the log when the cancel button is clicked.
Ti.UI.setBackgroundColor('white');
var win = Ti.UI.createWindow({
title: 'Click window to test',
backgroundColor: 'white',
exitOnClose: true,
fullscreen: false
});
win.addEventListener('click', function(e){
var dialog = Ti.UI.createAlertDialog({
cancel: 1,
buttonNames: ['Confirm', 'Cancel', 'Help'],
message: 'Would you like to delete the file?',
title: 'Delete'
});
dialog.addEventListener('click', function(e){
if (e.index === e.source.cancel){
Ti.API.info('The cancel button was clicked');
}
Ti.API.info('e.cancel: ' + e.cancel);
Ti.API.info('e.source.cancel: ' + e.source.cancel);
Ti.API.info('e.index: ' + e.index);
});
dialog.show();
});
win.open();
Create an alert dialog and allow the user enter plain text, which is invoked when the app window is clicked. Output entered text value to the log when the OK button is clicked.
Ti.UI.setBackgroundColor('white');
var win = Ti.UI.createWindow({
title: 'Click window to test'
});
win.addEventListener('click', function(e){
var dialog = Ti.UI.createAlertDialog({
title: 'Enter text',
style: Ti.UI.iPhone.AlertDialogStyle.PLAIN_TEXT_INPUT,
buttonNames: ['OK']
});
dialog.addEventListener('click', function(e){
Ti.API.info('e.text: ' + e.text);
});
dialog.show();
});
win.open();
Previous three-button alert dialog example as an Alloy view.
alertdialog.xml:
<Alloy>
<Window id="win" onClick="showDialog" title="Click window to test" backgroundColor="white"
exitOnClose="true" fullscreen="false" >
<AlertDialog id="dialog" onClick="doClick" title="Delete"
message="Would you like to delete the file?" cancel="1">
<!-- The ButtonNames tag sets the buttonNames property. -->
<ButtonNames>
<ButtonName>Confirm</ButtonName>
<ButtonName>Cancel</ButtonName>
<ButtonName>Help</ButtonName>
</ButtonNames>
</AlertDialog>
</Window>
</Alloy>
alertdialog.js:
function showDialog(){
$.dialog.show();
};
function doClick(e){
Ti.API.info('e.text: ' + e.text);
};
View to load inside the message area, to create a custom layout.
View to load inside the message area, to create a custom layout.
Indicates if the proxy will bubble an event to its parent.
Some proxies (most commonly views) have a relationship to other proxies, often established by the add() method. For example, for a button added to a window, a click event on the button would bubble up to the window. Other common parents are table sections to their rows, table views to their sections, and scrollable views to their views. Set this property to false to disable the bubbling to the proxy's parent.
Default: true
Index to define the cancel button.
On iOS and Mobile Web, set to -1 to disable the cancel option.
Default: undefined (Android), -1 (iOS, Mobile Web)
Key identifying a string in the locale file to use for the message text.
Key identifying a string in the locale file to use for the message text.
Text for the OK button.
Text for the OK button.
This property is useful when only one button is required, as it negates the need to define
the buttonNames property. If buttonNames is defined, this property is ignored.
Key identifying a string in the locale file to use for the ok text.
Key identifying a string in the locale file to use for the ok text.
If buttonNames is defined, this property is ignored.
Boolean value indicating if the alert dialog should only be cancelled by user gesture or by hide method.
This property is useful to ensure that the alert dialog will not be ignored by the user when the application is paused/suspended.
Default: false on iOS, true on Android
The style for the alert dialog.
Style of the alert dialog, specified using one of the constants from
Titanium.UI.iPhone.AlertDialogStyle. Using styles other than default one can break
your dialog layout if more than two buttons used. All styles can handle up to two
buttons comfortably, except for default style can handle up to six buttons when title
and message is empty or not given. Note that this property available on
iOS SDK 5 or above.
Default: Titanium.UI.iPhone.AlertDialogStyle.DEFAULT
Title of the dialog.
Title of the dialog.
If not set, a dialog with no title bar will be created.
Key identifying a string in the locale file to use for the title text.
Key identifying a string in the locale file to use for the title text.
Adds the specified callback as an event listener for the named event.
Name of the event.
Callback function to invoke when the event is fired.
Applies the properties to the proxy.
Properties are supplied as a dictionary. Each key-value pair in the object is applied to the proxy such that myproxy[key] = value.
A dictionary of properties to apply.
Fires a synthesized event to any registered listeners.
Name of the event.
A dictionary of keys and values to add to the Titanium.Event object sent to the listeners.
Removes the specified callback as an event listener for the named event.
Multiple listeners can be registered for the same event, so the
callback parameter is used to determine which listener to remove.
When adding a listener, you must save a reference to the callback function in order to remove the listener later:
var listener = function() { Ti.API.info("Event listener called."); }
window.addEventListener('click', listener);
To remove the listener, pass in a reference to the callback function:
window.removeEventListener('click', listener);
Name of the event.
Callback function to remove. Must be the same function passed to addEventListener.
Sets the value of the androidView property.
New value for the property.
Sets the value of the bubbleParent property.
New value for the property.
Sets the value of the cancel property.
New value for the property.
Sets the value of the message property.
New value for the property.
Sets the value of the persistent property.
New value for the property.
Sets the value of the style property.
New value for the property.
Fired when a button in the dialog is clicked.
True if the event will try to bubble up if possible.
Boolean type on Android and Mobile Web; Number on iOS.
On Android and Mobile Web, indicates whether the cancel button was clicked, in which
case returns true.
On iOS, the value of the cancel property is
returned, if defined, or -1 otherwise.
See the Three-button Alert Dialog example for a cross-platform workaround for this
parity issue.
Set to true to stop the event from bubbling.
Index of the button that was clicked.
Value of login field if dialog style property is defined as
Titanium.UI.iPhone.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Value of password field if dialog style property is defined as
Titanium.UI.iPhone.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Source object that fired the event.
Value of text field if dialog style property is defined as
Titanium.UI.iPhone.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iPhone.AlertDialogStyle.SECURE_TEXT_INPUT.
Name of the event fired.