January 25, 2021
Hot Topics:

Refactoring Android in Eclipse: Accelerate Your Android App Development

  • By Jessica Thornsby
  • Send Email »
  • More Articles »

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.

Originally published on https://www.developer.com.

Page 1 of 2

This article was originally published on November 1, 2012

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date