Using Alloy’s XML‑Based UI to Build Reusable Buttons in Appcelerator
Learn how Appcelerator’s Alloy MVC lets you define a button once in XML, reuse it across screens, and keep controller logic clean while understanding build‑size and debugging trade‑offs.
16 Aug 2026, 15:23 UTC

Problem: Repeating UI code across screens
When building a mobile app with Appcelerator Titanium, developers often copy the same button layout and click‑handler logic into multiple controllers. This duplication makes the code harder to maintain and increases the chance of inconsistencies.
Thesis: Alloy’s XML‑based UI and automatic binding let you define a button once, reuse it via a view, and keep the controller logic clean.
How Alloy compiles XML to native resources
Alloy treats each .xml file in the views/ folder as a declarative description of a screen. During the build, the Alloy compiler translates that XML into platform‑specific resource files – XIBs for iOS and layout files for Android – and injects the generated code into the Titanium build pipeline. The controller file (.js) is then wired to the generated view objects through automatic property binding.
Worked example: a reusable button component
- Create a new Alloy project (if you don’t have one):
ti new MyButtonDemo -t alloy - Navigate to the project folder:
cd MyButtonDemo - Define a button in
views/button.xml:
<Alloy>
<Button id="myButton" title="Press me" onClick="doClick" />
</Alloy>
- Create a controller for that view in
controllers/button.js:
function doClick(e) {
Ti.API.info('Button clicked!');
}
exports.doClick = doClick;
- Use the button as a child view in the main index view (
views/index.xml):
<Alloy>
<Window>
<Require src="button" />
</Window>
</Alloy>
- Build for the iOS simulator (requires Xcode and the Titanium CLI):
ti build -p ios -T simulator
During the build you may see Alloy’s code‑generation output in the console, for example lines similar to:
[INFO] Alloy compiled views/button.xml → Resources/iphone/button.xib
[INFO] Alloy generated controller button.js → Resources/iphone/button.js
When the app runs, tapping the button prints “Button clicked!” to the device log, confirming that the automatic binding between the XML‑declared onClick attribute and the controller function worked.
Trade‑off: build size and error traceability
Because Alloy creates a native resource file for every XML view, a project with many small views can increase the size of the Resources/ folder and lengthen the build process. Moreover, if a runtime error occurs inside the generated code (e.g., a missing outlet), the stack trace points to the generated file rather than the original XML, which can make debugging harder unless you enable Alloy’s verbose logs (ti build -p ios -T simulator --log-level trace).
Actionable closing
Try adding a second reusable component (e.g., a label with a style sheet) and observe how the Alloy compiler updates the corresponding XIB/layout file without touching the controller. Keep an eye on the build console for the Alloy compilation messages; they are the fastest way to verify that your XML is being turned into native resources as expected. If you notice the build time growing, consider consolidating related UI elements into fewer XML files or using Alloy’s theme and style files to share common attributes.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.