New XML Refactorings for Android Developers

Recently, we’ve added a number of new refactorings for Android XML layout files. All the refactorings can be used right in the editor, in the UI designer (from “Design” panel or “Component Tree”) or can be invoked from the Structure View.

So, here’s what we’ve prepared for you:

  • Extract Style. Styles in Android is an analogue of cascading stylesheets in web design — they allow you to separate the design from the content. With this new refactoring you can create a new style from a layout XML tag. To perform refactoring just place the caret into an XML tag and choose Refactor | Extract | Style… from a popup menu in XML editor or Refactor | Extract Style… in the UI designer.   Moreover, it is possible to extract parent style from an existing style declaration.

  • Inline Style. Sometimes you need to perform the reverse operation: add all the attributes defined in a style to the components it is applied to (all or one of) or merge parent style with its inheritor. To perform such refactoring place the caret into style declaration of some component the style applied to and choose Refactor | Inline Style… from the pop-up menu. Also, you can use common “Inline…” action of the IDE, just place the caret into “@style/…” reference and press Ctrl+Alt+N (Or Meta+Alt+N on Mac). In the UI designer the refactoring can be invoked by choosing Refactor | Inline Style…
  • Extract Layout. In Android it is possible to re-use your layouts using <include> tag. With this refactoring you’re able to move a part of your existing layout to separate xml file and insert <include> tag instead. To do it, select one or more elements in your layout and choose Refactor | Extract | Layout… from the pop-up menu. In UI designer mode the refactoring can be invoked from Refactor | Extract layout…
  • Inline Layout. This refactoring allows you to inline some included layout and remove <include> tag. To do this, choose an included file in Project View or place the caret to the “@layout/…” reference and press Ctrl+Alt+N. As well as other refactorings, this one is also available from the pop-up menu in UI designer.

Tip: One good way to invoke all these and other refactorings is the ‘Refactor This’ popup (Refactor | Refactor This… from the menu). Remember its hotkey and you have them all.

Reminder: All Android-related functionality is available in free IntelliJ IDEA Community Edition.

You’re welcome to try these features in the latest IntelliJ IDEA 12 EAP and let us know what you think.

image description