Have contributed a little minesweeper game, have a look at: http://js1k.com/demo/512
I've updated the code snippets of this blog to the newest SDK of Android (1.0 R2).
Due to spamming attempts I decided to deactivate the trackbacks for this log!
Today I stumbled accross a little paper which explains some programming concepts of Android. It has been written by D. Guntz and J. Pleumann who were involved in the creation of core libraries of Android. So have a look at their site for details (paper + full source and binary).
Here are some resources where you can also find How-Tos as well as other interesting Android related stuff:
- anddev.org (How-Tos, Tutorials, Android Apps, Discussions, FAQ, ...)
- Android Discussion Groups (official discussion group site from Google)
- Common Tasks and How To Do Them in Android (Android online Documentation)
- The Android Log (Unofficial Log about Android, couple of Android related Links)
- Android Boards (posts Android news, and has other stuff)
jQuery is well known to the "Ajax-Community". To get familiar with this Javascript library and tools, I implemented the little game "Trap" (a minesweeper clone) using jQuery features.
Findings: Maybe I need further experience, but for now jQuery reminds me of ancient times in which some self-styled programming gurus wrote single lines of code in C that nobody else could read....
Anyway: here is the result of my implementation and exploration efforts, feel free to play, copy, re-use and comment.
Findings: Maybe I need further experience, but for now jQuery reminds me of ancient times in which some self-styled programming gurus wrote single lines of code in C that nobody else could read....
Anyway: here is the result of my implementation and exploration efforts, feel free to play, copy, re-use and comment.
[NOTE] Updated on 2009-01-17: Adopted Samples to final release of Android SDK (1.0 R2)
An Android user interface is created by specifying a layout XML usually, but sometimes you need to create your interfaces or parts of it "on-the-fly" in your programm code. This is not very easy sometimes because of the lack of documentation. The following code snippets show you how I could create a Spinner and a ListView widget programmatically:
...
public class Sample extends Activity
...
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
...
//Creating a spinner
Spinner spinner = new Spinner(this);
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
new String[] { "Apple", "Peach", "Banana" });
spinner.setAdapter(spinnerArrayAdapter);
//Add spinner to this activity's view (a LinearLayout)
mainLayout.addView(spinner, new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
...
}
}
...
public class Sample extends Activity
...
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
...
//Creating a ListView with Context Menu
ListView listView = new ListView(this);
ArrayAdapter listViewArrayAdapter =
new ArrayAdapter(this,
android.R.layout.simple_list_item_1, new String[] {
"Apple", "Peach","Banane" });
listView.setAdapter(listViewArrayAdapter);
listView.setFocusableInTouchMode(true);
listView.setOnFocusChangeListener(
new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View arg0, boolean arg1) {
Log.i("SampleApp", "onFocusChanged() - view=" + arg0);
}
});
listView.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView adapterView, View view,
int arg2, long arg3) {
int selectedPosition = adapterView.getSelectedItemPosition();
Log.i("SampleApp", "Click on position"+selectedPosition);
}
});
listView
.setOnCreateContextMenuListener(
new View.OnCreateContextMenuListener() {
public void onCreateContextMenu(ContextMenu menu, View view,
ContextMenu.ContextMenuInfo menuInfo) {
AdapterContextMenuInfo mi =
(AdapterContextMenuInfo) menuInfo;
menu.add(0, 0, 0, "Context-Menu-Entry");
}
});
...
}
}
Here's the code to invoke a phone call for a given number:
try {
Intent intent = new Intent(Intent.CALL_ACTION);
intent.setData(Uri.parse("tel:+436641234567"));
startActivity(intent);
} catch (Exception e) {
Log.e("SampleApp", "Failed to invoke call", e);
}
[NOTE] Updated on 2009-03-11: Adopted sample to release of Android SDK (1.1 R1)
It took me some time to figure out how contacts are created and stored in Android. There were a couple of postings (see Android Google Groups) I had to consult, but finally I did it :-). In case you experience the same difficulties here is the full picture (source code) that worked for me:
ContentValues personValues = new ContentValues(); personValues.put(Contacts.People.NAME, "John F. Doe"); /* STARRED 0 = Contacts, 1 = Favorites */ personValues.put(Contacts.People.STARRED, 1);// worked in SDK 1.0 R2 but not in SDK 1.1 R1 anymore //Uri newPersonUri = getContentResolver() // .insert(Contacts.People.CONTENT_URI, personValues);Uri newPersonUri = Contacts.People .createPersonInMyContactsGroup(getContentResolver(), personValues); if (newPersonUri != null) {// add company (organisation)ContentValues organisationValues = new ContentValues(); Uri orgUri = Uri.withAppendedPath(newPersonUri, Contacts.Organizations.CONTENT_DIRECTORY); organisationValues.put(Contacts.Organizations.COMPANY, "MyCompany Inc."); organisationValues.put(Contacts.Organizations.TYPE, Contacts.Organizations.TYPE_WORK); Uri orgUpdate = getContentResolver() .insert(orgUri, organisationValues); if (orgUpdate == null) { throw new Exception("Failed to insert organisation"); }// add mobile phone numberContentValues mobileValues = new ContentValues(); Uri mobileUri = Uri.withAppendedPath(newPersonUri, Contacts.People.Phones.CONTENT_DIRECTORY); mobileValues.put(Contacts.Phones.NUMBER, "(660) 111-1111"); mobileValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_MOBILE); Uri phoneUpdate = getContentResolver() .insert(mobileUri, mobileValues); if (phoneUpdate == null) { throw new Exception( "Failed to insert mobile phone number"); }// add fax numberContentValues faxValues = new ContentValues(); Uri faxUri = Uri.withAppendedPath(newPersonUri, Contacts.People.Phones .CONTENT_DIRECTORY); faxValues.put(Contacts.Phones.NUMBER, "(408) 111-1111-1"); faxValues.put(Contacts.Phones.TYPE, Contacts.Phones.TYPE_FAX_WORK); phoneUpdate = getContentResolver() .insert(faxUri, faxValues); if (phoneUpdate == null) { throw new Exception( "Failed to insert work fax number"); }// add emailContentValues emailValues = new ContentValues(); Uri emailUri = Uri .withAppendedPath( newPersonUri, Contacts.People.ContactMethods .CONTENT_DIRECTORY); emailValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_EMAIL); emailValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_HOME); emailValues.put(Contacts.ContactMethods.DATA, "john.f.doe@exit.com"); Uri emailUpdate = getContentResolver() .insert(emailUri, emailValues); if (emailUpdate == null) { throw new Exception("Failed to insert email"); }// add addressContentValues addressValues = new ContentValues(); Uri addressUri = Uri .withAppendedPath( newPersonUri, Contacts.People.ContactMethods .CONTENT_DIRECTORY); addressValues.put(Contacts.ContactMethods.KIND, Contacts.KIND_POSTAL); addressValues.put(Contacts.ContactMethods.TYPE, Contacts.ContactMethods.TYPE_HOME); addressValues.put(Contacts.ContactMethods.DATA, "Baker Street 14\n54123 New Hampshire"); Uri addressUpdate = getContentResolver().insert(addressUri, addressValues); if (addressUpdate == null) { throw new Exception("Failed to insert address"); }
[Note] Updated: Adopted code to Android SDK 1.0 R2
To become familiar with mobile programming plattforms I've implemented a little game (a minesweeper clone) in J2ME. After having finished this I ported this game to Android.
While porting to Android I explored the following features:
- Rendering images via View.onDraw()
- Working with multiple Activities (Main Activity, Options Activity)
- Passing data from one Activity to another
- Handling Activity states (freeze, restore)
- Handling custom menues (onCreateOptionsMenu)
- etc.
Disclaimer: Please note, that my intention was to explore some features of the plattforms. There is still room for improvement as I've not implemented a timer, a highscore table etc..
Attached you can find the Eclipse (3.4) projects of both implementations: The following sources are extracts from the Android implementation:
TrapMain.java
package at.lacherstorfer.trap.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class TrapMain extends Activity {
private TrapView trapView;
private static int EDIT_OPTIONS = 1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.trap_main_layout);
trapView = (TrapView) findViewById(R.id.trap);
if (icicle != null) {
// We are being restored
Bundle map = icicle.getBundle("trapView");
if (map != null) {
trapView.restoreState(map);
}
}
trapView.doStart();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// store game state
outState.putBundle("trapView", trapView.saveState());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem menuItem = menu.add(0, 0, 0, "Start");
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
trapView.doStart();
return true;
}
});
menuItem = menu.add(0, 0, 0, "Options");
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
Intent optionsIntent = new Intent(TrapMain.this, TrapOptions.class);
Bundle extras = new Bundle();
extras.putInt("numberRows", trapView.getNumberRows());
extras.putInt("numberCols", trapView.getNumberCols());
extras.putInt("numberTraps", trapView.getNumberTraps());
optionsIntent.putExtras(extras);
startActivityForResult(optionsIntent, EDIT_OPTIONS);
return true;
}
});
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
trapView.doStart(data.getIntExtra("numberRows", 8),
data.getIntExtra("numberCols", 8), data.getIntExtra("numberTraps", 8));
}
}
}
TrapView.java
package at.lacherstorfer.trap.android;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import at.lacherstorfer.trap.shared.Cell;
import at.lacherstorfer.trap.shared.Field;
public class TrapView extends View {
private Field field;
private int numberRows = 8;
private int numberCols = 8;
private int numberTraps = 8;
private int cursorX;
private int cursorY;
private Drawable cellClosedImage;
private Drawable cell0Image;
private Drawable cell1Image;
private Drawable cell2Image;
private Drawable cell3Image;
private Drawable cell4Image;
private Drawable cellBombImage;
private Drawable cellExplodedImage;
private Drawable cellFlaggedImage;
private Drawable cursorImage;
public TrapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
loadImages(context);
}
public TrapView(Context context, AttributeSet attrs) {
super(context, attrs);
loadImages(context);
}
public TrapView(Context context) {
super(context);
loadImages(context);
}
public void loadImages(Context context) {
cellClosedImage = context.getResources().getDrawable(
R.drawable.cellclosed);
cell0Image = context.getResources().getDrawable(R.drawable.cell0);
cell1Image = context.getResources().getDrawable(R.drawable.cell1);
cell2Image = context.getResources().getDrawable(R.drawable.cell2);
cell3Image = context.getResources().getDrawable(R.drawable.cell3);
cell4Image = context.getResources().getDrawable(R.drawable.cell4);
cellBombImage = context.getResources().getDrawable(R.drawable.cellbomb);
cellExplodedImage = context.getResources().getDrawable(
R.drawable.cellexploded);
cellFlaggedImage = context.getResources().getDrawable(
R.drawable.cellflagged);
cursorImage = context.getResources().getDrawable(R.drawable.cursor);
setFocusable(true);
}
public Bundle saveState() {
Bundle map = new Bundle();
map.putInt("numberRows", Integer.valueOf(numberRows));
map.putInt("numberCols", Integer.valueOf(numberCols));
map.putInt("numberTraps", Integer.valueOf(numberTraps));
map.putInt("cursorX", Integer.valueOf(cursorX));
map.putInt("cursorY", Integer.valueOf(cursorY));
int[] fieldState = field.getState();
for (int i = 0; i < fieldState.length; i++) {
map.putInt("field-" + i, fieldState[i]);
}
return map;
}
public void restoreState(Bundle icicle) {
numberRows = icicle.getInt("numberRows");
numberCols = icicle.getInt("numberCols");
numberTraps = icicle.getInt("numberTraps");
cursorX = icicle.getInt("cursorX");
cursorY = icicle.getInt("cursorY");
int[] fieldState = new int[icicle.size() - 5];
for (int i = 0; i < fieldState.length; i++) {
fieldState[i] = icicle.getInt("field-" + i);
}
field = new Field(fieldState);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
Log.i("TrapView", "pressed key=" + keyCode);
boolean handled = false;
if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
cursorX -= cursorX > 0 ? 1 : 0;
handled = true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
cursorX += cursorX < numberCols - 1 ? 1 : 0;
handled = true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
cursorY -= cursorY > 0 ? 1 : 0;
handled = true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) {
cursorY += cursorY < numberRows - 1 ? 1 : 0;
handled = true;
} else if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER
|| keyCode == KeyEvent.KEYCODE_ENTER) {
fire();
handled = true;
} else if (keyCode == KeyEvent.KEYCODE_SPACE) {
flag();
handled = true;
}
if (handled) {
postInvalidate();
}
return handled;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
boolean handled = false;
if (event.getAction() == MotionEvent.ACTION_DOWN) {
cursorX = (int) (event.getX() / 15);
cursorX = cursorX > numberCols ? numberCols - 1 : cursorX;
cursorY = (int) (event.getY() / 15);
cursorY = cursorY > numberRows ? numberRows - 1 : cursorY;
handled = true;
}
postInvalidate();
return handled;
}
private void fire() {
if (field == null)
return; // game not started yet
int cellValue = field.fireAt(cursorY, cursorX);
if (cellValue == Cell.TRAP) {
field.reveal();
Builder b = new AlertDialog.Builder(this.getContext());
b.setTitle("Trap");
b.setIcon(0);
b.setMessage("Game Over");
b.show();
} else if (field.isSolved()) {
Builder b = new AlertDialog.Builder(this.getContext());
b.setTitle("Trap");
b.setIcon(0);
b.setMessage("Congratulation, You Win!");
b.show();
}
}
private void flag() {
if (field == null)
return; // game not started yet
field.flagAt(cursorY, cursorX);
if (field.isSolved()) {
Builder b = new AlertDialog.Builder(this.getContext());
b.setTitle("Trap");
b.setIcon(0);
b.setMessage("Congratulation, You Win!");
b.show();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (field == null)
return; // game not started yet
// draw the field
for (int row = 0; row < numberRows; row++) {
for (int col = 0; col < numberCols; col++) {
Cell cell = field.getCellAt(row, col);
Drawable cellImage = getDrawable(cell);
cellImage.setBounds(/* left */col * 15, /* top */row * 15, /* right */
col * 15 + 15, /* bottom */row * 15 + 15);
cellImage.draw(canvas);
}
}
// draw the cursor
cursorImage.setBounds(cursorX * 15, cursorY * 15, cursorX * 15 + 15,
cursorY * 15 + 15);
cursorImage.draw(canvas);
}
public void doStart(int numberRows, int numberCols, int numberTraps) {
this.numberRows = numberRows;
this.numberCols = numberCols;
this.numberTraps = numberTraps;
field = new Field(numberRows, numberCols, numberTraps);
postInvalidate();
}
public void doStart() {
if (field == null) {
field = new Field(numberRows, numberCols, numberTraps);
} else {
field.restart();
}
postInvalidate();
}
private Drawable getDrawable(Cell cell) {
if (cell.isOpen()) {
switch (cell.getValue()) {
case Cell.TRAP:
return cellBombImage;
case 0:
return cell0Image;
case 1:
return cell1Image;
case 2:
return cell2Image;
case 3:
return cell3Image;
case 4:
return cell4Image;
}
} else if (cell.isClosed()) {
return cellClosedImage;
} else if (cell.isFlagged()) {
return cellFlaggedImage;
} else if (cell.isExploded()) {
return cellExplodedImage;
} else {
throw new RuntimeException("Internal Error");
}
return null;
}
public int getNumberRows() {
return numberRows;
}
public int getNumberCols() {
return numberCols;
}
public int getNumberTraps() {
return numberTraps;
}
}
TrapOptions.java
package at.lacherstorfer.trap.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
public class TrapOptions extends Activity {
private EditText etNumberCols;
private EditText etNumberRows;
private EditText etNumberTraps;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.trap_options_layout);
etNumberRows = (EditText) findViewById(R.id.number_rows);
etNumberCols = (EditText) findViewById(R.id.number_cols);
etNumberTraps = (EditText) findViewById(R.id.number_traps);
Bundle extras = getIntent().getExtras();
etNumberRows.setText(""+extras.getInt("numberRows"));
etNumberCols.setText(""+extras.getInt("numberCols"));
etNumberTraps.setText(""+extras.getInt("numberTraps"));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
MenuItem menuItem = menu.add(0, 0, 0, "Ok");
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
int numberRows = Integer.parseInt(etNumberRows.getText().toString());
int numberCols = Integer.parseInt(etNumberCols.getText().toString());
int numberTraps = Integer.parseInt(etNumberTraps.getText().toString());
Bundle b = new Bundle();
b.putInt("numberRows", numberRows);
b.putInt("numberCols", numberCols);
b.putInt("numberTraps", numberTraps);
Intent resultIntent = new Intent();
resultIntent.putExtras(b);
setResult(RESULT_OK, resultIntent);
finish();
return true;
}
});
menuItem = menu.add(0, 0, 0, "Cancel");
menuItem.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
setResult(RESULT_CANCELED);
finish();
return true;
}
});
return true;
}
}
trap_main_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<at.lacherstorfer.trap.android.TrapView
android:id="@+id/trap"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</FrameLayout>
trap_options_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number Columns"/>
<EditText android:id="@+id/number_cols"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="2" />
</TableRow>
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number Rows"/>
<EditText android:id="@+id/number_rows"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLength="2" />
</TableRow>
<TableRow>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number Traps"/>
<EditText android:id="@+id/number_traps"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:maxLength="2"/>
</TableRow>
</TableLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="at.lacherstorfer.trap.android">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name="TrapMain" android:label="@string/app_name">
<intent-filter>
<action android:value="android.intent.action.MAIN"
android:name="android.intent.action.MAIN"/>
<category android:value="android.intent.category.LAUNCHER"
android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="TrapOptions"
android:label="@string/app_name"/>
</application>
</manifest>



Recent Comments