Android Studio Tutorial - Part 2 (2020 Edition)

Published: 22 February 2020
on channel: DJ Malone
185,795
2.2k

Note: This series is partially outdated. View the updated 2021 series here:    • Android Studio Tutorial (2021 Edition...   (View both series if you find them helpful, but if you get stuck see updated techniques in the 2021 series.)


In this Android Studio tutorial, let's create a new screen, or Activity, as it's called in the world of Android. (Activity is another word for a screen.) In the previous lesson we added a Button to our app, but when the user clicks on the Button it didn't do anything. We'll fix that.

Want to look at the Kotlin / XML code I wrote? See the files here on GitHub:
AddProductActivity.kt is here: https://github.com/danielmalone/Andro...
MainActivity.kt is here: https://github.com/danielmalone/Andro...
add_product.xml is here: https://github.com/danielmalone/Andro...
Entire code is here: https://github.com/danielmalone/Andro...

First, we'll create a new XML layout file in the res / layout folder. We'll drag in a EditText then use ConstraintLayout to position it near the top left of the screen. After that we'll create a new Kotlin file or Kotlin class. To successfully create a new Activity we need two files: a layout file and a Kotlin class that extends AppCompatActivity. The Kotlin Activity should override the onCreate method and setContentView should be inside of it.

To actually link the two screens together we need to give the Button in content_main.xml and ID. We'll use that ID in MainActivity.kt to trigger an onClickListener. To start a new screen we call the Android method startActivity(), passing an Intent inside of it. The Intent basically tells the startActivity() what screen to go to. If you app has 52 screens, Android won't know which screen to go to. To tell it which Kotlin Activity to go to by typing the class followed by ::class.java.

If the app crashes, it's probably because the new Kotlin class isn't in the manifest. All Kotlin Activities have to be registered in the manifest. Open the Activity then press Alt + Enter, or Option + Enter on a Mac. Select Add to Manifest. Run the app in the emulator again (or a phone plugged in via USB) and click on the Button. It should navigate you to the new screen! Let's hope :)

Sorry this tutorial is so long! I'll make them shorter in the future. Or if you like the long videos let me know? :)

Connect with me on LinkedIn!   / danielmalone  


Watch video Android Studio Tutorial - Part 2 (2020 Edition) online without registration, duration hours minute second in high quality. This video was added by user DJ Malone 22 February 2020, don't forget to share it with your friends and acquaintances, it has been viewed on our site 185,795 once and liked it 2.2 thousand people.