Generating Java Code
The Java code generator can quickly generate boilerplate code for selected UI elements defined in an
activity's or fragment's layout file. It generates attribute declarations, findViewById() calls, and event handlers.
Construct your layout file, ensuring that it contains all of the UI elements that you want to
programmatically access from your Java Activity or Fragment subclass. Save your work.
Right-click the Activity or Fragment subclass that presents and controls the layout (right-click either the
editor window showing this subclass, or the subclass' filename in the Package Explorer) and select
SourceGenerate Java Code Based on
Layout.
In order for this operation to work, your Activity or Fragment code must not have errors. As well, the
xml file that defines your layout must be well-formed.
The Generate Java Code Based on Layout dialog appears.
Ensure that the Project and Target Class selections are
correct.
From the UI objects list, select those items for which code is to be generated.
To avoid adding duplicate code, if the code generator finds an existing findViewById() statement for a
given UI object, it does not include that UI object in the list.
If at least one of the UI objects is normally used with a listener object, and you do not want
listeners to be created for you, clear the Generate default listeners when possible
option. Otherwise, leave this option selected so that listeners are generated for the appropriate UI
objects.
Click OK.
Note that the OK button isn't enabled until you select at least one UI object from
the list.
Attribute declarations, findViewById() calls, and event handlers are generated, as appropriate, and added to
the selected Activity or Fragment subclass.