Загрузка страницы

Android Studio Tutorial - Part 2 (2020 Edition)

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/Android-Studio-Tutorial-2020/blob/master/app/src/main/java/com/example/AddProductActivity.kt
MainActivity.kt is here: https://github.com/danielmalone/Android-Studio-Tutorial-2020/blob/master/app/src/main/java/com/example/MainActivity.kt
add_product.xml is here: https://github.com/danielmalone/Android-Studio-Tutorial-2020/blob/master/app/src/main/res/layout/add_product.xml
Entire code is here: https://github.com/danielmalone/Android-Studio-Tutorial-2020

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! https://linkedin.com/in/danielmalone

Видео Android Studio Tutorial - Part 2 (2020 Edition) канала Daniel Malone
Показать
Комментарии отсутствуют
Введите заголовок:

Введите адрес ссылки:

Введите адрес видео с YouTube:

Зарегистрируйтесь или войдите с
Информация о видео
22 февраля 2020 г. 22:36:47
00:29:15
Яндекс.Метрика