http://www.developer.com/ws/android/programming/refactoring-android-in-eclipse-accelerate-your-android-app-development.html

Back to article

Refactoring Android in Eclipse: Accelerate Your Android App Development


November 1, 2012

After you get a handle on the essentials of developing for a new platform, the next issue you should tackle is making the development process faster and easier -- and the Android platform is no exception.

Eclipse and the ADT plugin provide the Android developer with a powerful IDE that includes plenty of functionality for accelerating Android app development. In this article, I'll introduce you to refactoring Android in Eclipse, and demonstrate how leveraging it can give your Android app development a boost.

Intro to Refactoring in Android App Development

Refactoring is an essential technique for taking the stress out of building an application, allowing you to alter the structure of a program without impacting its functionality. If you're new to refactoring, this may sound like a strange concept (why fix what isn't broken?), but refactoring is handy for implementing design-focused changes. It automates potentially complicated and labor-intensive design tweaks. For example, instead of picking through your code and manually changing every layout to a new type, you can use Eclipse's intuitive 'Change Layout' option to automate this change across your project. Refactoring is also commonly used to make code easier to read and understand.

Eclipse has a range of refactoring options that can be accessed through the dedicated 'Refactor' drop-down menu, which I'll cover in detail in this post.


Refactoring Android in Eclipse
Click here for larger image

1) Change Widget Type

'Change Widget Type' replaces the type of the selected view with a new type. To support this change, Eclipse automatically removes any attributes that are unsupported by the new widget type, adds any attributes that are required by the new type, and changes the view ID to reflect the change.

To use this option, select 'Change Widget Type…' from Eclipse's 'Refactor' drop-down menu. In the subsequent dialog, select the desired widget from the 'New Widget Type' list.

Refactoring Android in Eclipse

If you wish to preview this change by comparing a 'before' and 'after' version of your code, select 'Preview.' Alternatively, press 'Ok' to go ahead and apply the change.

2) Wrap In Container

'Wrap In Container' allows you to select one or more elements and (as the name suggests) wrap them in a new container. To support the change, namespace and layout parameters are automatically transferred to the new parents. If you wrap root elements in a new container, the namespace declaration attributes and layout_attribute references will be transferred to the new root.

To take advantage of this refactoring option:

  • Select 'Wrap in Container' from Eclipse's 'Refactor' menu.
  • Select a new container type.
  • Enter a new layout ID.
  • Select 'Preview' to see how this change will affect your code, or 'Ok' to apply the change.

Refactoring Android in Eclipse

3) Change Layout

'Change Layout' can be applied to any layout view and converts the selected layout to a new type. For some supported items, 'Change Layout' does make the effort to preserve the pre-existing layout, but note that this option often simply changes the opening and closing XML tags, along with the supporting functions of updating IDs, removing unsupported attributes and adding missing attributes.

To implement this change, select 'Change Layout' from the 'Refactor' menu and choose your new layout type.

Refactoring Android in Eclipse

Tip. When the target type is RelativeLayout, the 'Flatten Hierarchy' option can convert the entire layout and flatten it into a single target layout.

4) Remove Container

As the name suggests, 'Remove Container' removes a container and places its children in the parent of the removed container. When you select 'Remove Container' from the 'Refactor' menu, Eclipse opens a preview dialog where you can assess the code changes before applying them to your project.

Refactoring Android in Eclipse

5) Extract Style

If you want to create reusable layouts, styles and themes, the 'Extract Style' option allows you to extract layout fragments and define them as style rules. Once you've clicked 'Extract Style' the wizard will display a list of attributes and their associated values.


Refactoring Android in Eclipse
Click here for larger image

In this dialog, you can choose to remove the extracted attributes, or remove all the attributes of the same name, to create a more unified layout. If an attribute value occurs more than once, Eclipse places a number beside that attribute, allowing you to see at a glance which attributes are the most common.

6) Extract as Include

'Extract as Include' is a visual refactoring operation that supports the reuse of layouts, and is useful for sharing visual elements between layouts. Essentially, this operation allows you to take a section of XML and extract it into a new layout file. Eclipse can also add an {include} tag in the view's original location, linking to the new location.

To extract an element into a new layout:

  • Select the item and choose 'Extract as Include' from the menu.
  • Enter a name for your new layout file

    Refactoring Android in Eclipse

  • Select 'Ok' to pull views from your original layout and automatically create a new layout file.

Tip. In most instances, it is good practice to ensure that the 'replace occurrences in all layouts with include to new layout' checkbox is ticked.

7) Extract Android String

If you're using hardcoded strings in your project, it can be useful to store them in a dedicated strings.xml file, which also allows you to see all the string resources at a glance. In Eclipse, extracting a string is simply a matter of:

1) Select the string you wish to extract.

2) Open the 'Refactor' menu, followed by 'Android' and 'Extract Android String.'


Content Assist for Android
Click here for larger image

3) Enter a new name for your string, optionally edit the string text and define where you wish to place the string. Unless you specify differently, the string will be placed in the res/values/strings.xml file.

4) If you need to, you can specify whether the string should be replaced in all Java files, or in all XML files, using the checkboxes.

5) After you've entered all the relevant information, click 'Ok' to extract the string.

Eclipse Refactoring for Android

Eclipse is an established and powerful IDE that offers integrated functionality to make developing your app faster, easier and less error-prone. In this article, I demonstrated how Eclipse's refactoring options can automate potentially complicated operations. Simplifying these operations using refactoring can help you create and ultimately ship your Android applications faster and with less hassle.

To find out more about how Eclipse can give your day-to-day productivity a boost, check out the Juno documentation, which is a great source of up-to-date information on this powerful IDE.

Sitemap | Contact Us