15
1 Introducing Introducing Activity and Activity and Intent Intent

1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

Embed Size (px)

Citation preview

Page 1: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

1

Introducing Introducing Activity and Activity and

IntentIntent

Page 2: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

2

Memory Memory

LinearLayout,

weight=2

LinearLayout,

weight=1

TextView

ListView

Page 3: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

3

How to create xml fileHow to create xml file

Right click (on

the folder)

Page 4: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

4

The menu.xmlThe menu.xml<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical" android:layout_width="fill_parent“ android:layout_height="fill_parent">

<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"

android:layout_weight="2">

<TextView android:id="@+id/TextView01" android:layout_width="wrap_content” android:layout_height="wrap_content“

android:textSize="@dimen/screen_title_size"

android:text="@string/menu" android:layout_gravity="center"

android:shadowColor="@android:color/white"

android:textColor="@color/title_color" />

</LinearLayout>

Page 5: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

5

The menu.xml cont.The menu.xml cont.<LinearLayout android:orientation="vertical"

android:layout_width="fill_parent" android:layout_height="fill_parent“ android:layout_weight="1">

<ListView android:layout_height="wrap_content" android:id="@+id/list_menu"

android:layout_width="fill_parent" android:layout_gravity="center_horizontal"

android:divider="@drawable/divider" android:listSelector="@drawable/textured">

</ListView></LinearLayout>

</LinearLayout>

Page 6: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

6

The dimens.xml (in values The dimens.xml (in values folder)folder)

<?xml version="1.0" encoding="utf-8"?><resources> <dimen name="logo_size">24pt</dimen> <dimen name="version_size">5pt</dimen> <dimen name="version_spacing">3pt</dimen> <dimen name="screen_title_size">16pt</dimen> <dimen name="menu_item_size">16pt</dimen> <dimen name="game_question_size">10pt</dimen> <dimen name="help_text_size">7pt</dimen> <dimen name="help_text_padding">20px</dimen></resources>

Page 7: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

7

The colors.xml (in values The colors.xml (in values folder)folder)

<?xml version="1.0" encoding="utf-8"?><resources> <color name="logo_color">#FFFF0F</color> <color name="version_color">#f0f0f0</color> <color name="version_bkgrd">#1a1a48</color> <color name="title_color">#f0f0f0</color> <color name="title_glow">#F00</color> <color name="menu_color">#FFFF0F</color> <color name="menu_glow">#F00</color> <color name="error_color">#F00</color></resources>

Page 8: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

8

How to create java fileHow to create java file

Right click (on

the folder)

Page 9: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

9

How to override a methodHow to override a method

Right click on the code

pane

Page 10: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

10

How to override a method How to override a method cont.cont.

Select the

method to

overridem eg,

onCreate

Page 11: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

11

The strings.xml (in values The strings.xml (in values folder)folder)

<Resources>….<string name="menu">Memory</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>

Page 12: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

12

Create ListView from Create ListView from resourceresource

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);

Page 13: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

13

Starting an Activity classStarting an Activity classpublic 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);

Page 14: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

14

Starting an Activity classStarting an Activity classmenuList.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));

}

}

});

}

}

Page 15: 1 Introducing Activity and Intent. 2 Memory LinearLayout, weight=2 LinearLayout, weight=1 TextView ListView

15

Include the Activity in the Include the Activity in the manifestmanifest

<?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>