Java Programming for Android

crookpatedspongySoftware and s/w Development

Dec 2, 2013 (3 years and 6 months ago)

57 views

Android Introduction

Outlines


Acitivity

Lifecycle


UI View


UI Input


State

What is Android


“Android is a software stack for mobile devices
that includes an operation system,
middleware and key
apllications
. The Android
SDK provides the tools and APIs necessary to
begin developing applications on the Android
platform using the Java programming
language.”

The Sample Application




Preconditions


Install Java 6


Install Android SDK


Learn Java

Project: Directory and Files


Prject


Src

Gen

Assets

Res



-

drawable
-
hdpi



-

drawable
-
mdpi



-

drawable
-
ldpi



-

layout



-

menu



-

values

File: AndroidManifext.xml


Configuration for the application


Versioning, icon,
initializatioon

<manifest package="
com.monead.games.android.sequence
"



android:versionName
="01.06"
android:versionCode
="17"
>


<
application
android:icon
="@
drawable
/
launch_seq
"



android:label
="@string/
title_app_name
">


<
activity
android:name
=".Sequence"



Android:label
="@string/
title_app_name
">



<
intent
-
filter>...</intent
-
filter>


</
activity>


</
application>


<
uses
-
sdk

android:targetSdkVersion
="8"



android:minSdkVersion
="4"
>


</
uses
-
sdk
>

</
manifest>

Directory:src


Application source
code
\


Directory: res/
drawable
-
*


Image files


3 directories to deal with various screen
resolutions

Directory:res
/layout


UI definitions


Layouts


Widgets

Directory: res/main


Defines first interface to the user


Directory: res/values


Constants, supports internationalization

Activity Lifecycle


An

Activity

is an application component that
provides a screen with which users can
interact in order to do something, such as dial
the phone, take a photo, send an email, or
view a map. Each activity is given a window in
which to draw its user interface. The window
typically fills the screen, but may be smaller
than the screen and float on top of other
windows.

Activity Lifecycle Depiction


Lifecycle Method


Void
onCreate
(Bundle
savedInstanceState
)


Void
onStart
()


Void
onRestart
()


Void
onResume
()


Void
onPause
()


Void
onStop
()


Void
onDestroy
()

onCreate
(Bundle
savedState
)


onPause
() and
onResume


Explore Some Source Code


We’ll switch to Eclipse and look at an Activity
using some lifecycle methods

UI View


Perferred

approach to define the UI is to use
XML files located in the res/layout directory


Using the Eclipse plugin for Android
development simplifies the development cycle
since there is a build step required to turn the
UI definitions into code so that you may
programmatically access the widgets defined
in the XML file

Simple Layout


Code using the Prior UI


Result Screen


Prgrammatic

UI


Can define the
vew

by extending the
android.view.view

class


Overrie

the
onDray
(Canvas canvas) method


Receives a Canvas instance, which represents
the screen


Use invalidate() method to force the view to
redraw itself on the device


Drawing on Canvas


Resulting Screen


Internationalization


Define strings in a resource file


Examples





res/values/strings.xml


<?xml version=
"1.0" encoding="utf
-
8"?>




<resources>



<string name=
"
app_name
">
Sudoku</string>



<string name=
"
main_title
">Android
Sudoku</string>



<string name=
"
continue_label
">Continue</string>



<string name=
"
new_game_label
">New Game</string>



<string name=
"
about_label
">About</string>



<string name=
"
exit_label
">Exit</string>



<string name=
"
settings_label
">Settings...</string>



<string name=
"
settings_title
">
Sudoku settings</string>



<string name=
"
settings_shortcut
">s</string>


........

Res/values/color.xml


<?xml version=
"1.0" encoding="utf
-
8" ?>


<resources>



<color name=
"background">#00000000</color>



<color name=
"
puzzle_background
">#ffe6f0ff</color>



<color name=
"
puzzle_hilite
">#
ffffffff
</color>



<color name=
"
puzzle_light
">#64c6d4ef</color>



<color name=
"
puzzle_dark
">#6456648f</color>



<color name=
"
puzzle_foreground
">#ff000000</color>



<color name=
"
puzzle_foreground_input
">#ff0000ff</color>



<color name=
"puzzle_hint_0">#64ff0000</color>



<color name=
"puzzle_hint_1">#6400ff80</color>



<color name=
"puzzle_hint_2">#2000ff80</color>



<color name=
"
puzzle_selected
">#64ff8000</color>



<color name=
"
puzzle_red
">#ffffff00</color>



</resources>

Get a Value


In a View


<?xml version="1.0" encoding="utf
-
8"?>


<
ScrollView



xmlns:android
="http://schemas.android.com/
apk
/res/android"



android:orientation
="vertical"



android:layout_width
="
match_parent
"



android:layout_height
="
match_parent
"



android:padding
="10dip">



<
TextView




android:id
= "@+id/
about_content
"




android:layout_width
="
wrap_content
"




android:layout_height
="
wrap_content
"




android:text
="@string/
about_text
"




/>


</
ScrollView
>

Get a Value In Program


UI Input


Typical
input sources




Keyboard




Touch Screen


Event
-
based Paradigm




Register to listen for device events


Could be physical or
vitual





-

No difference for developer


Activity will receive the events using callback
methods




-

Override to process input




onKeyDown
()



onKeyUp
()




onKeyLongPress
()




onKeyMultiple
()

onKeyDown
() Example


Persisting State

The Android SDK provides emulators
that can
mimic
a
variety
of Android
versions, screen
dimensions
and phone
behaviors


Runs slower than on a real
phone So
don't get too
worried if your
application feels
sluggish in the
emulator

Provides
support for all the features
and lifecycle
events including
persistingstate
, writing local files,
etc

From
Eclipse it launches
automatically when
you
run the application

Questions?