Intro to Android for iOS developers

  • 4,611 views
Uploaded on

Curious about Android, but never found the time to look into it? Come to this session for an accelerated introduction. We will look at the basic structure of an Android app, then deep dive into two …

Curious about Android, but never found the time to look into it? Come to this session for an accelerated introduction. We will look at the basic structure of an Android app, then deep dive into two aspects that differentiate Android from iOS: layout and intents. How does the layout system deal with the myriad of Android devices? How do intents facilitate deep integration among Android apps?

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
4,611
On Slideshare
0
From Embeds
0
Number of Embeds
12

Actions

Shares
Downloads
73
Comments
0
Likes
1

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Intro to Android for iOS developers Chiu-Ki Chan @chiuki
  • 2. @chiuki@chiuki
  • 3. @chiuki@chiuki Hello World Layouts Intents & Components
  • 4. Hello World
  • 5. @chiuki
  • 6. @chiuki ViewController
  • 7. @chiuki
  • 8. @chiuki
  • 9. @chiuki xib
  • 10. @chiuki
  • 11. @chiuki
  • 12. @chiuki
  • 13. @chiuki
  • 14. @chiuki IBOutlet
  • 15. @chiuki
  • 16. @chiuki@chiuki Center <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />
  • 17. @chiuki
  • 18. @chiuki@chiuki Button <Button android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:text="@string/add" />
  • 19. @chiuki LinearLayout
  • 20. @chiuki
  • 21. @chiuki@chiuki LinearLayout <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="128sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add" android:onClick="add" /> </LinearLayout>
  • 22. @chiuki@chiuki IBAction <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" > <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="128sp" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/add" android:onClick="add" /> </LinearLayout>
  • 23. @chiuki@chiuki IBAction public class MainActivity extends Activity { private TextView countView; private int count = 0; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countView = (TextView) findViewById(R.id.count); updateCount(); } public void add(View v) { count += 1; updateCount(); } private void updateCount() { countView.setText(String.valueOf(count)); } } android:onClick="add"
  • 24. @chiuki@chiuki Update view public class MainActivity extends Activity { private TextView countView; private int count = 0; protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); countView = (TextView) findViewById(R.id.count); updateCount(); } public void add(View v) { count += 1; updateCount(); } private void updateCount() { countView.setText(String.valueOf(count)); } }
  • 25. @chiuki
  • 26. @chiuki
  • 27. @chiuki RelativeLayout
  • 28. @chiuki
  • 29. @chiuki
  • 30. @chiuki
  • 31. @chiuki
  • 32. @chiuki <RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /> </RelativeLayout>
  • 33. @chiuki <RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /> </RelativeLayout>
  • 34. @chiuki <RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /> </RelativeLayout>
  • 35. @chiuki <RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /> </RelativeLayout>
  • 36. @chiuki <RelativeLayout> <TextView android:id="@+id/count" android:layout_centerInParent="true" /> <Button android:id="@+id/reset" android:layout_below="@id/count" android:layout_centerHorizontal="true" android:text="@string/reset" /> <Button android:layout_below="@id/count" android:layout_toLeftOf="@id/reset" android:text="@string/subtract" /> <Button android:layout_below="@id/count" android:layout_toRightOf="@id/reset" android:text="@string/add" /> </RelativeLayout>
  • 37. @chiuki@chiuki Device Preview
  • 38. @chiuki@chiuki Bigger text on tablets
  • 39. Resource Folders
  • 40. @chiuki@chiuki textSize <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="128sp" /> res/layout/activity_main.xml
  • 41. @chiuki@chiuki res/values/dimens.xml <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/counter_size" /> res/layout/activity_main.xml <dimen name="counter_size" value="128sp" /> res/values/dimens.xml
  • 42. @chiuki@chiuki res/values-sw600dp/dimens.xml <TextView android:id="@+id/count" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="@dimen/counter_size" /> res/layout/activity_main.xml <dimen name="counter_size" value="128sp" /> <dimen name="counter_size" value="256sp" /> res/values/dimens.xml res/values-sw600dp/dimens.xml
  • 43. @chiuki@chiuki Resource Folders Type Variation layout values drawable menu Language & Region: en, fr, fr-rCA, ja Screen size: small, large, sw600dp, h400dp Screen orientation: port, land Screen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi Platform version: v4, v11, v14 UI mode: car, desk, television, appliance http://developer.android.com/guide/topics/resources/providing-resources.html
  • 44. @chiuki@chiuki Resource Folders Type Variation layout values drawable menu Language & Region: en, fr, fr-rCA, ja Screen size: small, large, sw600dp, h400dp Screen orientation: port, land Screen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi Platform version: v4, v11, v14 UI mode: car, desk, television, appliance http://developer.android.com/guide/topics/resources/providing-resources.html res/values-sw600dp/dimens.xml
  • 45. @chiuki@chiuki Resource Folders Type Variation layout values drawable menu Language & Region: en, fr, fr-rCA, ja Screen size: small, large, sw600dp, h400dp Screen orientation: port, land Screen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi Platform version: v4, v11, v14 UI mode: car, desk, television, appliance http://developer.android.com/guide/topics/resources/providing-resources.html res/drawable-hdpi/ic_launcher.png
  • 46. @chiuki@chiuki Resource Folders Type Variation layout values drawable menu Language & Region: en, fr, fr-rCA, ja Screen size: small, large, sw600dp, h400dp Screen orientation: port, land Screen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi Platform version: v4, v11, v14 UI mode: car, desk, television, appliance http://developer.android.com/guide/topics/resources/providing-resources.html res/drawable-hdpi/ic_launcher.png @2x
  • 47. @chiuki@chiuki Resource Folders Type Variation layout values drawable menu Language & Region: en, fr, fr-rCA, ja Screen size: small, large, sw600dp, h400dp Screen orientation: port, land Screen density: ldpi, mdpi, hdpi, xhdpi, nodpi, tvdpi Platform version: v4, v11, v14 UI mode: car, desk, television, appliance http://developer.android.com/guide/topics/resources/providing-resources.html res/layout-ja/name.xml
  • 48. Activity Lifecycle
  • 49. @chiuki@chiuki onCreate() Activity Lifecycle
  • 50. @chiuki@chiuki onCreate() Activity Lifecycle
  • 51. @chiuki@chiuki onCreate() viewDidLoad: Activity Lifecycle
  • 52. @chiuki@chiuki onCreate() viewDidLoad: onStart() Activity Lifecycle
  • 53. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() Activity Lifecycle
  • 54. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: Activity Lifecycle
  • 55. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() Activity Lifecycle
  • 56. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() Activity Lifecycle
  • 57. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: Activity Lifecycle
  • 58. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: onDestroy() Activity Lifecycle
  • 59. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: onDestroy() viewDidUnload: Activity Lifecycle
  • 60. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: onDestroy() viewDidUnload: Activity Lifecycle
  • 61. @chiuki@chiuki Visibility
  • 62. More Activities
  • 63. @chiuki@chiuki startActivity (explicit) Intent intent = new Intent(this, NumberActivity.class); intent.putExtra("count", count); startActivity(intent);
  • 64. @chiuki@chiuki startActivity (explicit) Intent intent = new Intent(this, NumberActivity.class); intent.putExtra("count", count); startActivity(intent);
  • 65. @chiuki@chiuki startActivity (explicit) Intent intent = new Intent(this, NumberActivity.class); intent.putExtra("count", count); startActivity(intent);
  • 66. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 67. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 68. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 69. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 70. @chiuki@chiuki Register your Activity <activity android:name=".YourActivity"> <intent-filter>    <action android:name="android.intent.action.VIEW" />     <category android:name="android.intent.category.DEFAULT" />     <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />   </intent-filter> </activity> AndroidManifest.xml Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent); Info.plist
  • 71. @chiuki@chiuki IntentFilter: Action <activity android:name=".YourActivity"> <intent-filter>    <action android:name="android.intent.action.VIEW" />     <category android:name="android.intent.category.DEFAULT" />     <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />   </intent-filter> </activity> AndroidManifest.xml Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 72. @chiuki@chiuki IntentFilter: Data <activity android:name=".YourActivity"> <intent-filter>    <action android:name="android.intent.action.VIEW" />     <category android:name="android.intent.category.DEFAULT" />     <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />   </intent-filter> </activity> AndroidManifest.xml Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 73. @chiuki@chiuki IntentFilter: Category <activity android:name=".YourActivity"> <intent-filter>    <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.DEFAULT" />     <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />   </intent-filter> </activity> AndroidManifest.xml Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 74. @chiuki@chiuki IntentFilter: Category <activity android:name=".YourActivity"> <intent-filter>    <action android:name="android.intent.action.VIEW" />     <category android:name="android.intent.category.DEFAULT" />     <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />   </intent-filter> </activity> AndroidManifest.xml Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  • 75. @chiuki@chiuki Intent Data <activity android:name=".YourActivity"> <intent-filter>    <action android:name="android.intent.action.VIEW" />     <category android:name="android.intent.category.DEFAULT" />     <category android:name="android.intent.category.BROWSABLE" />   <data android:scheme="http" android:host="twitter.com" />   </intent-filter> </activity> Uri uri = getIntent().getData(); AndroidManifest.xml YourActivity.java
  • 76. @chiuki@chiuki startActivityForResult private static final int REQUEST_CODE_CAMERA = 1; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CODE_CAMREA); protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); } }
  • 77. @chiuki@chiuki startActivityForResult private static final int REQUEST_CODE_CAMERA = 1; Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(intent, REQUEST_CODE_CAMERA); protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); } }
  • 78. @chiuki@chiuki Providing Result Intent data = new Intent(); data.putExtra("data", bitmap); setResult(RESULT_OK, data); finish(); protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); } }
  • 79. @chiuki@chiuki Intent Data Intent data = new Intent(); data.putExtra("data", bitmap); setResult(RESULT_OK, data); finish(); protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); } }
  • 80. @chiuki@chiuki Result Code Intent data = new Intent(); data.putExtra("data", bitmap); setResult(RESULT_OK, data); finish(); protected void onActivityResult( int requestCode, int resultCode, Intent data) { if (requestCode == REQUEST_CODE_CAMERA && resultCode == RESULT_OK) { Bitmap bitmap = (Bitmap) data.getExtras().get("data"); } }
  • 81. @chiuki@chiuki Share intent Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, message); intent.setType("image/*"); Uri uri = Uri.fromFile(new File(path)); intent.putExtra(Intent.EXTRA_STREAM, uri);
  • 82. @chiuki@chiuki IntentFilter: Type <activity android:name=".YourActivity"> <intent-filter>    <action android:name="android.intent.action.ACTION_SEND" />     <category android:name="android.intent.category.DEFAULT" />   <data android:mimeType="image/*" />   </intent-filter> </activity> AndroidManifest.xml Intent intent = new Intent(Intent.ACTION_SEND); intent.setType("image/*");
  • 83. @chiuki@chiuki Intent Data String subject = intent.getStringExtra(Intent.EXTRA_SUBJECT); String message = intent.getStringExtra(Intent.EXTRA_TEXT); Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM); YourActivity.java Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_SUBJECT, subject); intent.putExtra(Intent.EXTRA_TEXT, message); intent.setType("image/*"); Uri uri = Uri.fromFile(new File(path)); intent.putExtra(Intent.EXTRA_STREAM, uri);
  • 84. Beyond Activities
  • 85. @chiuki@chiuki Service BroadcastReceiver ContentProvider Notification
  • 86. @chiuki@chiuki Service Look ma, no UI!
  • 87. @chiuki@chiuki Service Long-running
  • 88. @chiuki@chiuki BroadcastReceiver Respond
  • 89. @chiuki@chiuki BroadcastReceiver Respond Battery low Wifi connected Incoming SMS App installed Screen off
  • 90. @chiuki@chiuki ContentProvider Provide data to other apps
  • 91. @chiuki@chiuki ContentProvider Provide data to other apps Contacts Images Music Calendar
  • 92. @chiuki@chiuki ContentProvider Internal data (unless you need SQLite interface)
  • 93. @chiuki@chiuki Notification Notify
  • 94. @chiuki@chiuki Notify
  • 95. @chiuki@chiuki Notification Communicate with background Service
  • 96. @chiuki@chiuki Background Service
  • 97. Case study
  • 98. @chiuki Email Outbox
  • 99. @chiuki
  • 100. @chiuki@chiuki Notification
  • 101. @chiuki@chiuki AlarmService
  • 102. @chiuki@chiuki Connectivity change
  • 103. Summary
  • 104. @chiuki@chiuki Summary Hello World Layouts Resource Folders Activity Lifecycle Intents & Components Case Study
  • 105. @chiuki@chiuki Thank you! http://eepurl.com/lR5uD http://blog.sqisland.com http://twitter.com/chiuki