An alert dialog is a modal view that includes an optional title, a message and buttons, positioned in the middle of the display.
![]() |
![]() |
![]() |
Android | iOS | Windows Phone |
---|
An alert dialog is created using Titanium.UI.createAlertDialog or <AlertDialog>
Alloy element.
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. 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
.
The 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.
Starting at Titanium SDK 5.1.0, you can also specify the placeholder
, keyboardType
and returnKeyType
properties when using the alert dialog style Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
When using the alert dialog style Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT, you can
specify the loginPlaceholder
, loginKeyboardType
and loginReturnKeyType
properties for the login field,
as well as the passwordPlaceholder
, passwordKeyboardType
and passwordReturnKeyType
properties for the password field.
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'
});
dialog.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.iOS.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.
In an Alloy application you can specify this property with either an <AndroidView/>
or
<View/>
element inside the <AlertDialog/>
element, for example:
<Alloy>
<AlertDialog onClick="doClick" title="Delete"
message="Would you like to delete the file?" cancel="1">
<!-- Add View or AndroidView for the androidView property -->
<View platform="android">
<Label color="red" text="Warning! This change is permanent and you cannot undo it!" />
</View>
<ButtonNames>
<ButtonName>Confirm</ButtonName>
<ButtonName>Cancel</ButtonName>
</ButtonNames>
</AlertDialog>
</Alloy>
The name of the API that this proxy corresponds to.
The name of the API that this proxy corresponds to.
The value of this property is the fully qualified name of the API. For example, Button
returns Ti.UI.Button
.
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, set to -1
to disable the cancel option.
Default: undefined (Android), -1 (iOS)
When this is set to true
, the dialog is canceled when touched outside the window's bounds.
Default: true on Android
Index to define the destructive button.
Requires: iOS 8.0 and later
Note that this property is only available on iOS 8 or above. Setting to -1 disables this option.
Default: -1
Base elevation of the view relative to its parent in pixels.
Requires: Android 5 and later
The elevation of a view determines the appearance of its shadow. Higher elevations produce larger and softer shadows.
Note: The elevation
property only works on Titanium.UI.View
objects.
Many Android components have a default elevation that cannot be modified.
For more information, see
Google design guidelines: Elevation and shadows.
Hint text of the text field inside the dialog.
Hint text of the text field inside the dialog.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
Keyboard appearance to be displayed when the text field inside the dialog is focused.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
This API can be assigned the following constants:
Default: Titanium.UI.KEYBOARD_APPEARANCE_DEFAULT
Keyboard type to display when this text field inside the dialog is focused.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
This API can be assigned the following constants:
Default: Titanium.UI.KEYBOARD_TYPE_DEFAULT
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
The Window or TabGroup whose Activity lifecycle should be triggered on the proxy.
If this property is set to a Window or TabGroup, then the corresponding Activity lifecycle event callbacks will also be called on the proxy. Proxies that require the activity lifecycle will need this property set to the appropriate containing Window or TabGroup.
Hint text of the login text field inside the dialog.
Hint text of the login text field inside the dialog.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Keyboard type to display when this text field inside the dialog is focused.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
This API can be assigned the following constants:
Default: Titanium.UI.KEYBOARD_DEFAULT
Placeholder of the login text field inside the dialog.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.loginHintText> instead.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Specifies the text to display on the keyboard Return
key when this field is focused.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
This API can be assigned the following constants:
Default: Titanium.UI.RETURNKEY_NEXT
Value of the login text field inside the dialog.
Value of the login text field inside the dialog.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Key identifying a string from the locale file to use for the loginHintText property.
Key identifying a string from the locale file to use for the loginHintText property.
Only one of loginHintText
or loginhinttextid
should be specified.
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.
Hint text of the password text field inside the dialog.
Hint text of the password text field inside the dialog.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Keyboard type to display when this text field inside the dialog is focused.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
This API can be assigned the following constants:
Default: Titanium.UI.KEYBOARD_DEFAULT
Placeholder of the password text field inside the dialog.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.passwordHintText> instead.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Specifies the text to display on the keyboard Return
key when this field is focused.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
This API can be assigned the following constants:
Default: Titanium.UI.RETURNKEY_DONE
Value of the password text field inside the dialog.
Value of the password text field inside the dialog.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Key identifying a string from the locale file to use for the passwordHintText property.
Key identifying a string from the locale file to use for the passwordHintText property.
Only one of passwordHintText
or hinttextid
should be specified.
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
Placeholder of the text field inside the dialog.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.hintText> instead.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
Index to define the preferred button.
Requires: iOS 9.0 and later
When you specify a preferred action, the alert dialog highlights the text of that action to give it emphasis. (If the alert also contains a cancel button, the preferred action receives the highlighting instead of the cancel button.) If the iOS device is connected to a physical keyboard, pressing the Return key triggers the preferred action.
Note that this property is only available on iOS 9 or above.
Default: -1
The preview context used in the 3D-Touch feature "Peek and Pop".
Requires: iOS 9.0 and later
Preview context to present the "Peek and Pop" of a view. Use an configured instance of Titanium.UI.iOS.PreviewContext here.
Note: This property can only be used on devices running iOS9 or later and supporting 3D-Touch. It is ignored on older devices and can manually be checked using Titanium.UI.iOS.forceTouchSupported.
Specifies the text to display on the keyboard Return
key when this field is focused.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
This API can be assigned the following constants:
Default: Titanium.UI.RETURNKEY_DEFAULT
Clockwise 2D rotation of the view in degrees.
Clockwise 2D rotation of the view in degrees.
Translation values are applied to the static post layout value.
Clockwise rotation of the view in degrees (x-axis).
Clockwise rotation of the view in degrees (x-axis).
Translation values are applied to the static post layout value.
Clockwise rotation of the view in degrees (y-axis).
Clockwise rotation of the view in degrees (y-axis).
Translation values are applied to the static post layout value.
Scaling of the view in x-axis in pixels.
Scaling of the view in x-axis in pixels.
Translation values are applied to the static post layout value.
Scaling of the view in y-axis in pixels.
Scaling of the view in y-axis in pixels.
Translation values are applied to the static post layout value.
The style for the alert dialog.
Style of the alert dialog, specified using one of the constants from
Titanium.UI.iOS.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 is only available on
iOS SDK 5 or above.
Default: Titanium.UI.iOS.AlertDialogStyle.DEFAULT
The tint-color of the dialog.
Requires: iOS 8.0 and later
This property is a direct correspondant of the tintColor
property of
UIView on iOS. For a dialog, it will tint the color of it's buttons.
Default:
Overrides: Titanium.UI.View.tintColor
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.
A material design visual construct that provides an instantaneous visual confirmation of touch point.
Requires: Android 5.0 and later
This is an opt-in feature available from Android Lollipop. Touch feedback is applied only if the backgroundColor is a solid color.
Default: false
Optional touch feedback ripple color. This has no effect unless touchFeedback
is true.
Requires: Android 5.0 and later
Default: Theme provided color.
A name to identify this view in activity transition.
Requires: Android 5 and later
Name should be unique in the View hierarchy.
Horizontal location of the view relative to its left position in pixels.
Horizontal location of the view relative to its left position in pixels.
Translation values are applied to the static post layout value.
Vertical location of the view relative to its top position in pixels.
Vertical location of the view relative to its top position in pixels.
Translation values are applied to the static post layout value.
Depth of the view relative to its elevation in pixels.
Requires: Android 5 and later
Translation values are applied to the static post layout value.
Value of the text field inside the dialog.
Value of the text field inside the dialog.
Note that this property is only available if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
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.
Finishes a batch update of the View's layout properties and schedules a layout pass of the view tree.
deprecated since 3.0.0
Use the <Titanium.Proxy.applyProperties> method to batch-update layout properties.
Since the layout pass scheduled is asynchronous, the rect
and size values may not be available immediately after
finishLayout
is called.
To be notified when the layout pass completes, add a listener for the postlayout event.
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.
Gets the value of the loginPlaceholder property.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.loginHintText> instead.
Gets the value of the passwordPlaceholder property.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.passwordHintText> instead.
Gets the value of the placeholder property.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.hintText> instead.
Gets the value of the tintColor property.
Overrides: Titanium.UI.View.getTintColor
Returns the matching view of a given view ID.
The ID of the view that should be returned. Use the id
property in your views to
enable it for indexing in this method.
Hides this dialog.
Animation options for Android. Since Release 5.1.0.
Overrides: Titanium.UI.View.hide
Inserts a view at the specified position in the children array.
Useful if the layout
property is set to horizontal
or vertical
.
Pass an object with the following key-value pairs:
view
(Titanium.UI.View): View to insertposition
(Number): Position in the children array to
insert the view. If omitted, inserts the view to the end of the array.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
.
Replaces a view at the specified position in the children array.
Useful if the layout
property is set to horizontal
or vertical
.
Pass an object with the following key-value pairs:
view
(Titanium.UI.View): View to insertposition
(Number): Position in the children array of
the view elment to replace.Sets the value of the bubbleParent property.
New value for the property.
Sets the value of the buttonNames property.
New value for the property.
Sets the value of the cancel property.
New value for the property.
Sets the value of the canceledOnTouchOutside property.
New value for the property.
Sets the value of the destructive property.
New value for the property.
Sets the value of the elevation property.
New value for the property.
Sets the value of the hiddenBehavior property.
New value for the property.
Sets the value of the hintText property.
New value for the property.
Sets the value of the hinttextid property.
New value for the property.
Sets the value of the keyboardAppearance property.
New value for the property.
Sets the value of the keyboardType property.
New value for the property.
Sets the value of the lifecycleContainer property.
New value for the property.
Sets the value of the loginHintText property.
New value for the property.
Sets the value of the loginKeyboardType property.
New value for the property.
Sets the value of the loginPlaceholder property.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.loginHintText> instead.
New value for the property.
Sets the value of the loginReturnKeyType property.
New value for the property.
Sets the value of the loginValue property.
New value for the property.
Sets the value of the loginhinttextid property.
New value for the property.
Sets the value of the message property.
New value for the property.
Sets the value of the messageid property.
New value for the property.
Sets the value of the okid property.
New value for the property.
Sets the value of the passwordHintText property.
New value for the property.
Sets the value of the passwordKeyboardType property.
New value for the property.
Sets the value of the passwordPlaceholder property.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.passwordHintText> instead.
New value for the property.
Sets the value of the passwordReturnKeyType property.
New value for the property.
Sets the value of the passwordValue property.
New value for the property.
Sets the value of the passwordhinttextid property.
New value for the property.
Sets the value of the persistent property.
New value for the property.
Sets the value of the placeholder property.
deprecated since 5.4.0
Use <Titanium.UI.AlertDialog.hintText> instead.
New value for the property.
Sets the value of the preferred property.
New value for the property.
Sets the value of the previewContext property.
New value for the property.
Sets the value of the returnKeyType property.
New value for the property.
Sets the value of the rotation property.
New value for the property.
Sets the value of the rotationX property.
New value for the property.
Sets the value of the rotationY property.
New value for the property.
Sets the value of the scaleX property.
New value for the property.
Sets the value of the scaleY property.
New value for the property.
Sets the value of the style property.
New value for the property.
Sets the value of the tintColor property.
New value for the property.
Overrides: Titanium.UI.View.setTintColor
Sets the value of the title property.
New value for the property.
Sets the value of the titleid property.
New value for the property.
Sets the value of the touchFeedback property.
New value for the property.
Sets the value of the touchFeedbackColor property.
New value for the property.
Sets the value of the transitionName property.
New value for the property.
Sets the value of the translationX property.
New value for the property.
Sets the value of the translationY property.
New value for the property.
Sets the value of the translationZ property.
New value for the property.
Sets the value of the value property.
New value for the property.
Shows this dialog.
Animation options for Android. Since Release 5.1.0.
Overrides: Titanium.UI.View.show
Starts a batch update of this view's layout properties.
deprecated since 3.0.0
Use the <Titanium.Proxy.applyProperties> method to batch-update layout properties.
To prevent a layout pass each time a property is modified, call startLayout
before
changing any properties that may change this view's layout. This initiates a batch update
mode where layout changes are deferred.
Call finishLayout to end batch update mode and trigger a layout pass. For example:
view.startLayout();
view.top = 50;
view.left = 50;
view.finishLayout();
Note that any property changes made during the batch update may be deferred until
finishLayout
is called. This may vary somewhat by platform. For example, changing the
text of a label may trigger a layout pass. In iOS, updating the label text is
deferred.
See also: updateLayout, finishLayout, postlayout event.
Performs a batch update of all supplied layout properties and schedules a layout pass after they have been updated.
deprecated since 3.0.0
Use the <Titanium.Proxy.applyProperties> method to batch-update layout properties.
This is another way to perform a batch update. The updateLayout
method is called with a
dictionary of layout properties to perform the batch update. For example:
view.updateLayout({top:50, left:50});
This is equivalent to the following:
view.startLayout();
view.top = 50;
view.left = 50;
view.finishLayout();
See also: startLayout, finishLayout, postlayout event.
Layout properties to be updated.
Fired when a button in the dialog is clicked.
There is a subtle difference between singletap and click events.
A singletap event is generated when the user taps the screen briefly without moving their finger. This gesture will also generate a click event.
However, a click event can also be generated when the user touches, moves their finger, and then removes it from the screen.
On Android, a click event can also be generated by a trackball click.
Boolean type on Android; Number on iOS.
On Android, 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. Also note that the cancel button may not be
used on the iPad, because iOS will internally decide whether or not to show it in
the current context (e.g. in a popover).
See the Three-button Alert Dialog
example for a cross-platform workaround for this
parity issue.
Index of the button that was clicked.
Value of login field if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Value of password field if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.LOGIN_AND_PASSWORD_INPUT.
Value of text field if dialog style
property is defined as
Titanium.UI.iOS.AlertDialogStyle.PLAIN_TEXT_INPUT or
Titanium.UI.iOS.AlertDialogStyle.SECURE_TEXT_INPUT.
Source object that fired the event.
Name of the event fired.
True if the event will try to bubble up if possible.
Set to true to stop the event from bubbling.
Overrides: Titanium.UI.View.click