android app development 02 : activity & layout

Post on 01-Sep-2014

501 Views

Category:

Documents

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Activity & LayoutAnuchit Chalothornanoochit@gmail.com

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

2

Quote

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

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

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

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

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

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

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.

Activity Life Cycle

Created

Paused and Resumed

Stopped

Save and Restore State

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

Activity Layout

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

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"

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"

Multiple Activities

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

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

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

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

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

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

Layouts

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

Workshop: App with Relative Layout

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

Workshop: App with Linear Layout

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

Workshop: App with Frame Layout

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

Trick: Dump Layout

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

End

top related