Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases now! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Android Programming with Kotlin for Beginners

You're reading from   Android Programming with Kotlin for Beginners Build Android apps starting from zero programming experience with the new Kotlin programming language

Arrow left icon
Product type Paperback
Published in Apr 2019
Publisher Packt
ISBN-13 9781789615401
Length 698 pages
Edition 1st Edition
Languages
Tools
Arrow right icon
Author (1):
Arrow left icon
John Horton John Horton
Author Profile Icon John Horton
John Horton
Arrow right icon
View More author details
Toc

Table of Contents (31) Chapters Close

Preface 1. Getting Started with Android and Kotlin FREE CHAPTER 2. Kotlin, XML, and the UI Designer 3. Exploring Android Studio and the Project Structure 4. Getting Started with Layouts and Material Design 5. Beautiful Layouts with CardView and ScrollView 6. The Android Lifecycle 7. Kotlin Variables, Operators, and Expressions 8. Kotlin Decisions and Loops 9. Kotlin Functions 10. Object-Oriented Programming 11. Inheritance in Kotlin 12. Connecting Our Kotlin to the UI and Nullability 13. Bringing Android Widgets to Life 14. Android Dialog Windows 15. Handling Data and Generating Random Numbers 16. Adapters and Recyclers 17. Data Persistence and Sharing 18. Localization 19. Animations and Interpolations 20. Drawing Graphics 21. Threads and Starting the Live Drawing App 22. Particle Systems and Handling Screen Touches 23. Android Sound Effects and the Spinner Widget 24. Design Patterns, Multiple Layouts, and Fragments 25. Advanced UI with Paging and Swiping 26. Advanced UI with Navigation Drawer and Fragment 27. Android Databases 28. A Quick Chat Before You Go A. Other Book You May Enjoy Index

Deploying the app so far

Before we explore any of the code and learn our first bit of Kotlin, you might be surprised to learn that we can already run our project. It will be a fairly featureless screen, but as we will be running the app as often as possible to check our progress, let's see how to do that now. You have three options:

  • Run the app on the emulator on your PC (part of Android Studio) in debug mode
  • Run the app on a real Android device in USB debugging mode
  • Export the app as a full Android project that can be uploaded to the Play Store

The first option (debug mode) is the easiest to set up, because we did it as part of setting up Android Studio. If you have a powerful PC, you will hardly see the difference between the emulator and a real device. However, screen touches are emulated by mouse clicks, and proper testing of the user experience is not possible in some of the later apps, such as the drawing app. Furthermore, you might just prefer to test out your creations on a real device – I know I do.

The second option, using a real device, has a couple of additional steps, but, once set up, is as good as option one, and the screen touches are for real.

The final option takes about five minutes (at least) to prepare, and then you need to manually put the created package onto a real device and install it (every time you make a change to the code).

The best way is probably to use the emulator to quickly test and debug minor increments in your code, and then use the USB debugging mode on a real device fairly regularly to make sure things are still as expected. Only occasionally will you want to export an actual deployable package.

Tip

If you have an especially slow PC or a particularly aging Android device, you will be fine just running the projects in this book using just one option or the other. Note that a slow Android phone will probably be OK and cope, but a very slow PC will probably not handle the emulator running some of the later apps, and you will benefit from running them on your phone/tablet.

For these reasons, I will now go through how to run the app using the emulator and USB debugging on a real device.

Running and debugging the app on an Android emulator

Follow these simple steps to run the app on the default Android emulator:

  1. On the Android Studio main menu bar, select Tools | AVD Manager. AVD stands for Android Virtual Device (an emulator). You will see the following window:
    Running and debugging the app on an Android emulator
  2. Notice that there is an emulator in the list. In my case, it is Pixel 2 XL API 28. If you are following this sometime in the future, it will be a different emulator that was installed by default. It won't matter. Click the green play icon (to the right) shown in the following screenshot, and wait while the emulator boots up:
    Running and debugging the app on an Android emulator
  3. Now you can click the play icon on the Android Studio quick-launch bar as shown in the following screenshot and, when prompted, choose Pixel 2 XL API 28 (or whatever your emulator is called) and the app will launch on the emulator:
    Running and debugging the app on an Android emulator

You're done. Here is what the app looks like so far in the emulator. Remember that you might (probably do) have a different emulator, which is fine:

Running and debugging the app on an Android emulator

Clearly, we have more work to do before we move to Silicon Valley and look for financial backing, but it is a good start.

We need to test and debug our apps often throughout development to check for any errors, crashes, or anything else unintended.

Note

We will see how we get errors and other feedback for debugging from our apps in the next chapter.

It is also important to make sure it looks good and runs correctly on every device type/size that you want to target. Obviously, we do not own one of each of the many thousands of Android devices. This is where emulators come in.

Emulators, however, are sometimes a bit slow and cumbersome, although they have improved a lot recently. If we want to get a genuine feel for the experience our user will get, then you can't beat deploying to a real device. So, we will want to use both real devices and emulators while developing our apps.

Tip

If you are planning on using the emulator again soon, leave it running to avoid having to wait for it to start again.

If you want to try out your app on a tablet, you're going to need a different emulator.

Note

Creating a new emulator

It is simple to create an emulator for a different Android device. From the main menu, select Tools | AVD Manager. In the AVD Manager window, left-click Create New Virtual Device. Now left-click on the type of device you want to create – TV, Phone, Wear OS, or Tablet. Now simply left-click Next and follow the instructions to create your new AVD. Next time you run your app, the new AVD will appear as an option to run the app on.

Now we can look at how we can get our app onto a real device.

Running the app on a real device

The first thing to do is to visit your device manufacturer's website and obtain and install any drivers that are needed for your device and operating system.

Tip

Most newer devices won't need a driver, so you may want to just try the following steps first.

The next few steps will set up the Android device for debugging. Note that different manufacturers structure the menu options slightly differently to others. But the following sequence is probably very close, if not exact, for enabling debugging on most devices:

  1. Tap the Settings menu option or the Settings app on your phone/tablet.
  2. This next step will vary slightly for different versions of Android. The Developer options menu is hidden away so as not to trouble regular users. You must perform a slightly odd task to unlock the menu option. Tap the About device or About Phone option. Find the Build Number option and repeatedly tap it until you get a message informing you that You are now a developer!

    Tip

    Some manufacturers have different, obscure methods for achieving this step. If this step doesn't work, do a web search for your device and "unlocking developer options."

  3. Go back to the Settings menu.
  4. Tap Developer options.
  5. Tap the checkbox for USB Debugging.
  6. Connect your Android device to the USB port of your computer.
  7. Click the play icon from the Android Studio toolbar, as shown in the following screenshot:
    Running the app on a real device
  8. When prompted, click OK to run the app on your chosen device.

We are now ready to learn some Kotlin and add our own Kotlin code to the Hello World project.

You have been reading a chapter from
Android Programming with Kotlin for Beginners
Published in: Apr 2019
Publisher: Packt
ISBN-13: 9781789615401
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime