Android Session2
Android Session2
APPING-MASTERS-2013-VERDIER
PAGE 1 OF 7
ANDROID: TP1
APPING-MASTERS-2013-VERDIER
There is a very useful tool called Hierarchy Viewer that let you see how the UI of a running activity is
structured.
This tool is part of the SDK so it can be found in the SDK/Tools directory, but recently Google included
it in the ADT plugin. As such, its now accessible directly in Eclipse [Windows->Open perspective>Other->Hierarchy View].
Launch
the
hierarchy
viewer
Your application has to be running for the tool to see it. If you dont see your application, refresh the list of
currently running activities in the Windows tab.
To give you a better understanding of how our application UI is structured, here is its hierarchy view.
PAGE 2 OF 7
ANDROID: TP1
APPING-MASTERS-2013-VERDIER
background
onPostExecute(): called when the AsyncTask finishes; its the place where you are allowed to
access your UI
@Override
protected String doInBackground(String... params)
{
//your code here
return params[0];
}
@Override
protected void onPostExecute(String result)
{
//your code here
Log.d(TAG, result);
}
PAGE 3 OF 7
ANDROID: TP1
APPING-MASTERS-2013-VERDIER
Use
the
doInBackground()
method
to
sleep
1000
ms
and
then
display
a
toast
notication
in
the
onPostExecute()
method.
Use your DelayedMessage class to display the message when the DelayedMessage button is clicked.
Create
a
new
BackgroundImageDl
class
inside
your
activity
the
same
way
your
created
your
previous
DelayedMessage
class.
Here some code to download an image from a URL string and generate a Drawable object that you can
display in your ImageView:
URL url = new URL(stringUrl);
//extra sleep to simulate a longer download
Thread.sleep(1000);
Object content = url.getContent();
InputStream is = (InputStream) content;
Drawable d = Drawable.createFromStream(is, "src");
Your doInBackground() method has to download the image from the URL string that is passed to the
AsyncTask.Execute() method, generate a Drawable object and then return it so that the
onPostExecute() method can display it.
Download
and
display
the
following
image
when
the
user
clicks
on
the
DelayedSmiley
url: http://education.3ie.fr/android/smiley.png
PAGE 4 OF 7
ANDROID: TP1
APPING-MASTERS-2013-VERDIER
The way we added the BackgroundImageDl class in our main Java file is eective but kind of ugly. A
better way to do it is to use a separate class for your AsyncTask.
Since we are outside of our Activity, we dont have the reference to the imageView so we have to give it to
this class we are creating.
Here is what your class is going to look like. You have to fill the blanks and then use this ImageDownload
class instead of our previous DelayedMessage.
public class ImageDownloader
{
public void download(String url, ImageView imageView)
{
BitmapDownloaderTask task = new BitmapDownloaderTask(imageView);
task.execute(url);
}
@Override
// Actual download method, run in the task thread
protected Drawable doInBackground(String... params) {
//TODO
}
@Override
protected void onProgressUpdate(Integer... values) {
//nothing to do here
}
@Override
protected void onPostExecute(Drawable d) {
//TODO
}
PAGE 5 OF 7
ANDROID: TP1
APPING-MASTERS-2013-VERDIER
Part 2: RelativeLayout
First step: RelativeLayout presentation
This
step
is
not
an
exercise,
just
some
explanations.
The RelativeLayout works in such a way that the programmer describes how the various widgets are
related to one another. It is very powerful but not as easy to understand as the LinearLayout.
The best way to explain it is to look at a simple example.
We want to make this simple UI that cannot be done with only one LinearLayout:
To make this kind of interface with only LinearLayouts, we would have to create multiple nested
LinearLayouts because of the 2 buttons we want to have next to each other.
Here is how you could structure your layout (not the complete XML):
<LinearLayout android:orientation="vertical" >
<EditText />
<LinearLayout android:orientation="horizontal" >
<Button />
<Button />
</LinearLayout>
</LinearLayout>
In RelativeLayout, we can easily place the 2 buttons next 2 each other without having to use another
nested Layout, so the UI is faster to render.
Here is how you could structure the previous UI in relative layout (not the complete XML):
PAGE 6 OF 7
ANDROID: TP1
APPING-MASTERS-2013-VERDIER
<RelativeLayout >
<EditText
android:id="@+id/editText1"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<Button
android:id="@+id/button1"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1" />
<Button
android:id="@+id/button2"
android:layout_below="@+id/editText1"
android:layout_toRightOf="@+id/button1" />
</RelativeLayout>
Notice how the button2 is configured to be below the editText and to the right of the button1.
PAGE 7 OF 7