Lunes 31 enero 2011
Keyboarding
package cis493.keyboarding;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;
public class SimpleKeybording extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);// try main2, main3
} catch (Exception e) {
// TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),e.getMessage(), 1).show();
}
}
}
Main1.xml
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffcccccc"
android:orientation="vertical"
xmlns:android="
TextView
android:id="@+id/caption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:text="inputType: text|textCapWords"
android:textStyle="bold"
android:textSize="22sp" />
EditText
android:id="@+id/txtData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
android:textSize="18sp"
android:inputType="text|textCapWords" />
</LinearLayout
Main2.xml
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffcccccc"
android:orientation="vertical"
xmlns:android="
TextView
android:id="@+id/caption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:text="inputType: number|numberSigned|numberDecimal"
android:textStyle="bold"
android:textSize="22sp"
</TextView
EditText
android:id="@+id/txtData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
android:textSize="18sp"
android:inputType="number|numberSigned"
</EditText
</LinearLayout
Main3.xml
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayout
android:id="@+id/widget31"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffcccccc"
android:orientation="vertical"
xmlns:android="
TextView
android:id="@+id/caption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff0000ff"
android:textStyle="bold"
android:textSize="22sp"
android:text="inputType: date"
</TextView
EditText
android:id="@+id/txtData"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10px"
android:textSize="18sp"
android:inputType="date"
</EditText
</LinearLayout
TEXT WATCHER
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayoutxmlns:android="
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffaabbcc"
>
EditText
android:id="@+id/txtInput"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:padding="4px"
android:textStyle="bold"
/>
TextView
android:id="@+id/txtMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10px"
android:padding="4px"
android:background="#ff0000ff"
android:textStyle="bold"
/>
</LinearLayout
CODE
// demonstrate the use of a simple TEXTWATCHER control
package cis493.keyboarding;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;
publicclass TextWatcherDemo extends Activity {
EditText txtInput;
TextView txtMsg;
intkeyCount = 0;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
txtMsg = (TextView)findViewById(R.id.txtMsg);
txtInput = (EditText)findViewById(R.id.txtInput);
// ------
txtInput.addTextChangedListener(new TextWatcher() {
String msg;
publicvoid afterTextChanged(Editable arg0) {
msg = "count: " + txtInput.getText().toString().length()
+ " " + arg0.toString();
txtMsg.setText( msg );
}
publicvoid beforeTextChanged(CharSequence arg0, int arg1,
int arg2, int arg3) {
//Toast.makeText(getApplicationContext(), "BTC " + arg0, 1).show();
}
publicvoid onTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
//Toast.makeText(getApplicationContext(), "OTC " + arg0, 1).show();
}
}); //addTextChangedListener
// ------
}//textWatcher
}//class
FONTS
<?xmlversion="1.0"encoding="utf-8"?>
TableLayout
android:id="@+id/myTableLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:stretchColumns="1"
xmlns:android="
TableRow
android:id="@+id/myRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
TextView
android:id="@+id/labelSans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff006666"
android:layout_marginRight="24px"
android:text="sans:"
android:textSize="20sp"
</TextView
TextView
android:id="@+id/sans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, world!"
android:textSize="20sp"
android:typeface="sans"
</TextView
</TableRow
TableRow
android:id="@+id/myRow2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
TextView
android:id="@+id/labelSerif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="24px"
android:text="serif:"
android:textSize="20sp"
android:background="#ff006666"
</TextView
TextView
android:id="@+id/serif"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, world!"
android:textSize="20sp"
android:typeface="serif"
</TextView
</TableRow
TableRow
android:id="@+id/myRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
TextView
android:id="@+id/labelMonospace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="24px"
android:text="monospace:"
android:textSize="20sp"
android:background="#ff006666"
</TextView
TextView
android:id="@+id/monospace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, world!"
android:textSize="20sp"
android:typeface="monospace"
</TextView
</TableRow
TableRow
android:id="@+id/myRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
TextView
android:id="@+id/labelCustom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="24px"
android:text="custom:"
android:textSize="20sp"
android:background="#ff006666"
</TextView
TextView
android:id="@+id/custom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, world!"
android:textSize="20sp"
</TextView
</TableRow
</TableLayout
Code
// demonstrates basic use of FONTS
package cis493.androfontsdemo;
import android.app.Activity;
import android.graphics.Typeface;
import android.os.Bundle;
import android.widget.TextView;
publicclass AndroFontDemo extends Activity {
@Override
publicvoid onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// bind the "custom" textview with chosen font
TextView tvCustom=(TextView)findViewById(R.id.custom);
Typeface myNewFace=Typeface.createFromAsset(getAssets(),
"fonts/Jokerman.TTF");
tvCustom.setTypeface(myNewFace);
}
}
- WebView Simple URL
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayoutxmlns:android="
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
WebView
android:id="@+id/webkit"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout
package cis493.webviewdemo;
import android.os.Bundle;
import android.app.Activity;
import android.webkit.WebView;
public class WebView1 extends Activity {
WebView browser;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
browser = (WebView) findViewById(R.id.webkit);
browser.loadUrl("
//browser.loadUrl("
browser.getSettings().setJavaScriptEnabled(true);
}
}
2. WebView – Passing Objects to JS
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayout
xmlns:android="
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout
res/assets/my_local_page1.html
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<title>Android GeoWebOne Demo</title>
<script language="javascript">
function whereami() {
document.getElementById("lat").innerHTML=locater.getLatitude();
document.getElementById("lon").innerHTML=locater.getLongitude();
var spy = "Spy data coming from HTML\n"
+ document.getElementById("lat").innerHTML
+ "\n"
+ document.getElementById("lon").innerHTML;
locater.setValue(spy);
}
function talkBack2Android() {
var spy = "Spy data coming from HTML\n"
+ "\n"
+ document.getElementById("myText").value
+ "\n"
+ document.getElementById("lat").innerHTML
+ "\n"
+ document.getElementById("lon").innerHTML;
locater.htmlPassing2Android(spy);
}
</script>
</head>
<body>
<p>You are at</p>
<table border="1" cellspacing="1" cellpadding="1">
<tr>
<td bgcolor="#FFFFCC">Latitude</td>
<td<span id="lat">(unknown)</span</td>
</tr>
<tr>
<td bgcolor="#FFFFCC">Longitude</td>
<td<span id="lon">(unknown)</span</td>
</tr>
</table>
<p<a onClick="whereami()"<u>Click to Get Location</u</a</p>
<p> Enter some data here <input type="text" id="myText" />
<p> <input type="button" onclick= "talkBack2Android()" value="Talking to Android">
</body>
</html>
// WEBVIEW2
// A WebView control is used to expose a locally held html page.
// An Android object is exposed to JS so data can be exchanged.
package ucr.webkit3;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.webkit.WebView;
import android.widget.Toast;
public class Main extends Activity {
private WebView browser;
MyLocater locater = new MyLocater();
Location mostRecentLocation;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
// get a location fix (lat, lon)
mostRecentLocation = fakeGetLocation();
// set up the webview to show location results
browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.addJavascriptInterface(locater, "locater");
browser.loadUrl("file:///android_asset/my_local_page1.html");
}
private Location fakeGetLocation() {
// faking the obtaining of a location object (discussed later!)
Location fake = new Location("fake");
Random generator = new Random();
double latSign = 1;
if ( generator.nextDouble() <= 0.5) latSign = -1;
fake.setLatitude(generator.nextDouble() * 90 * latSign);
double lonSign = 1;
if ( generator.nextDouble() <= 0.5) lonSign = -1;
fake.setLongitude(generator.nextDouble() * 180 * lonSign);
return fake;
}
// //////////////////////////////////////////////////////////////////
// An object of type "MyLocater" will be used to pass data back and
// forth between the Android app and the JS code behing the html page.
public class MyLocater {
public String spy = "XXX";
public double getLatitude() {
if (mostRecentLocation == null){
return (0);
}
else {
return mostRecentLocation.getLatitude();
}
}
public double getLongitude() {
if (mostRecentLocation == null){
return (0);
}
else {
return mostRecentLocation.getLongitude();
}
}
public void htmlPassing2Android(String dataFromHtml) {
// changes to the HTML place-holders lat & lon can
// be seen here. There is an HTML button that
// when clicked calls this Android method.
spy = dataFromHtml;
Toast.makeText(getApplicationContext(), spy, 1).show();
}
}//MyLocater
}
3. WebView – Google Maps V3
<?xmlversion="1.0"encoding="utf-8"?>
LinearLayout
xmlns:android="
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
WebView
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout
res/assests/webview_map.html
<!DOCTYPE html
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0px; padding: 0px }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="
</script>
<script type="text/javascript">
function initialize() {
varlatlng = new google.maps.LatLng(9.938095, -84.054433);
var myOptions = {
zoom: 15,
center: latlng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
}
</script>
</head>
<body onload="initialize()">
<div id="map_canvas" style="width:100%; height:100%"</div>
</body>
</html>
Add to Manifest
uses-permissionandroid:name="android.permission.INTERNET"/>
uses-permissionandroid:name="android.permission.ACCESS_COARSE_LOCATION"/>
Code
// WebView3
// Loads a local html file on a WebView. The html file contains
// a JS script method to show a Google Map V3 centered around
// UCR San Jose CR (zoom level 13/23, centered, sensor=true)
// The image on the phone is the same that would be displayed
// by a browser in other device (laptop, iPhone,...)
// ------
package ucr.googlemaps;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
public class Main extends Activity {
WebView browser;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// connect browser to local html file defining mapping operation
browser = (WebView) findViewById(R.id.webview);
browser.getSettings().setJavaScriptEnabled(true);
browser.loadUrl("file:///android_assets/webview_map.html");
}
}
4. WebView – Google Maps V3 – Passing Object to JS (not show here)
// WEBVIEW4
// Uses Google Maps V3 JS scripting. A map is displayed on a WebView.
// Android code provides current device's location. This data is
// used to center a Google map on the given coordinates.
// (test with a real device)
Using Dialog Boxes
// create a simple dialog box (Yes/No/Cancel)
package cis493.selectionwidgets;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
publicclass AlertDialogDemo extends Activity {
Button btnGo;
EditText txtMsg;
String msg;
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtMsg = (EditText)findViewById(R.id.txtMsg);
btnGo = (Button) findViewById(R.id.btnGo);
btnGo.setOnClickListener(new OnClickListener() {
@Override
publicvoid onClick(View arg0) {
AlertDialog diaBox = makeDialogBox();
diaBox.show();
//WARNING: (in general...)
//after showing a dialog you should have NO more
//code. Let the buttons of the dialog box handle
//the rest of the logic. For instance, in this
//example a modal dialog box is displayed (once shown
//you can not do anything to the parent until the
//child is closed) however the code in the parent
//continues to execute after the show() method is
//called.
txtMsg.setText("I am here!");
}
});
}//onCreate
private AlertDialog makeDialogBox(){
AlertDialog myQuittingDialogBox =
new AlertDialog.Builder(this)
//set message, title, and icon
.setTitle("Terminator")
.setMessage("Are you sure that you want to quit?")
.setIcon(R.drawable.ic_menu_end_conversation)
//set three option buttons
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int whichButton) {
//whatever should be done when answering "YES" goes here
msg = "YES " + Integer.toString(whichButton);
txtMsg.setText(msg);
}
})//setPositiveButton
.setNeutralButton("Cancel",new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int whichButton) {
//whatever should be done when answering "CANCEL" goes here
msg = "CANCEL " + Integer.toString(whichButton);
txtMsg.setText(msg);
}//OnClick
})//setNeutralButton
.setNegativeButton("NO", new DialogInterface.OnClickListener() {
publicvoid onClick(DialogInterface dialog, int whichButton) {
//whatever should be done when answering "NO" goes here
msg = "NO " + Integer.toString(whichButton);
txtMsg.setText(msg);
}
})//setNegativeButton
.create();
return myQuittingDialogBox;
}
}//AndSelectionWidgets
main.xml
LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="
android:orientation="horizontal"
Button
android:text="GO"
android:id="@+id/btnGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5px"/>
EditText
android:hint="click the button"
android:id="@+id/txtMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5px" />
</LinearLayout
1