Thursday, January 27, 2011

Android Hello, NotePad tutorial

If you have been following the notepad tutorial at Android's developer page here, everything would go fine until you reach Part 2. It spits out quite a lot of errors, when you import the example code given for the second section. There is no solution in one place for the errors you encounter. Worse, the solutions found around the web, put together also doesn't work at times. So following is what has been missing - A one stop solution!



Warning: This's not for you, if you're completely new to Android. You must be ready for the Notepad tutorial with quite the basic knowledge of resources, build targets etc.

Problem 1: String types
To start with, the part 2 of the Notepad tutorial (hence referred as N2), is built against API level 8 (Froyo).
If you are adamant to only work with the API version your phone is running (mine being 7 - Eclair) and not even install the higher versions, then BANG, that's the cause of 

Error: String types not allowed (at layout_height with value 'match_parent')
Error: String types not allowed (at layout_width with value 'match_parent')

Solution 1:
Install a new build target 'API level 8' using the Android AVD and SDK manager, change the build target  property of the project. 

or You only want to stick with Eclair, change the layout properties (in note_edit.xml) from match_parent to fill_parent. Match_parent is a new attribute added in API 8.

Problem 2: R resolution

The resource file R that has to be generated by Android SDK for you, is not being generated.

Solution 2:
A) Manually clean the project in Eclipse (Project > Clean)
and re-build the project. If 'Build Project is disabled, Force build the project, by unchecking 'Build Automatically'.

If that doesn't work,
B) Delete the 'gen' folder and create new 'source folder' named 'gen' in the project. Re-build project.

Problem 3: 

"The project cannot be built until the build path errors are resolved"

Occasionally this error might also crop up. In that case, modify the build path of the project. I understand, that's confusing! Although you don't need an external library, just add any third party jar file in the build path and save the settings and later remove it.

This's to force eclipse to refresh it's Build-path settings.

Happy Programming!!!