Developing MyQuiz Apps Part 3
Using ADT 18 On Win 7
CREATING LISTVIEW MENU
0) Continue from previous tutorial
or download startup file
1) Prepare the string values to be used in this App.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string
name="app_name">MyQuiz</string>
<string
name="help">HELP</string>
<string
name="settings">SETTINGS</string>
<string
name="game">GAME</string>
<string
name="scores">SCORES</string>
<string
name="menu">MENU</string>
<string
name="menu_item_settings">Settings</string>
<string
name="menu_item_play">Play Game</string>
<string
name="menu_item_scores">View Scores</string>
<string
name="menu_item_help">Help</string>
</resources>
|
File name: MyQuiz/res/values/strings.xml
2) Prepare Layout files to be used in this App
2.1) The menu item file
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:shadowRadius="5"
android:gravity="center"
android:shadowDy="3"
android:shadowDx="3"
android:textSize="32sp"
/>
|
File name: MyQuiz/res/layout/menu_item.xml
2.2) The menu file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@drawable/bckgrnd">
<ListView
android:layout_height="wrap_content"
android:id="@+id/ListView_Menu"
android:layout_width="match_parent"
android:layout_centerHorizontal="true">
</ListView>
</LinearLayout>
|
3) Edit QuizMenuActivity.java
Replace the content of QuizMenuActivity.java with the following codes.
package com.example.myquiz;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
public class QuizMenuActivity extends QuizActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
ListView menuList = (ListView) findViewById(R.id.ListView_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.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))) {
// Launch the Game Activity
startActivity(new Intent(QuizMenuActivity.this, QuizGameActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_help))) {
// Launch the Help Activity
startActivity(new Intent(QuizMenuActivity.this, QuizHelpActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_settings))) {
// Launch the Settings Activity
startActivity(new Intent(QuizMenuActivity.this, QuizSettingsActivity.class));
} else if (strText.equalsIgnoreCase(getResources().getString(R.string.menu_item_scores))) {
// Launch the Scores Activity
startActivity(new Intent(QuizMenuActivity.this, QuizScoresActivity.class));
}
}
});
}
}
|
4) Check that all Activity Class have been specified in the Android Manifest.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myquiz"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.myquiz.QuizSplashActivity"
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="QuizMenuActivity"></activity>
<activity
android:name="QuizHelpActivity"></activity>
<activity
android:name="QuizScoresActivity"></activity>
<activity
android:name="QuizSettingsActivity"></activity>
<activity
android:name="QuizGameActivity"></activity>
</application>
</manifest>
|
5) Run.
REFERENCES
DOWNLOAD
---
No comments:
Post a Comment