Vous êtes sur la page 1sur 16

With Android telephones and drugs making

their manner into an increasing number of


pockets and luggage, dipping a toe into Android
coding is becoming more famous too. And it is
a first-rate platform to code for -- the API is
essentially well-documented and clean to use,
and it is just a laugh to write down something
that you may run on your very own phone. You
don't even need a smartphone at first, due to the
fact you can write and take a look at code in an
emulator on your Linux PC. In the first of this
-component intro to Android coding, get a
fundamental timer app up and going for walks
and start gaining knowledge of about the
Android API. This educational assumes a few
basic familiarity with Java, XML, and
programming concepts, however even if you're
shaky on the ones, sense unfastened to comply
with alongside!
Dev environment and getting commenced

A notice on versions: the maximum latest


model of Android is 4.2 (Jelly Bean), but as
you can see from this Wikipedia chart, there
aren't many people the usage of it but. You're
better off coding for one or each of 4.0 (Ice
Cream Sandwich) or 2.Three (Gingerbread), in
particular as Android is totally forwards-well
suited (so your 2.Three code will run on four.2)
however not constantly backwards-compatible.
The code here have to work on either four.0 or
2.3.

Android countdown timer

The fastest manner to get your dev environment


set up is to down load the Android Bundle.
You'll additionally need JDK 6 (now not just
JRE); note that Android isn't always well
matched with gcj. If you have already got
Eclipse, or desire to use another IDE, you may
set it up for Android as described here.

Now, create a project called Countdown both


the usage of Eclipse, or from the command
line. I set the BuildSDK to four.Zero.Three, and
minimum SDK to two.2, and (in Eclipse) used
the BlankActivity template.

My First Android Project: Layout

For our very first program, we are going to do


is to reveal a timer that counts down from 10
seconds when you click a button. Before
writing the code, permit's create the interface -
- what the user will see when they begin the
app. Open up
res/layout/activity_countdown.Xmlto create an
XML format, the use of both the Eclipse
graphical editor, or a textual content/XML
editor, to enter this:
<RelativeLayout
xmlns:android="http://schemas.android.c

xmlns:tools="http://schemas.android.com

android:layout_width="match_parent"

android:layout_height="match_parent"
>
<TextView
android:id="@+id/time_display_box"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="60dp"
android:text="@string/_00_30"
android:textAppearance="?
android:attr/textAppearanceLarge"/>
<Button
android:id="@+id/startbutton"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/time_disp

android:layout_centerHorizontal="true"

android:layout_marginTop="41dp"
android:text="@string/start" />

</RelativeLayout>
Note the references
to @string/start and @string/__00_30 .
These values are stored
in res/values/strings.xml :
<string name="start">Start</string>
<string
name="_00_30">00:30</string>
This illustrates the standard way of
referring to Android resources. It's best
practice to use string references rather
than hard-coding strings.
You'll notice the thing that makes this a
surprisingly easy first project: the
Android API includes a
CountDownTimer that you can use. We
set up this, and the countdown display,
as private member variables.
In onCreate() we use the built-
in setContentView method to grab our
XML layout The R.foo.bar syntax is a
standard way to refer to Android XML
resources in your code, so you'll see it a
lot.
findViewById is another method you'll
use a lot; here, it grabs the display and
the Start button from the XML layout.
For the Button to work when clicked, it
needs an OnClickListener. This is an
interface, so must be subclassed. We
could create a whole new MyButton
class to do this, but this is overkill for a
single button. Instead, we do it inline,
creating a new OnClickListener and
its onClick() method. Ours simply
calls showTimer() on the number of
milliseconds we want to use (currently
hard-coded).

So what does showTimer() do?


private void showTimer(int
countdownMillis) {
if(timer != null) { timer.cancel(); }
timer = new
CountDownTimer(countdownMillis,
MILLIS_PER_SECOND) {
@Override
public void onTick(long
millisUntilFinished) {
countdownDisplay.setText("counting
down: " +
millisUntilFinished /
MILLIS_PER_SECOND);
}
@Override
public void onFinish() {

countdownDisplay.setText("KABOOM!"
}
}.start();
}
The CountDownTimer class does
maximum of the paintings for us, which
is quality. Just in case there may be
already a strolling timer, we start out via
cancelling it if it exists. Then we create
a brand new timer, setting the number of
milliseconds to be counted down (from
the showTimer() parameter) and the
milliseconds in keeping with rely
interval. This interval is how frequently
the onTick()callback is fired.

CountDownTimer is some other abstract


class, and the __onTick()__ and
__onFinish()__ strategies ought to be
carried out when it is subclassed. We
override onTick() to decrease the
countdown show with the aid of a 2d on
each tick; and override onFinish() to set
a show message once the countdown
finishes. Finally, start() sets the timer
going.

If you choose 'Run' in Eclipse, you could


pick out to run this as an Android app,
and an emulator will automatically be
generated and run for you. Check out the
Android docs in case you want greater
records on putting in place an emulator,
or on going for walks an app from the
command line.

Congratulations, you've got written your


first Android app! In the second a part of
this series, we'll have a more in-depth
study the structure of an Android app,
and make a few enhancements to the
timer to enter a countdown time, a Stop
button, and menu alternatives. We'll also
take a look at jogging it on a bodily
telephone in preference to the software
program emulator.

Vous aimerez peut-être aussi