# Titanium.UI.Button
A button widget that has four states: normal, disabled, focused and selected.
# Overview
Android | iOS | Windows |
---|---|---|
A button displays its selected state while it is being pressed. The focused state is only used on Android devices that have navigation keys or a keyboard, to indicate which button has input focus.
You can specify background images for each state, as well as button text and a button icon. On iOS, there are button styles which define appearance for each state, even if no button images are set.
Use the Titanium.UI.createButton method or a <Button>
Alloy element to create a button
(see Examples) .
# Android Platform Notes
Android uses default button images for the normal, focused, selected and disabled states. If you use a custom button image, you should specify versions for the focused and selected states (and the disabled state, if the button is ever disabled).
Android also supports setting the background color for each of the states. These are not supported on iOS.
The Material style introduced in Titanium SDK 10.0.0 will change the default look of the buttons. You can change button styles with a custom theme as follows:
<!-- Assign this custom theme to "tiapp.xml" file's android manifest application element. -->
<style name="Theme.MyTheme" parent="Theme.Titanium.Light">
<item name="textAppearanceButton">@style/TextAppearance.MyButton</item>
<item name="android:buttonStyle">@style/Widget.MyButton</item>
<item name="buttonStyle">@style/Widget.MyButton</item>
</style>
<!-- Affects the appearance of text in all material buttons. -->
<style name="TextAppearance.MyButton" parent="TextAppearance.MaterialComponents.Button">
<!-- Do not display button text in all-caps. -->
<item name="android:textAllCaps">false</item>
<!-- Remove extra spacing between characters. -->
<item name="android:letterSpacing">0</item>
</style>
<!-- Affects the appearance of all non-material system buttons. -->
<style name="Widget.MyButton" parent="Widget.AppCompat.Button">
<!-- Do not display button text in all-caps. -->
<item name="android:textAllCaps">false</item>
<!-- Remove extra spacing between characters. -->
<item name="android:letterSpacing">0</item>
</style>
# iOS Platform Notes
iOS buttons have two special properties, style
and systemButton
.
The style
property specifies the type of button decoration, and can be set to one
of the values described in Titanium.UI.iOS.SystemButtonStyle.
To use a custom button style, such as a background gradient, you may need to explicitly set
the button's style
property to PLAIN
, to prevent the button style from overriding any background color or gradient.
Also note:
- When assigning a custom image to the
backgroundImage
property, the image must be partially or wholly transparent for the background color or background gradient to be visible. - Similarly, for an assigned background gradient to show through, the
backgroundColor
may need to be set to 'transparent'.
Unlike Android, iOS doesn't supply any default background images for buttons-- system button styles serve this purpose.
The systemButton
property lets you create a predefined system-defined button,
such as the Camera or Add buttons, for use in toolbars and nav bars.
Specifically, system buttons may be used in the following locations:
- Titanium.UI.iOS.Toolbar. The
items
array can contain system buttons. - Titanium.UI.iPad.Popover. The
leftNavButton
andrightNavButton
properties can refer to system buttons - Titanium.UI.iOS.SplitWindow. The
toolbar
array can contain system buttons. - Titanium.UI.TextField. The
keyboardToolbar
array can contain system buttons. - Titanium.UI.TextArea. The
keyboardToolbar
array can contain system buttons. - Titanium.UI.Window. The
leftNavButton
andrightNavButton
properties can refer to system buttons and thetoolbar
array can contain system buttons.
Use the constants in Titanium.UI.iOS.SystemButton
to specify a button type. The size, style and decoration of system buttons is set
automatically, so you can create a button by setting only the systemButton
property.
For example:
var cancelButton = Ti.UI.createButton({ systemButton: Ti.UI.iOS.SystemButton.CANCEL });
In Alloy, you can omit the namespace when setting the systemButton
property:
<Button id="cancelbtn" systemButton="CANCEL"></Button>
Note that the systemButton
property specifies appearance, not behavior. For
example, to bring up the camera when the user presses the Camera button, you must
add an event listener to the button and call Titanium.Media.showCamera when the
button is clicked.
# iOS Pressed Button Effects
If you do not supply separate background images for the normal and selected states, iOS supplies a built-in effect for pressed buttons. For most iOS buttons, the effect dims the entire button (except for the text) while the button is being pressed.
For toolbar buttons that use the Titanium.UI.iOS.SystemButtonStyle.PLAIN style or use system icons such as Titanium.UI.iOS.SystemButton.CAMERA, a glow effect is used. The glow effect shows as a white circular glow or highlight at the center of the button when the button is pressed.
# Examples
# Simple Button Example
var button = Titanium.UI.createButton({
title: 'Hello',
top: 10,
width: 100,
height: 50
});
button.addEventListener('click',function(e)
{
Titanium.API.info("You clicked the button");
});
# Alloy XML Markup
Previous example as an Alloy view.
button.xml:
<Alloy>
<Window id="win" backgroundColor="white">
<!-- The title property can also be defined as node text. -->
<Button id="button" onClick="doClick" title="Hello"
top="10" width="100" height="50" />
</Window>
</Alloy>
button.js:
function doClick(e){
Titanium.API.info("You clicked the button");
};
# Properties
# attributedString
Specify an attributed string for the label.
The underlying attributed string drawn by the label. If set, avoid setting common attributes
in the label, such as color
and font
, as unexpected behaviors may result.
iOS Note: This property can also be used to supress the underline style when accessibility
is enabled. To do so, set the type
to ATTRIBUTE_UNDERLINES_STYLE and the
value
to ATTRIBUTE_UNDERLINE_STYLE_NONE.
# backgroundDisabledImage
Background image for the button in its disabled state, specified as a local file path or URL.
Also sets the background image for the other button states (normal, focused, selected), unless they've been specified explicitly, except for iOS, which requires a backgroundImage.
# backgroundFocusedImage
Background image for the button in its focused state, specified as a local file path or URL.
Focusable must be true for normal views. Sets the background image for the focused state only. For iOS, since there is not a trackball, this does nothing.
# backgroundImage
Background image for the button in its normal state, specified as a local file path or URL.
Also sets the background image for the other button states (disabled, focused, selected),
unless they've been specified explicitly. On iOS, if backgroundDisabledImage is unset, the
image will appear faded to indicate being disabled. Also on iOS, if backgroundSelectedImage
is unset, the image will be darkened to indicate being selected.
# backgroundSelectedColor
Selected background color of the view, as a color name or hex triplet.
For information about color values, see the "Colors" section of Titanium.UI.
Default: Background color of this view.
# backgroundSelectedImage
Background image for the button in its selected state, specified as a local file path or URL.
On Android, Focusable must be true in order to be selected. Sets the background image for the selected state only.
# color
Default button text color, as a color name or hex triplet.
For information about color values, see the "Colors" section of Titanium.UI.
# disabledColor
Text color of the button in its disabled state, as a color name or hex triplet.
# enabled
Set to true
to enable the button, false
to disable the button.
Default: true
# image
Image to display next to the button title.
As of Titanium 10.2.0, the imageIsMask property determines how
this image is displayed. If set true
, this image will be tinted using the color assigned to the
tintColor property. If set false
, the image is displayed as-is.
On Android, you can set this to a numeric resource drawable ID via Ti.App.Android.R.drawable.*
which lets you use native vector drawbles that are commonly used as icons.
# imageIsMask
Set true to tint the button image. Set false to show the image as-is.
Default: true
# selectedColor
Button text color used to indicate the selected state, as a color name or hex triplet.
For information about color values, see the "Colors" section of Titanium.UI.
# shadowColor
Shadow color of the title, as a color name or hex triplet.
Use in conjunction with shadowOffset and shadowRadius. For information about color values, see the "Colors" section of Titanium.UI.
# shadowOffset
Shadow offset of the title, as a dictionary with the properties x
and y
.
Use in conjunction with shadowColor and shadowRadius.
# shadowRadius
Shadow radius of the title.
Use in conjunction with shadowColor and shadowOffset.
# style
The border and fill style the button will use.
On Android, this is a creation-only property and cannot be changed dynamically.
For Titanium versions older than 10.0.0, this is an iOS only property and must be assigned a constant from Titanium.UI.iOS.SystemButtonStyle which is now deprecated.
Default: Titanium.UI.BUTTON_STYLE_TEXT> (on iOS),
<Titanium.UI.BUTTON_STYLE_FILLED> (on Android)
# systemButton
Specifies an iOS system button appearance, as defined in Titanium.UI.iOS.SystemButton.
In JavaScript:
var cancelButton = Ti.UI.createButton({
systemButton: Ti.UI.iOS.SystemButton.CANCEL
});
In Alloy, you can omit the namespace when setting the systemButton
property on a <Button>
element:
<Button systemButton="CAMERA"/>
- Titanium.UI.iOS.SystemButton.ACTION
- Titanium.UI.iOS.SystemButton.ACTIVITY
- Titanium.UI.iOS.SystemButton.ADD
- Titanium.UI.iOS.SystemButton.BOOKMARKS
- Titanium.UI.iOS.SystemButton.CAMERA
- Titanium.UI.iOS.SystemButton.CANCEL
- Titanium.UI.iOS.SystemButton.COMPOSE
- Titanium.UI.iOS.SystemButton.CONTACT_ADD
- Titanium.UI.iOS.SystemButton.DISCLOSURE
- Titanium.UI.iOS.SystemButton.DONE
- Titanium.UI.iOS.SystemButton.EDIT
- Titanium.UI.iOS.SystemButton.FAST_FORWARD
- Titanium.UI.iOS.SystemButton.FIXED_SPACE
- Titanium.UI.iOS.SystemButton.FLEXIBLE_SPACE
- Titanium.UI.iOS.SystemButton.INFO_DARK
- Titanium.UI.iOS.SystemButton.INFO_LIGHT
- Titanium.UI.iOS.SystemButton.ORGANIZE
- Titanium.UI.iOS.SystemButton.PAUSE
- Titanium.UI.iOS.SystemButton.PLAY
- Titanium.UI.iOS.SystemButton.REFRESH
- Titanium.UI.iOS.SystemButton.REPLY
- Titanium.UI.iOS.SystemButton.REWIND
- Titanium.UI.iOS.SystemButton.SAVE
- Titanium.UI.iOS.SystemButton.SPINNER
- Titanium.UI.iOS.SystemButton.STOP
- Titanium.UI.iOS.SystemButton.TRASH
# textAlign
Text alignment, specified using one of the Titanium.UI text alignment constants.
Default: Titanium.UI.TEXT_ALIGNMENT_CENTER
# tintColor
Color applied to button's image and title.
Color to be applied to the button's image, but only if the
imageIsMask property is set true
.
This tint color is also applied to the button's title unless the color property is set.
Default: null
# titleid
Key identifying a string from the locale file to use for the button title.
Only one of title
or titleid
should be specified.
# verticalAlign
Vertical alignment for the text field, specified using one of the vertical alignment constants from Titanium.UI.
# Events
# touchfiltered
Fired when the device detects a swipe gesture against the view.
If the button's filterTouchesWhenObscured property
is set true
, then this event will be fired if the touch event was discarded because another app's
overlapping window obscured it.
This is a security feature to protect an app from "tapjacking", where a malicious app can use a system overlay to intercept touch events in your app or to trick the end-user to tap on UI in your app intended for the overlay.
You can use this event to display an alert dialog explaining why the button's action has been disabled. Especially if the overlapping window is completely invisible.
Properties
Name | Type | Description |
---|---|---|
x | Number | X coordinate of the event from the |
y | Number | Y coordinate of the event from the |
obscured | Boolean | Always |
source | Object | Source object that fired the event. |
type | String | Name of the event fired. |
bubbles | Boolean | True if the event will try to bubble up if possible. |
cancelBubble | Boolean | Set to true to stop the event from bubbling. |