Refactoring Android XML Layout Files
In addition to common refactoring, IntelliJ IDEA provides a number of the Android-specific refactorings for the Android layout definition XML files. Most of these refactorings are available in both design modes:
- The manual mode, right in the editor tab where the target layout definition file is opened, and from the Structure tool window.
- In the Android UI Designer, from the Design pane or Component Tree.
You can toggle between these modes by switching between the Design and Text tabs in the editor where the layout definition file is opened.
In this topic:
With the extract style refactoring, you can create a style from a layout XML tag. IntelliJ IDEA creates a style declaration in the
and converts the tag attributes into style properties declared in the
item elements. The benefit is that now you no longer need to parse an entire
resource definition file updating the tag attributes to edit a layout. All you need to do is update the style definition, whereupon the changes are applied automatically
wherever the style is referenced.
To apply the extract style refactoring
Do one of the following:
- In the visual mode, select the component where the source XML tag is used in the definition on the canvas in the Design pane, or in the Component Tree.
- In the manual mode, position the cursor anywhere inside the source XML tag.
- From the main menu, select Refactor | Refactor This or press Ctrl+Shift+Alt+T. In the pop-up that appears, select Style from the Extract group. Alternatively, choose Refactor | Extract | Extract Style on the context menu of the selection.
- In the Extract Android Style dialog box that opens, specify the name for the style to be created and select the tag attributes to be included as style properties.
- To have IntelliJ IDEA automatically update the layout definition by replacing tag attributes with references to the newly created style, select the Launch 'Use Style Where Possible' refactoring after the style is extracted checkbox.
This refactoring is opposite to the Extract Style refactoring and results in adding all attributes defined in a style to one or all components where this style is applied. This refactoring is also used when you need to merge a parent style with its inheritor.
You can invoke the Inline Style refactoring from a style definition or from a component to which the style is applied. In the first case, IntelliJ IDEA removes the style definition and adds the corresponding attributes to all components where it is used. When refactoring is invoked from a component, you can apply it either to all components with this style, or only to the current one.
To apply the Inline Style refactoring
To invoke the refactoring from a style definition:
- Open the style definition file, position the cursor at the name of the style to be inlined, and choose Refactor | Inline from the context menu or press Ctrl+Alt+N.
- The Inline Android Style dialog box that opens provides only one option (Inline all references and remove the style) which is selected by default. As a result, IntelliJ IDEA converts the attributes of the style into XML tags, adds them to the definitions of all components where the style is used, and removes the style definition.
To invoke the refactoring from a component definition:
Select a component with the style in question:
- In the visual mode, select a component with this style in the Design pane or in the Component Tree. Then choose Refactor | Inline Style from the main menu or from the context menu of the selection.
- In the manual mode, select a component with this style in the Structure view and choose Refactor | Inline Style from the main menu or from the context menu of the selection.
In the Inline Android Style dialog box that opens, configure the refactoring:
- To have IntelliJ IDEA convert the attributes of the style into XML tags, add them to the definitions of all the components where the style is used, and remove the style definition, choose Inline all references and remove the style.
- To have the refactoring applied to the current component only, choose Inline this usage and keep the style.
- Specify how you want to apply the changes.
- Select a component with the style in question:
IntelliJ IDEA supports re-using Android layouts:
IntelliJ IDEA moves a part of an existing layout definition to a separate layout definition file and references it in the original layout definition through
an automatically inserted
<include/> tag. In IntelliJ IDEA, this operation is called extract layout refactoring.
To extract a layout
Select the fragment to extract as a separate layout. Do one of the following:
- In the manual mode, select the code fragment to extract or position the cursor in the root element of the fragment.
- In the visual mode, select the component or a group of components to be extracted in the Design. pane or in the Component Tree.
- From the main menu, choose Refactor | Refactor This or press Ctrl+Shift+Alt+T. In the pop-up list that appears, choose Layout from the Extract group. Alternatively, choose Refactor | Extract | Extract Layout from the context menu of the selection.
- In the Extract Android Layout dialog box that opens, specify the name of the file where the extracted layout definition will be stored and the name of its parent folder. You do not need to create the file and the folder in advance, IntelliJ IDEA will create them automatically.
Create a list of configuration qualifiers for the layout. This list determines
the set of alternative resources that must be provided to enable running the application in specific target device configurations.
- To add a qualifier to the set, select it in the Available Qualifiers list, click the button, and specify the value of the qualifier.
- To remove a qualifier from the set, select it in the Chosen Qualifiers list and click the button.
For detailed information on the qualifiers, their meaning, acceptable values and format, see specification of configuration qualifiers for alternative resources.
Inline Layout Refactoring
This refactoring is opposite to the Extract Layout refactoring
and results in embedding a layout that was referenced through an
<include/> tag and removing the tag.
The Inline Layout refactoring is invoked from the
<include/> tag in the layout definition file.
You can have IntelliJ IDEA apply it either to all references and remove the layout definition file, or only to the current reference.
To inline a layout
- Open the XML definition file from which the layout in question is referenced.
Navigate to the
<include/>tag which references the layout in question and position the cursor at the layout name.
Do one of the following:
- Choose Refactor | Inline from the main menu.
- Choose Refactor | Refactor This from the main menu, and then choose Inline in the pop-up list that opens,
- Press Ctrl+Alt+N.
- Choose Refactor | Inline from the context menu.
In the Inline Android Layout dialog box that opens, configure the refactoring:
To have IntelliJ IDEA embed the layout into all files where it is referenced through
<include/>tags and remove the referenced layout definition file, select Inline all references and remove the file.
- To have the refactoring applied to the current reference only, select Inline this usage and keep the file.
- To have IntelliJ IDEA embed the layout into all files where it is referenced through
- Specify how you want to apply the changes: