02.activity States GUI View - PDF
02.activity States GUI View - PDF
Examples
Building Blocks Of Android Application
AndroidManifest.xml
Activities
Views
Intents
Services
Notifications
Content Providers
AndroidManifest.xml
Contains information about the application needed by the phone to run it
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.hello2"
android:versionCode="1"
android:versionName="1.0.0"
>
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".HelloAndroid"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
10
@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
Contains
Data
Action
What you want done
MAIN, VIEW, PICK, EDIT, etc
Intent filter
What intents an activity can handle
Can be long
lived No UI
Example
music player
Notifications
Icon that appears in the status bar
Address book
Activity Life Cycle
Activity
Single, focused thing that a user can do
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
Activity Example
package test.test.a1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
}
Sample Run
Android Examples
String Resources
Logging
EditText
Click Listener
Button
Menus
Some Android Views
• AutoCompleteTextView MultiAutoCompleteTextView
• Button RadioButton
RatingBar
• CheckBox
ScrollView
• CheckedTextView
SeekBar
• Chronometer Spinner
• DatePicker TabHost
• DigitalClock TabWidget
• EditText TableRow
TimePicker
• ExpandableListView
ToggleButton
• Gallery
TwoLineListItem
• GridView VideoView
• ImageButton ViewAnimator
• ListView WebView
• MapView, ZoomButton
ZoomControls
Using String Resources Example
import
android.app.Activity;
import android.os.Bundle;
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.
Using Text in main.xml
res/layout/main.xml
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hello, Android from xml"/>
</LinearLayout>
Measurement Units
px pixels
dip, dp device independent pixels
sp scaled pixels — best for text size
pt points
in inches
mm millimeters
XML Attributes
android.R.styleable
Defines all xml attributes
http://code.google.com/android/reference/android/R.styleable.htm
l
import ndroid.app.Activity;
import android.os.Bundle;
import android.util.Log;
Log Methods
Error
In order of verbosity
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)
EditText Example
package sdsu.cs696;
import
android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
@ 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
See id.message exists now
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class id {
public static final int message=0x7f050000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int Foo=0x7f040002;
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
Click Listener Example
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.
43
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>
</resources>
Display