13. Starting an Activity class
public class Memory extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.list_menu);
String[] items = { getResources().getString(R.string.menu_item_play),
getResources().getString(R.string.menu_item_scores),
getResources().getString(R.string.menu_item_settings),
getResources().getString(R.string.menu_item_help) };
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this,R.layout.menu_item, items);
menuList.setAdapter(adapt);
menuList.setSelection(-1);
13
14. Starting an Activity class
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id)
{
// Note: if the list was built "by hand" the id could be used.
// As-is, though, each item has the same id
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(
getResources().getString(R.string.menu_item_play))) {
startActivity(new Intent(Memory.this, MemoryPlayGame.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_help))) {
//
startActivity(new Intent(Memory.this, MemoryHelp.class));
}
}
});
}
}
14
15. Include the Activity in the
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.plearn" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".Memory" android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".PlayGame" android:label="@string/app_name">
</activity>
</application>
</manifest>
15
17. Using View to draw graphics
public class GraphicsExample extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new ViewGraphics(this));
}
class ViewGraphics extends View{
public ViewGraphics(Context context){
super(context);
}
public void onDraw(Canvas canvas){ // canvas to draw on
}
}
}
17
18. Paint contains drawing
properties
Paint mPaint=new Paint(); // Paint contains properties to use when drawing
Typeface mTypeFace;
public ViewGraphics(Context context){
super(context);
mPaint.setStrokeWidth(3);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setTextSize(30);
mPaint.setTypeface(Typeface.create(Typeface.SERIF,
Typeface.BOLD_ITALIC));
mFace = Typeface.createFromAsset(getContext().getAssets(),
"fonts/gigi.ttf");
//copy a font file in to Assets/fonts folder
}
18
21. The onTouchEvent
The onTouchEvent method occurred when the user touch the screen.
The (getX(), getY()) is the coordinate (x,y) axis where the screen is touched.
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
float x = event.getX();
float y = event.getY();
return true;
}
21
22. The PlayGame Activity
public class PlayGame extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
GameView gameView = new GameView(this);
setContentView(gameView);
}
}
22
23. The PlayGame Activity cont.
public class PlayGame extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
GameView gameView = new GameView(this);
setContentView(gameView);
}
}
class GameView extends View {
public GameView(Context context){
}
public void onDraw(Canvas canvas) {
}
public boolean onTouchEvent(MotionEvent event) {
}
}
23
24. Assignment 6
1. Implement the graphic primitives in the onDraw() method
of the PlayGame Activity.
2. Implement the TouchEvent method to show the
coordinate (x,y) using the Toast.makeText().
24