Android 2
Android 2
Contents
Mercurial & Example Repository
Android Parts
Activity Life Cycle
Android Examples
Mercurial
http://mercurial.selenic.com/wiki/
Distributed Source Control System
Written in Python
Mercurial Eclipse
http://www.vectrace.com/mercurialeclipse/
Eclipse Plugin for Mercurial
History
Android Parts
AndroidManifest.xml
Contains information about the application needed by the phone to run it
@string/app_name indicates that the string is to be found in the string resource file under the label app_name.
Activities
Code that does some work
Has a life cycle
Paused, Killed, etc.
Views have an associated activity
Activities can be viewless
11
View
An object that knows how to draw itself to the screen
Set of existing views or widgets
Can create your own view
Games
New widgets
12
Intent
Used to move from screen to screen
Contains
Data
Action
What you want done
MAIN, VIEW, PICK, EDIT, etc
Intent filter
What intents an activity can handle
To move to a new screen register an intent
startActivity(anIntent).
13
Service
Code that runs in the background
Can be long lived
No UI
Example
music player
14
Notifications
Icon that appears in the status bar
Used to notify user
SMS
Voicemail
15
Content Holder
Set of methods to let applications access content provider data
Used to allow multiple applications access data
Address book
16
17
Activity
Single, focused thing that a user can do
Usually each screen has its own activity
An application may have multiple screens, hence multiple activities
An application runs in its own Linux process
18
Activity States
Active
Running activity in foreground of screen
Paused
Lost focus, but still visible
Retains all state information
In extreme memory situations may be killed
Stopped
Not visible
Retains all state information
Often will be killed
Killed
19
20
Activity Example
package edu.sdsu.cs696;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class CountStates extends Activity {
int paused = 0;
int killed = 0;
int stopped = 0;
TextView text;
21
Activity Example
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
paused = savedInstanceState.getInt("paused");
killed = savedInstanceState.getInt("killed");
stopped = savedInstanceState.getInt("stopped");
}
text = new TextView(this);
text.setText("Paused: " + paused + " stopped: " + stopped + " killed "
+ killed);
setContentView(text);
}
22
Activity Example
protected void onResume() {
super.onResume();
text.setText("Paused: " + paused + " stopped: " + stopped + " killed "
+ killed);
}
protected void onStart() {
super.onStart();
text.setText("Paused: " + paused + " stopped: " + stopped + " killed "
+ killed);
}
protected void onStop() {
stopped++;
super.onStop();
}
23
Activity Example
protected void onPause() {
paused++;
super.onPause();
}
protected void onDestroy() {
killed++;
super.onDestroy();
}
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("paused", paused);
outState.putInt("killed", killed);
outState.putInt("stopped", stopped);
}
}
24
Sample Run
25
Android Examples
String Resources
Logging
EditText
Click Listener
Button
Menus
26
MultiAutoCompleteTextView
RadioButton
RatingBar
ScrollView
SeekBar
Spinner
TabHost
TabWidget
TableRow
TimePicker
ToggleButton
TwoLineListItem
VideoView
ViewAnimator
WebView
ZoomButton
ZoomControls
27
28
src/com.android/hello2/R.java
package sdsu.cs696;
//Autogenerated
29
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
30
res/values/strings.xml
31
The easiest way to create String resources is to first put the string in you source code and then use the Anrdoid "Extract Android
String" refactoring.
32
Measurement Units
px
pixels
dip
sp
pt
points
in
inches
mm
millimeters
33
XML Attributes
android.R.styleable
Defines all xml attributes
http://code.google.com/android/reference/android/R.styleable.html
Widget class docs contain links to classes attributes
34
Logging Example
package sdsu.cs696;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
35
36
Use Filters
37
Log Methods
Error
e(String tag, String message, Throwable throw)
e(String tag, String message)
WARN
w(String tag, String message, Throwable throw)
w(String tag, String message)
INFO
i(String tag, String message, Throwable throw)
i(String tag, String message)
DEBUG
d(String tag, String message, Throwable throw)
d(String tag, String message)
VERBOSE (only for development)
v(String tag, String message, Throwable throw)
v(String tag, String message)
38
In order of verbosity
EditText Example
package sdsu.cs696;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
public class HelloAndroid extends Activity {
private EditText messageText;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageText = (EditText) this.findViewById(R.id.message);
messageText.setText("From onCreate");
}
}
39
main.xml with id
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingBottom="3dip">
<EditText android:id="@+id/message"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello, Android from xml2"/>
</LinearLayout>
40
@ symbol in the id tells the XML parser should parse and expand the rest
"+" indicates that a resource should be created if is does not already exist
Listeners
GestureDetector.OnGestureListener
MenuItem.OnMenuItemClickListener
View.OnClickListener
a view is clicked.
View.OnCreateContextMenuListener
View.OnFocusChangeListener
View.OnKeyListener
View.OnLongClickListener
View.OnTouchListener
ViewGroup.OnHierarchyChangeListener
ViewStub.OnInflateListener
ViewTreeObserver.OnGlobalFocusChangeListener
ViewTreeObserver.OnGlobalLayoutListener
ViewTreeObserver.OnPreDrawListener
ViewTreeObserver.OnTouchModeChangeListener
43
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
Button Example
44
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:paddingBottom="3dip">
<EditText android:text="First Edit text"
android:id="@+id/message"
android:autoText="true"
android:ems="25"
android:capitalize="sentences"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/ok"
android:layout_gravity="center"
android:text="@string/button_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
45
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello using String resources!</string>
<string name="app_name">HelloWorldApp</string>
<string name="button_ok">Ok</string>
</resources>
46
Menus Example
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class HelloAndroid extends Activity implements View.OnClickListener {
private EditText messageText;
private static final int DAD_ID = Menu.FIRST;
private static final int MOM_ID = Menu.FIRST + 1;
47
Menus Cont.
public void onClick(View v) {
messageText.setText(messageText.getText() + " click");
}
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
menu.add(0, DAD_ID, 0, R.string.menu_dad).setShortcut('0', 'd');
menu.add(0, MOM_ID, 0, R.string.menu_mom);
return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case DAD_ID:
messageText.setText(messageText.getText() + " Dad");
return true;
case MOM_ID:
messageText.setText(messageText.getText() + " Mom");
return true;
}
return super.onOptionsItemSelected(item);
}
}
48
Menus Cont.
49
This method is called just before your menu is to be displayed. In it you can modify the menu to meet the current needs. In this
example it is not needed, but is here to make sure you know about it.
res/values/strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">From String resource</string>
<string name="app_name">Lecture Examples</string>
<string name="button_ok">OK</string>
<string name="menu_dad">Hi Dad</string>
<string name="menu_mom">Hi Mom</string>
</resources>
50
Display
51