Содержание
- 2. You can trust me Mobile Consultant 7+ years of Android, 9+ years total Conferences & meetups,
- 3. Checklist Android Studio, Java, Git are ready to use Checked-in on Eventbrite Joined slack (android-academy-msk) https://bit.ly/2NIR0x0
- 4. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 5. Android Studio & First Project
- 6. File >> New Project
- 7. File >> New Project
- 9. https://developer.android.com/about/dashboards/index.html
- 11. What’s an Activity? Activity - a component with a screen with which users can interact in
- 14. API 28 Alchaka Art of Movement Breath control Center of Being Detoxify Poison Flashburn Force Body
- 15. API 21 Alchaka Art of Movement Breath control Center of Being Detoxify Poison Flashburn Force Body
- 16. Alchaka Art of Movement Breath control Center of Being Detoxify Poison Flashburn Force Body Force Enlightenment
- 18. AndroidManifest.xml MainActivity.java activity_main.xml 3 Interesting Things:
- 22. How Does An Activity Look?
- 23. Android Application Structure for now Application Activity A Activity B ... … Manifest ... Activity C
- 24. How to run the app? Push the tempo!
- 25. How to run the app? We need to choose a device.
- 26. Option 1: A Real Device Allow USB Debugging. It’s in the Developer Options setting, which might
- 27. Android developer in 30 seconds Android developer
- 28. Confirm popup and run
- 29. Option 2: A Virtual Device Open the AVD manager and create a virtual device. The run
- 30. I click “Run”, You say Ho! Android Studio Builds the project
- 31. *.java *.xml images, videos AndroidManifest.xml Android Asset Packaging Tool (aapt) R.java Java Compiler Compiled Resources *.class
- 32. What does it take to build the project? But Gradle helps us with it. https://developer.android.com/studio/build/
- 33. I click “Run”, You say Ho! Android Studio Builds the project http://developer.android.com/tools/help/adb.html
- 34. How is it run? Android looks through the application’s manifest Finds an Activity with special “marker”
- 35. It’s ALIVE!
- 36. Build config Project’s build.gradle App’s build.gradle
- 39. Any Questions?
- 40. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 41. A Little Bit On Layouts
- 42. Views Everything in that XML file is a View. A View: Knows to draw itself Used
- 43. ViewGroup (Layout) A special kind of view. Knows to position its children views on the screen.
- 44. LinearLayout Lays items in a row or a column. Can also divide existing space by weight.
- 45. How does the layout look?
- 47. 1 2 3
- 48. Any Questions?
- 49. BTC Miner
- 52. activity_main.xml
- 53. LinearLayout orientation
- 54. activity_main.xml
- 55. **making things prettier**
- 56. Change Text Size (editor)
- 57. Change Text Size (code) ... android:textSize="70sp" /> ... android:textSize="70sp" />
- 58. Save Size We have set up the value 70sp twice! Repetition in code?? Not for me!
- 59. Save Size in dimens.xml 70sp
- 60. dimens.xml
- 61. Use Size From dimen.xml ... android:textSize="@dimen/btc_text_size"/>
- 62. Saving String Resources ... android:text="ADD BTC"/>
- 63. Saving String Resources ... android:text="@string/add_btc"/>
- 64. Saving String on strings.xml BTC Miner ADD BTC 0
- 65. strings.xml
- 66. Resources Layouts Drawable (images, vectors, shapes) Animations Colors Themes & Styles Booleans / Integers / Dimens
- 67. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 68. BTC Miner Does nothing...
- 69. Activity public class MainActivity extends AppCompatActivity { }
- 70. Activity public class MainActivity extends AppCompatActivity { }
- 71. Activity public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
- 72. Activity public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } }
- 73. setContentView public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
- 74. Let’s make it mine! Access Views declared in XML from Java code Change text in TextView
- 75. Reference to the view on code public class MainActivity extends AppCompatActivity { private TextView btcText; @Override
- 76. Assign view ID ... android:id="@+id/btc_text"/>
- 77. Reference to the view on code public class MainActivity extends AppCompatActivity { private TextView btcText; @Override
- 78. *.java *.xml images, videos AndroidManifest.xml Android Asset Packaging Tool (aapt) R.java Java Compiler Compiled Resources *.class
- 79. Mighty (Tho)R Autogenerated Each resource type becomes a static nested class Each resource becomes an int
- 80. Change text in TextView btcText = findViewById(R.id.btc_text); btcText.setText("Hello World!");
- 81. Change text in TextView btcText = findViewById(R.id.btc_text); btcText.setText("Hello World!");
- 82. Change text in TextView btcText = findViewById(R.id.btc_text); btcText.setText(R.string.initial_btc);
- 83. Find a button and setOnClickListener Button addBtcButton = findViewById(R.id.add_btc_button); addBtcButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View
- 84. Find a button and setOnClickListener Button addBtcButton = findViewById(R.id.add_btc_button); addBtcButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View
- 85. Let’s make a BTC counter! We’ll need a BTC counter variable. Let’s add an int and
- 86. Setting onClickListener addBtcButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { btcCounter++; String btc = String.valueOf(btcCounter);
- 87. Not xml? android:id="@+id/add_btc_button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:onClick="onButtonClick"/> public void onButtonClick(View view) { }
- 88. OMG, It looks great!
- 89. Questions?
- 90. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 91. Rotate the device
- 92. WHERE ARE... MY BTC?????...
- 93. Is it a bug or feature?
- 94. Rotate the device
- 95. Create a different layout for landscape
- 96. activity_main.xml (#2)
- 98. Now: Rotate the device
- 99. setContentView public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); }
- 101. First android device HTC Dream 320x480px (2:3) 180 ppi 3.2” (81mm) http://en.wikipedia.org/wiki/HTC_Dream
- 102. Current Android Device Pixel 2 XL 2880x1440px (18:9) 538 ppi 6.0” (152mm) https://en.wikipedia.org/wiki/Pixel_2
- 103. Big Android Device Nexus 10 (Tablet) 2560x1600px 300ppi 10.1” http://en.wikipedia.org/wiki/Nexus_10
- 104. Small Android Device LG G Watch Most Same techniques apply http://en.wikipedia.org/wiki/Android_Wear
- 105. The difference is Big
- 106. Differences Pixel Density Screen size Orientation OS version Locale Device type ... https://developer.android.com/guide/topics/manifest/activity-element#config https://developer.android.com/guide/topics/resources/providing-resources#AlternativeResources
- 108. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 109. We need more Activities!
- 110. Creating another Activity Using Android Studio, use Alt + Insert -or- Command + N to create
- 111. File >> New Project
- 112. File >> New Project
- 113. You can’t just open an activity on your own - Android does it for you. And
- 114. Intents have... used for Explicit Intents used for Implicit Intents used to tell things to the
- 115. How to open SecondActivity MainActivity.java: public void openSecondActivity() { Intent secondActivityIntent = new Intent(this, SecondActivity.class); startActivity(secondActivityIntent);
- 116. Invoke opening SecondActivity Let’s open it on every 5th BTC mined! addBtcButton.setOnClickListener(new View.OnClickListener() { @Override public
- 117. Intents have... used for Explicit Intents used for Implicit Intents used to tell things to the
- 118. MainActivity.java: private void openSecondActivity() { Intent secondActivityIntent = new Intent(this, SecondActivity.class); secondActivityIntent.putExtra("KEY_BTC", btcCounter); startActivity(secondActivityIntent); } Pass
- 119. Pass arguments to next activity SecondActivity.java: public static final String KEY_BTC = "KEY_BTC"; MainActivity.java: private void
- 120. Get the data passed by the caller See that the data passed @Override protected void onCreate(Bundle
- 121. activity_second.xml The whole Activity is just a TextView with ID: android:id="@+id/status_text" android:layout_width="match_parent" android:layout_height="match_parent" />
- 122. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); int btcCount = getIntent().getIntExtra(KEY_BTC, 0); } Use the
- 123. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); int btcCount = getIntent().getIntExtra(KEY_BTC, 0); TextView statusText =
- 124. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); int btcCount = getIntent().getIntExtra(KEY_BTC, 0); TextView statusText =
- 125. String with format BTC Miner ADD BTC 0 Wow! You already have %d BTC
- 126. SecondActivity
- 127. Best Practice: Start Activity Statically SecondActivity.java: private static final String KEY_BTC = "KEY_BTC"; public static void
- 128. Best Practice: Start Activity Statically SecondActivity.java: private static final String KEY_BTC = "KEY_BTC"; public static void
- 129. Best Practice: Start Activity Statically MainActivity.java: public void openSecondActivity() { SecondActivity.start(this, btcCounter); }
- 130. Any Questions?
- 131. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 132. Activity Lifecycle
- 133. The Activity Lifecycle https://developer.android.com/guide/components/activities/activity-lifecycle
- 134. The Activity Lifecycle When activity lifecycle state changes, Android lets us know with a callback: onCreate()
- 135. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 136. Logging
- 137. Logging Definition: A logfile is a file that records either events that occur in an operating
- 138. Log level Verbose Log.v() Debug Log.d() Info Log.i() Warn Log.w() Error Log.e()
- 139. Log level Verbose Log.v() Debug Log.d() Info Log.i() Warn Log.w() Error Log.e() Fatal Log.wtf()
- 140. Logging
- 141. private static final String TAG = "MainActivity"; @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume"); }
- 142. private static final String TAG = "MainActivity"; @Override protected void onResume() { super.onResume(); Log.i(TAG, "onResume"); }
- 143. Any Questions?
- 144. Agenda Android Studio & First Project Activity & Layouts Resources Java & XML works together Different
- 145. Recap What’s an Activity? Describe Activity Lifecycle methods. What’s the Manifest? Which resource types do we
- 146. git - getting today’s code If you want to grab the code for this lecture, you
- 147. And now….Exercise! https://goo.gl/phRktb
- 148. Fundamentals #2: Views
- 150. Скачать презентацию