34
Activity Michael Pan

Activity

Embed Size (px)

DESCRIPTION

Android App Dev lesson serial - Activity

Citation preview

Page 1: Activity

ActivityMichael Pan

Page 2: Activity

The project

Page 3: Activity

Blank Activity

Page 4: Activity

Layout - activity_record.xml

Page 5: Activity

Project View

Page 6: Activity

AndroidManifest.xmlDailyRecord > app > src > main > AndroidManifest.xml

Page 7: Activity

Application attributes<?xml version="1.0" encoding="utf-8"?>!<manifest xmlns:android="http://schemas.android.com/apk/res/android"! package="com.zencher.dailyrecord.app" >!! <application! android:allowBackup="true" ! android:icon="@drawable/ic_launcher"! android:label="@string/app_name"! android:theme="@style/AppTheme" >! <activity! android:name="com.zencher.dailyrecord.app.RecordActivity"! android:label="@string/app_name" >! <intent-filter>! <action android:name="android.intent.action.MAIN" />!! <category android:name="android.intent.category.LAUNCHER" />! </intent-filter>! </activity>! </application>!!</manifest>

http://bit.ly/1msJ804

Backup Appimage

Page 8: Activity

Change icon

@drawable/ic_launcher

app > src > main > res > drawable-xxxx

Page 9: Activity

Run - Change Icon

Page 10: Activity

Application attributes - android:label<?xml version="1.0" encoding="utf-8"?>!<manifest xmlns:android="http://schemas.android.com/apk/res/android"! package="com.zencher.dailyrecord.app" >!! <application! android:allowBackup="true" ! android:icon="@drawable/ic_launcher"! android:label="@string/app_name"! android:theme="@style/AppTheme" >! <activity! android:name="com.zencher.dailyrecord.app.RecordActivity"! android:label="@string/app_name" >! <intent-filter>! <action android:name="android.intent.action.MAIN" />!! <category android:name="android.intent.category.LAUNCHER" />! </intent-filter>! </activity>! </application>!!</manifest>

Page 11: Activity

Another resource

@string/app_name

app > src > main > res > values > strings.xml

Page 12: Activity

Tag - activity<?xml version="1.0" encoding="utf-8"?>!<manifest xmlns:android="http://schemas.android.com/apk/res/android"! package="com.zencher.dailyrecord.app" >!! <application! android:allowBackup="true" ! android:icon="@drawable/ic_launcher"! android:label="@string/app_name"! android:theme="@style/AppTheme" >! <activity! android:name="com.zencher.dailyrecord.app.RecordActivity"! android:label="@string/app_name" >! <intent-filter>! <action android:name="android.intent.action.MAIN" />!! <category android:name="android.intent.category.LAUNCHER" />! </intent-filter>! </activity>! </application>!!</manifest>

Class name

Page 13: Activity

activity - android:label<?xml version="1.0" encoding="utf-8"?>!<manifest xmlns:android="http://schemas.android.com/apk/res/android"! package="com.zencher.dailyrecord.app" >!! <application! android:allowBackup="true" ! android:icon="@drawable/ic_launcher"! android:label="@string/app_name"! android:theme="@style/AppTheme" >! <activity! android:name="com.zencher.dailyrecord.app.RecordActivity"! android:label="@string/app_name" >! <intent-filter>! <action android:name="android.intent.action.MAIN" />!! <category android:name="android.intent.category.LAUNCHER" />! </intent-filter>! </activity>! </application>!!</manifest>

Page 14: Activity

Create a new string tag

Page 15: Activity

Run @string/main_activity_label

Page 16: Activity

intent-filter<?xml version="1.0" encoding="utf-8"?>!<manifest xmlns:android="http://schemas.android.com/apk/res/android"! package="com.zencher.dailyrecord.app" >!! <application! android:allowBackup="true" ! android:icon="@drawable/ic_launcher"! android:label="@string/app_name"! android:theme="@style/AppTheme" >! <activity! android:name="com.zencher.dailyrecord.app.RecordActivity"! android:label="@string/app_name" >! <intent-filter>! <action android:name="android.intent.action.MAIN" />!! <category android:name="android.intent.category.LAUNCHER" />! </intent-filter>! </activity>! </application>!!</manifest>

Page 17: Activity

intentMessage among activities

intent-filter

filter intent event

must have a action

android.intent.action.MAIN - the initial activity

category

android.intent.category.LAUNCHER - Should be displayed in the top-level launcher

Page 18: Activity

Add some Control

EditText

Button

Page 19: Activity

activity_record.xml in layoutapp > src > main > res > layout > activity_record.xml

<RelativeLayout >!!

<TextView />!!

<EditText />!!

<Button />!!

</RelativeLayout>

Page 20: Activity

Design & Text - layout xml

Design Text

Page 21: Activity

RecordActivity.javaapp > src > main > java > (domain) > RecordActivity.java

public class RecordActivity extends Activity {!!

@Override! protected void onCreate(Bundle savedInstanceState) {! super.onCreate(savedInstanceState);! setContentView(R.layout.activity_record);! } !! ! // ignored… !}

Page 22: Activity

R

Auto generated Class

files in res/ will be the field in R

app > build > source > r > debug > (domain) > R

Page 23: Activity

layout xml in R

Page 24: Activity

Compiling processres/ AndroidManifest.xml R.java

Asset Packaging Tool

(aapt)

src/ RecordActivity.java

Compile Java

Java bytecode .classCross Compile

to DalvikDalvik bytecode

.dexCompiled Resource

Build & Sign apk

Android Package .apk

Install & Run

Page 25: Activity

setContentView(R.layout.activity_record);<RelativeLayout >!!

<TextView />!!

<EditText />!!

<Button />!!

</RelativeLayout>

ClassLoader.loadClass(“RelativeLayout”) RelativeLayout

ClassLoader.loadClass(“TextView”) TextView

EditText

Button

ClassLoader.loadClass(“EditText”)

ClassLoader.loadClass(“Button”)

Page 26: Activity

Run

Page 27: Activity

Get instance into variable

Page 28: Activity

import classoption + Enter

Page 29: Activity

EditText & Button

Page 30: Activity

Get instance in ActivityfindViewById()

protected void onCreate(Bundle savedInstanceState) {! super.onCreate(savedInstanceState);! setContentView(R.layout.activity_record);! mTopTextView = (TextView) findViewById(R.id.textView);!}

activity_record.xml

Page 31: Activity

Apply into three variables

Page 32: Activity

OnClickListener

Page 33: Activity

View.OnClickListenernew View.OnClickListener() {!! @Override!! public void onClick(View v) {!! ! mTopTextView.setText(mInputView.getText());!! }!}

Page 34: Activity

Question