Activity

Post on 17-May-2015

245 views 0 download

Tags:

description

Android App Dev lesson serial - Activity

Transcript of Activity

ActivityMichael Pan

The project

Blank Activity

Layout - activity_record.xml

Project View

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

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

Change icon

@drawable/ic_launcher

app > src > main > res > drawable-xxxx

Run - Change Icon

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>

Another resource

@string/app_name

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

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

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>

Create a new string tag

Run @string/main_activity_label

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>

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

Add some Control

EditText

Button

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

<RelativeLayout >!!

<TextView />!!

<EditText />!!

<Button />!!

</RelativeLayout>

Design & Text - layout xml

Design Text

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… !}

R

Auto generated Class

files in res/ will be the field in R

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

layout xml in R

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

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

Run

Get instance into variable

import classoption + Enter

EditText & Button

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

Apply into three variables

OnClickListener

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

Question