0% found this document useful (0 votes)
89 views

02.activity States GUI View - PDF

This document provides examples of building blocks of Android applications including the AndroidManifest.xml file, activities, views, intents, services, notifications, and content providers. It describes what each component is used for at a high level. It also provides an example of an activity lifecycle and using string resources, logging, EditText, and click listeners in Android applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
89 views

02.activity States GUI View - PDF

This document provides examples of building blocks of Android applications including the AndroidManifest.xml file, activities, views, intents, services, notifications, and content providers. It describes what each component is used for at a high level. It also provides an example of an activity lifecycle and using string resources, logging, EditText, and click listeners in Android applications.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 44

Activity States & GUI View

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

Has a life cycle


Paused, Killed, etc.

Views have an associated activity

Activities can be viewless


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
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).
Service
Code that runs in the background

Can be long

lived No UI

Example
music player
Notifications
Icon that appears in the status bar

Used to notify user


SMS
Voicemail
Content Provider
Set of methods to let applications access content provider data

Used to allow multiple applications access data

Address book
Activity Life Cycle
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


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
Activity Example
package test.test.a1;

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;
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);
• }
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();
• }
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);
}

}
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;

• public class HelloAndroid extends Activity {


• /** Called when the activity is first created. */
• @Override
• public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
• }
• }
src/com.android/hello2/R.java
package package.name;
//Autogenerated
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int icon=0x7f020000;
}
public static final class layout {
public static final int main=0x7f030000;
}
public static final class string {
public static final int app_name=0x7f040001;
public static final int hello=0x7f040000;
}
}
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>
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>
</resources>

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

Widget class docs contain links to classes attributes

http://code.google.com/android/reference/android/R.styleable.html#View contains list of attributes for View class


Logging Example
package sdsu.cs696;

import ndroid.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class HelloAndroid extends Activity {


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{ super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.i("test","me");
}
Viewing the Log
Start the emulator

Run your program from eclipse

In eclipse select DDMS perspective


Use Filters
31

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;

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");
}
}
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>

@ 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

public class HelloAndroid extends Activity implements View.OnClickListener {


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");
messageText.setOnClickListener(this);
}

public void onClick(View v) {


messageText.setText(messageText.getText() + " click");
}
}
Listeners
GestureDetector.OnGestureListener Notify when gestures occur
MenuItem.OnMenuItemClickListener a menu item is clicked.
View.OnClickListener a view is clicked.
View.OnCreateContextMenuListener the context menu for this view is being built.
View.OnFocusChangeListener the focus state of a view changed.
View.OnKeyListener a key event is dispatched to this view.
View.OnLongClickListener a view has been clicked and held.
View.OnTouchListener a touch event is dispatched to this view.
ViewGroup.OnHierarchyChangeListener the hierarchy within this view changed.
ViewStub.OnInflateListener ViewStub has successfully inflated its layout resource.
ViewTreeObserver.OnGlobalFocusChangeListener the focus state within the view tree changes.
ViewTreeObserver.OnGlobalLayoutListener the global layout state or the visibility of views within
the view tree changes.
ViewTreeObserver.OnPreDrawListener the view tree is about to be drawn.
ViewTreeObserver.OnTouchModeChangeListener the touch mode changes.

See View overview at: http://code.google.com/android/reference/android/view/package-summary.html for more information


and links to each listener
import android.app.Activity;
import android.os.Bundle; Button Example
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class HelloAndroid extends Activity implements View.OnClickListener {


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");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(this);
}

public void onClick(View v) {


messageText.setText(messageText.getText() + " click");
}
}
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>

@string/hello = use the string resource hello


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>
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;

public void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
setContentView(R.layout.main);
messageText = (EditText) this.findViewById(R.id.message);
messageText.setText("From onCreate");
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(this);
}
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);
}
}
Menus Cont.

public boolean onPrepareOptionsMenu(Menu menu)


{ super.onPrepareOptionsMenu(menu);
}
}

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>

<string name="menu_dad">Hi Dad</string>


<string name="menu_mom">Hi Mom</string>

</resources>
Display

You might also like