Mastering Eclipse Content Assist for Android
When you're just beginning to develop Android apps in Eclipse, your immediate concern will be coming to grips with the ins-and-outs of a new platform, but once you've mastered the essentials you'll want to focus on making development easier, less error-prone and ultimately faster. This is where some extra Eclipse know-how comes in handy.
As essential way to speed up your day-to-day development work is to harness Eclipse's powerful and time-saving 'content assist' feature. Content assist provides context-sensitive code completion via a pop up window, saving you from typing out every line of code in full, while also reducing the chances of spelling mistakes and typos.
Accessing Eclipse Content Assist for Android
To access content assist for Android, press Ctrl and the space bar. This will open a pop up containing potential endings for whatever code you're currently typing.
If you're not a fan of keyboard shortcuts, the content assist option can also be found in Eclipse's Edit menu.
In this example, I'll use the simple scenario of specifying the width parameter for TextView to demonstrate how you might use content assist for Android in your project.
- Type android:layout and press Ctrl+space. This will activate content assist with its list of context-sensitive proposals.
- Scroll down until you find the desired attribute. In this example, we are looking for 'width.'
- Clicking on widthonce opens a short description of the option (note, a description is currently not available for all options.)
- Double clicking, or pressing Enter inserts this attribute into your code.
- Press Ctrl+space again and content assist will suggest new context-sensitive attributes.
Tip:You can press Ctrl+space without typing anything first if you wish to view all the constants and methods of a class.
Troubleshooting with Content Assist for Android
Despite being such a useful and intuitive tool, Eclipse's content assist for Android functionality can sometimes be a source of pain for Android developers, as it may stop working when you update your Eclipse installation -- or seemingly stop working at random! In this section, we'll address some common complaints and share some techniques for getting content assist for Android back on track.
Content Assist Is Displaying 'No Default Proposals'
If your code completion window is displaying a 'No Default Proposals' message, your default options might be at fault. To address this:
- Open the Windows menu.
- Select Preferences.
- Double-click the Java button on the left-hand menu.
- Double-click Editor, followed by Content Assistant.
- Select Advanced.
- Select the proposal kinds contained in the default content assist list. This should solve your No Default Proposals problem.
Content Assist Is Always Displaying the Same Non-Usable Suggestions
If content assist is constantly bringing up non-usable suggestions (for example "default namespace - Default namespace attribute" or "xsinsp - XML schema namespace"), the problem may be that your XML file has been assigned to the XML editor, rather than the customized Android XML editor. To check whether this has occurred in your installation:
- In Eclipse's Package Explorer, right-click on the XML file where you're struggling to call content assist.
- Select Open Withfrom the subsequent menu.
- Ensure the Android Common XML option is selected. If it isn't, then chances are this is behind your content assist for Android problems.
Upgrading to Eclipse 'Juno' Has Broken My Content Assist
If you've just upgraded to the latest, 'Juno' release of Eclipse, you may suddenly start seeing the dreaded "No Default Proposals" when you call content assist, despite it working perfectly before. In this situation, there are several possible fixes that can get content assist running smoothly again.
- Open the Window menu, followed by Preferences.
- Select Java from the left-hand menu.
- Select Editor, followed by Content Assist.
- Select Advanced.
- Click the Restore Defaults option.
If this doesn't solve the problem, re-open the above dialog and ensure the Java Proposals checkbox is selected in the Default Proposal Kinds box.
And If All Else Fails ...
Closing and restarting Eclipse in 'clean' mode will remove cached data stored by the Eclipse runtime and force cache reinitialization. This should rectify any outstanding issues with content assist for Android.
- Close your Eclipse installation.
- Locate the eclipse.inifile.
- Select the Edit option, and open the file in Notepad.
-cleanas the first argument on the first line.
- Hit Save.
Restart Eclipse. Note, Eclipse will typically take much longer to load with the
-cleanargument in place, so be patient!
Tip: After you've run the
-cleanargument once, you should remove it. Leaving the
-cleanargument in the eclipse.ini file means you will keep encountering these increased startup times.
Alternatively, you can open a clean installation using your Eclipse shortcut.
- Right-click on your shortcut.
- Open the Properties dialog.
- Select the Target box, and type the
-cleanargument after the current Target.
Again, remember to remove the
-cleanargument once you've finished with it, as it greatly increases Eclipse's load time.
Having content assist for Android handy can greatly increase your productivity. Not only will it save you typing out every character of your code, but its context-sensitive nature means you are less likely to insert the incorrect code or make typos/spelling mistakes, which aren't always easy to spot. In this article, we covered the essentials of coming to grips with content assist for Android, and shared some tips for keeping this useful functionality running smoothly.
Eclipse has plenty more ways to accelerate your day to day development work - if you're interested in learning more, then the Juno documentation is a great source of up to date information.