Game Development Reference
In-Depth Information
button=
new
Button(
this
);
button.setText( "Touch me!" );
The next line in
onCreate()
sets the
OnClickListener
of the
Button
.
OnClickListener
is a
callback interface with a single method,
OnClickListener.onClick()
, which gets called when the
Button
is clicked. We want to be notified of clicks, so we let our
HelloWorldActivity
implement
that interface and register it as the
OnClickListener
of the
Button
.
button.setOnClickListener(
this
);
The last line in the
onCreate()
method sets the
Button
as the content
View
of our
Activity
.
View
s can be nested, and the content
View
of the
Activity
is the root of this hierarchy. In our
case, we simply set the
Button
as the
View
to be displayed by the
Activity
. For simplicity's
sake, we won't get into details of how the
Activity
will be laid out given this content
View
.
setContentView(button);
}
The next step is simply the implementation of the
OnClickListener.onClick()
method, which
the interface requires of our
Activity
. This method gets called each time the
Button
is clicked.
In this method, we increase the
touchCount
counter and set the
Button
's text to a new string.
public void
onClick(View v) {
touchCount++;
button.setText("Touched me"+touchCount+"times");
}
Thus, to summarize our
Hello World
application, we construct an
Activity
with a
Button
.
Each time the
Button
is clicked, we set its text accordingly. This may not be the most exciting
application on the planet, but it will do for further demonstration purposes.
Note that we never had to compile anything manually. The ADT plug-in, together with Eclipse,
will recompile the project every time we add, modify, or delete a source file or resource. The
result of this compilation process is an APK file ready to be deployed to the emulator or an
Android device. The APK file is located in the
bin/
folder of the project.
You'll use this application in the following sections to learn how to run and debug Android
applications on emulator instances and on devices.
Running the Application on a Device or Emulator
Once we've written the first iteration of our application code, we want to run and test it to
identify potential problems or just be amazed at its glory. We have two ways we can achieve this:
We can run our application on a real device connected to the development
ï?®
PC via USB.
We can fire up the emulator included in the SDK and test our application there.
ï?®
In both cases, we have to do a little bit of setup work before we can finally see our application
in action.