android app development 02 : activity & layout

35
Activity & Layout Anuchit Chalothorn [email protected] Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. 2

Upload: anuchit-chalothorn

Post on 01-Sep-2014

501 views

Category:

Documents


3 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Android App Development 02 : Activity & Layout

Activity & LayoutAnuchit [email protected]

Licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.

2

Page 2: Android App Development 02 : Activity & Layout

Quote

การทําอะไรใหถ้งึขนัเทพนัน ไมเ่คยงา่ย มแีตค่วามมุง่มนั อดทน อยา่งยาวนานเทา่นัน ทจีะทําใหบ้รรลไุด ้

วถิแีหง่เทพนันยอ่มเต็มไปดว้ยความยากลําบากเสมอ อนัเทพแหง่ศาสตรใ์ด เรมิตน้ตอ้งมคีวามรัก

อยา่งงมงายในศาสตรนั์น มคีวามพากเพยีรในการศกึษาจนแตกฉาน ฝึกหดัอยา่งอดทน

หมนัพจิารณาแกไ้ขใหศ้าสตรแ์หง่ตนยา่งเขา้สูค่วามสมบรูณ์ อจัฉรยิภาพแคช่ว่ยลดเวลาแหง่ความยากลําบากใหส้นัลงเทา่นัน แตห่ากปราศจากความมุง่มนัแลว้ ตอ่ใหม้อีจัฉรยิภาพสกัเพยีงไหน

ยอ่มไมส่ามารถถงึซงึความเป็นเลศินันไดเ้ลย

-- ภชุงค ์อทุโยภาศ

Page 3: Android App Development 02 : Activity & Layout

Activity

An activity represents the visual representation of an Android application. activities use views, i.e. user interface widgets as for example buttons to create the user interface and to interact with the user. An Android application can have several activities.

Page 4: Android App Development 02 : Activity & Layout

Activity Life Cycle

Page 5: Android App Development 02 : Activity & Layout

Created

Page 6: Android App Development 02 : Activity & Layout

Paused and Resumed

Page 7: Android App Development 02 : Activity & Layout

Stopped

Page 8: Android App Development 02 : Activity & Layout

Save and Restore State

Page 9: Android App Development 02 : Activity & Layout

Workshop: Trace Activity Life Cycle

You can use Log or Toast to check life cycle of activity on each methods;

● onCreate● onStart● onResume● onPause● onStop● onDestroy

Page 11: Android App Development 02 : Activity & Layout

Activity Layout

The user interface for Activities is typically defined via XML files (layout files).

Page 12: Android App Development 02 : Activity & Layout
Page 13: Android App Development 02 : Activity & Layout
Page 14: Android App Development 02 : Activity & Layout

Trick: Single Task

Prevent create instance of Activity, you can identify launchMode in AndroidManifest.xml● force for single instance

○ android:launchMode="singleInstance"● force for single task

○ android:launchMode="singleTask"● force app to recognize latest state

○ android:alwaysRetainTaskState="true"

Page 15: Android App Development 02 : Activity & Layout

Trick: Screen Orientation

You can identify the screen orientation by config in AndroidManifest.xml ● Portrait

○ android:screenOrientation="portrait"● Landscape

○ android:screenOrientation="landscape"● Landscape with No Keyboard

○ android:configChange="orientation|kyboardHidden"

Page 16: Android App Development 02 : Activity & Layout

Multiple Activities

Single App has at least 1 Activity, complex app has multiple activity. You can call to another activity using Intent.

Page 17: Android App Development 02 : Activity & Layout

Workshop: Two Activities

Create App with 2 Activities each activity has a button for navigate to other activity. Using Intent to call another activity.

Intent i = new Intent(MainActivity.this,SecondActivity);

startactivity(i);

Page 19: Android App Development 02 : Activity & Layout

Workshop: Sent data between activity

Create App with 2 Activities, first activity has text field and button, after push button it'll sent data in text field to the second activity. Using putExtra method to create a variable and identify value;

Intent i = new Intent(getApplicationContext(), NewActivity.class);

i.putExtra("new_variable_name","value");

startActivity(i);

Page 20: Android App Development 02 : Activity & Layout

Sent a value

Using putExtra method to create a variable and identify value;

Intent i = new Intent(getApplicationContext(), NewActivity.class);

i.putExtra("new_variable_name","value");

startActivity(i);

Page 21: Android App Development 02 : Activity & Layout

Receive a value

Using getExtras methods to receive value from variable;

Bundle extras = getIntent().getExtras();if (extras != null) { String value = extras.getString("new_variable_name");}

Page 23: Android App Development 02 : Activity & Layout

Workshop: Temperature Converter

Create multiple Activity App, temperature converter between Celsius and Fahrenheit using the following formula

°C x 9/5 + 32 = °F(°F - 32) x 5/9 = °C

Page 25: Android App Development 02 : Activity & Layout

Layouts

Android has different layouts for place an widgets ● Linear Layout● Relative Layout● Frame Layout

Page 26: Android App Development 02 : Activity & Layout

Workshop: App with Relative Layout

Create single Activity App with Label and Button using Relative Layout and see how relative layout work.

Page 28: Android App Development 02 : Activity & Layout

Workshop: App with Linear Layout

Create single Activity App with Label and Button using Linear Layout and see how the linear layout work.

Page 31: Android App Development 02 : Activity & Layout

Workshop: App with Frame Layout

Create single Activity App with Label and Button using Frame Layout and see how the frame layout work.

Page 33: Android App Development 02 : Activity & Layout

Trick: Dump Layout

You can use DDMS tool to dump screen to see the layout design and properties

Page 35: Android App Development 02 : Activity & Layout

End