On the [Android Developer site]
(http://developer.android.com/guide/topics/ui/layout/gridview.html)
(accessed 20121212) there is sample code to illustrate basic use of the
GridView
layout. Since layouts are the foundation of Android code,
they are useful for study at the outset of learning to write for the
platform.
The gridview.html
page seems to date from "09 Dec 2012 21:51", but the
code does not run on Eclipse (Juno 4.2, service release 1, build ID
20121004-1855; Android SDK 4.1.2) as written. Below are my notes on
changes necessary to make it run.
- The code provides the content for
res/layout/main.xml
, but by default Eclipse now names this fileactivity_main.xml
. Choose one or the other and make sure it is referred to consistently throughout the code. - The code provides a
HelloGridView.java
class containing anonCreate()
method, but by default Eclipse now places the skeleton of that method in a class calledMainActivity.java
, not mentioned Google's tutorial. I recommend placing the content of Google'sonCreate()
method into the skeleton generated by Eclipse. If, instead, you choose to name your classHelloGridView.java
and to deleteMainActivity.java
, then you must remember to edit theAndroidManifest.xml
file accordingly; that is where the<application ... <activity ... / > </application>
entry names the class of the project's main activity. - The code prescribes a
/res/drawable
directory, but you will have to create that; Eclipse currently creates only/drawable-hdpi
,/drawable-ldpi
, etc. directories in/res
. - Eclipse will supply
import
statements for all recognized methods in importable classes if you press CTRL-SHIFT-O (or CMD-SHIFT-O on a Mac keyboard). But ifimport android.R;
is supplied as a result, you may find errors being raised on lines calling the automatically generatedR
class. If so, delete or comment out the lineimport android.R;
.
It's likely that the sample code on Google's site will be updated before long, but in the past month there seems to have been a gap between the posted code and what is needed to run (and be studied) on the current Eclipse installation.
[end]