Friday, February 15, 2008
New Android SDK is released!
Thursday, February 14, 2008
A simple Google Android application Part II
main.xml
<Button android:id="@+id/MainButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="OK"
android:layout_x="10px" android:layout_y="102px">
</Button>
<Button android:id="@+id/MainButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Cancel"
android:layout_x="80px" android:layout_y="102px">
</Button>
queen.xml:
<Button android:id="@+id/QueenButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="OK"
android:layout_gravity="right">
</Button>
MainActivity.java
package csu.android.applications;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.text.method.*;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private EditText myEText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
myEText = (EditText) findViewById(R.id.MainEditText1);
myEText.setInputMethod(DigitsInputMethod.getInstance());
Button okButton = (Button) findViewById(R.id.MainButton1);
Button cancelButton = (Button) findViewById(R.id.MainButton2);
okButton.setOnClickListener(mOKListener);
cancelButton.setOnClickListener(mCancelListener);
}
private OnClickListener mOKListener = new OnClickListener() {
public void onClick(View v) {
String tmp = myEText.getText().toString();
if (!myEText.getText().toString().equals("")) {
int Qnum = Integer.parseInt(tmp);
if (Qnum <= 9) {
// Here we start up the main entry point
Intent intent = new Intent(MainActivity.this, Queen.class);
intent.putExtra("NumKey", tmp);
startActivity(intent);
finish();
} else {
showAlert("The number is bigger than 9!", 0, "Try again!",
"OK", true);
myEText.setText("");
return;
}
}
}
};
private OnClickListener mCancelListener = new OnClickListener() {
public void onClick(View v) {
myEText.setText("");
}
};
}
Queen.java
package csu.android.applications;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import java.util.*;
public class Queen extends Activity {
EditText myEText;
int QueenNum;
int totalcount = 0; // the total number of distinct results.
int uniquecount = 0; // the total number of unique results.
boolean[][] arr;
// Record the scores for mapping
ArrayList
// For check the duplicate solutions
int[][] arrRotation0;
int[][] arrRotation90;
int[][] arrRotation180;
int[][] arrRotation270;
int[][] arrMirror;
private StringBuilder sb;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.queen);
myEText = (EditText) findViewById(R.id.QueenEditText1);
Button backButton = (Button) findViewById(R.id.QueenButton1);
backButton.setOnClickListener(this.mBackListener);
Bundle extras = getIntent().getExtras();
if (extras != null) {
QueenNum = Integer.parseInt(extras.getString("NumKey"));
startCalculation();
myEText.setText(sb.toString());
} else {
showAlert("There is no input!", 0, "Click me", "OK", true);
Intent intent = new Intent(Queen.this, MainActivity.class);
startActivity(intent);
finish();
}
}
private OnClickListener mBackListener = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Queen.this, MainActivity.class);
startActivity(intent);
finish();
}
};
/*
* startCalculation This method implements recursion for N*N Queen problem.
*/
public void startCalculation() {
sb = new StringBuilder();
// Array for N*N Queens problem
arr = new boolean[QueenNum][QueenNum];
// Array for calculating the duplicate result.
arrRotation0 = new int[QueenNum][QueenNum];
arrRotation90 = new int[QueenNum][QueenNum];
arrRotation180 = new int[QueenNum][QueenNum];
arrRotation270 = new int[QueenNum][QueenNum];
arrMirror = new int[QueenNum][QueenNum];
al = new ArrayList
// Prepare random numbers inserted into arrays
// for the calculation of the duplication .
prepareRotationData();
// Do recursions
putQtoArray(QueenNum - 1);
}
/*
* putQtoArray This method implements recursion for N*N Queen problem.
* @param: QueenNum
*/
public void putQtoArray(int myNum) {
if (myNum == -1) {
// Find a distinct result.
totalcount++;
// Display the result.
showMe();
return;
}
for (int i = 0; i <>
if (!checkRule(i, myNum)) {
arr[i][myNum] = true;
// Recursion
putQtoArray(myNum - 1);
arr[i][myNum] = false;
}
}
}
}
Because Activity "Queen" will be started by Activity "MainActivity" using Intent, we also need to add a line into AndroidManifest.xml as below, then that is all set. <activity android:name="Queen" android:label="@string/app_name">
A simple Google Android application Part I
Basically, Android includes Linux kernel as operating system, libraries ( C language), Android runtime ( Dalvik virtual machine for Java byte code), and application framework (APIs for Java) . The applications we wanna develop are based on this application framework and Java 1.5 language specification, and will run on Android runtime.
Until now, Google only has prototype cell phone for running Android platform. Due to this reason, all the applications need to execute on the emulator which emulates the same environment just like you really have the hardware. There is a simple example that can give an idea about developing a Android application.
Then it will calculate the possibility of results showing as a simple checker.
P.S: It runs on the Android emulator.
Friday, February 8, 2008
[Android] DroidDraw
All the things need to do with XML. Fortunately, there is a tool that is very useful to it.
It also provides tutorials that developers can quickly understand how to use it.
http://www.droiddraw.org/