Intro to Android for iOS developers

6,820 views
6,661 views

Published 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 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?

Published in: Technology, News & Politics
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
6,820
On SlideShare
0
From Embeds
0
Number of Embeds
3,556
Actions
Shares
0
Downloads
111
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Intro to Android for iOS developers

  1. 1. Intro to Android for iOS developers Chiu-Ki Chan @chiuki
  2. 2. @chiuki@chiuki
  3. 3. @chiuki@chiuki Hello World Layouts Intents & Components
  4. 4. Hello World
  5. 5. @chiuki
  6. 6. @chiuki ViewController
  7. 7. @chiuki
  8. 8. @chiuki
  9. 9. @chiuki xib
  10. 10. @chiuki
  11. 11. @chiuki
  12. 12. @chiuki
  13. 13. @chiuki
  14. 14. @chiuki IBOutlet
  15. 15. @chiuki
  16. 16. @chiuki@chiuki Center <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" />
  17. 17. @chiuki
  18. 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. 19. @chiuki LinearLayout
  20. 20. @chiuki
  21. 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. 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. 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. 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. 25. @chiuki
  26. 26. @chiuki
  27. 27. @chiuki RelativeLayout
  28. 28. @chiuki
  29. 29. @chiuki
  30. 30. @chiuki
  31. 31. @chiuki
  32. 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. 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. 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. 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. 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. 37. @chiuki@chiuki Device Preview
  38. 38. @chiuki@chiuki Bigger text on tablets
  39. 39. Resource Folders
  40. 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. 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. 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. 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. 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. 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. 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. 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. 48. Activity Lifecycle
  49. 49. @chiuki@chiuki onCreate() Activity Lifecycle
  50. 50. @chiuki@chiuki onCreate() Activity Lifecycle
  51. 51. @chiuki@chiuki onCreate() viewDidLoad: Activity Lifecycle
  52. 52. @chiuki@chiuki onCreate() viewDidLoad: onStart() Activity Lifecycle
  53. 53. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() Activity Lifecycle
  54. 54. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: Activity Lifecycle
  55. 55. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() Activity Lifecycle
  56. 56. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() Activity Lifecycle
  57. 57. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: Activity Lifecycle
  58. 58. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: onDestroy() Activity Lifecycle
  59. 59. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: onDestroy() viewDidUnload: Activity Lifecycle
  60. 60. @chiuki@chiuki onCreate() viewDidLoad: onStart() onResume() viewWillAppear: viewDidAppear: onPause() onStop() viewWillDisappear: viewDidDisappear: onDestroy() viewDidUnload: Activity Lifecycle
  61. 61. @chiuki@chiuki Visibility
  62. 62. More Activities
  63. 63. @chiuki@chiuki startActivity (explicit) Intent intent = new Intent(this, NumberActivity.class); intent.putExtra("count", count); startActivity(intent);
  64. 64. @chiuki@chiuki startActivity (explicit) Intent intent = new Intent(this, NumberActivity.class); intent.putExtra("count", count); startActivity(intent);
  65. 65. @chiuki@chiuki startActivity (explicit) Intent intent = new Intent(this, NumberActivity.class); intent.putExtra("count", count); startActivity(intent);
  66. 66. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  67. 67. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  68. 68. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  69. 69. @chiuki@chiuki startActivity (implicit) Intent intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("http://twitter.com/chiuki")); startActivity(intent);
  70. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 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. 84. Beyond Activities
  85. 85. @chiuki@chiuki Service BroadcastReceiver ContentProvider Notification
  86. 86. @chiuki@chiuki Service Look ma, no UI!
  87. 87. @chiuki@chiuki Service Long-running
  88. 88. @chiuki@chiuki BroadcastReceiver Respond
  89. 89. @chiuki@chiuki BroadcastReceiver Respond Battery low Wifi connected Incoming SMS App installed Screen off
  90. 90. @chiuki@chiuki ContentProvider Provide data to other apps
  91. 91. @chiuki@chiuki ContentProvider Provide data to other apps Contacts Images Music Calendar
  92. 92. @chiuki@chiuki ContentProvider Internal data (unless you need SQLite interface)
  93. 93. @chiuki@chiuki Notification Notify
  94. 94. @chiuki@chiuki Notify
  95. 95. @chiuki@chiuki Notification Communicate with background Service
  96. 96. @chiuki@chiuki Background Service
  97. 97. Case study
  98. 98. @chiuki Email Outbox
  99. 99. @chiuki
  100. 100. @chiuki@chiuki Notification
  101. 101. @chiuki@chiuki AlarmService
  102. 102. @chiuki@chiuki Connectivity change
  103. 103. Summary
  104. 104. @chiuki@chiuki Summary Hello World Layouts Resource Folders Activity Lifecycle Intents & Components Case Study
  105. 105. @chiuki@chiuki Thank you! http://eepurl.com/lR5uD http://blog.sqisland.com http://twitter.com/chiuki

×