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! 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
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Android Programming with Kotlin for Beginners
Android Programming with Kotlin for Beginners

Android Programming with Kotlin for Beginners: Build Android apps starting from zero programming experience with the new Kotlin programming language

eBook
R$49.99 R$173.99
Paperback
R$217.99
Subscription
Free Trial
Renews at R$50p/m

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Table of content icon View table of contents Preview book icon Preview Book

Android Programming with Kotlin for Beginners

Chapter 2. Kotlin, XML, and the UI Designer

At this stage, we have a working Android development environment and we have built and deployed our first app. It is obvious, however, that code autogenerated by Android Studio is not going to make the next top-selling app on Google Play. We need to explore this autogenerated code so that we can begin to understand Android and then learn how to build on this useful template. With this aim in mind, we will do the following in this chapter:

  • See how to get technical feedback from our apps.
  • Examine the Kotlin code and User Interface (UI) XML code from our first app.
  • Get our first taste of using the Android UI designer.
  • Write our first Kotlin code.
  • Learn some core Kotlin fundamentals and how they relate to Android.

First, let's see how to get feedback from our apps.

Examining the log output

In the previous chapter, we mentioned that our app was running in debug mode on the emulator or real device; this is so that we can monitor it and get feedback when things go wrong. So, where is all this feedback?

You might have noticed a lot of scrolling text at the bottom of the Android Studio window. If not, click on the logcat tab, as shown by the highlighted area labeled as 1 in the following screenshot:

Tip

Note that the emulator must be running, or a real device must be attached in debugging mode, for you to see the following window. Furthermore, if you restarted Android Studio for some reason and have not yet executed the app, then the logcat window will be empty. Refer to the first chapter to get the app running on an emulator or a real device:

Examining the log output

You can drag the window to make it taller, just as you can in most other Windows applications, if you want to see more.

This window is called logcat, or, sometimes, it is referred to as console. It is our app's...

Exploring the project's Kotlin code and the main layout's XML code

We are going to look at the resource files containing the code that defines our simple UI layout and the file that has our Kotlin code. At this stage, we won't try to understand it all, as we need to learn some more basics before it makes sense to do so. What we will see, however, is the basic content and structure of both files, so we can reconcile their content with what we already know about Android resources and Kotlin.

Examining the MainActivity.kt file

Let's take a look at the Kotlin code first. You can see this code by left-clicking on the MainActivity.kt tab, as shown in the following screenshot:

Examining the MainActivity.kt file

As we are not looking at the intricate details of the code, an annotated screenshot is more useful than reproducing the actual code in text form. Regularly refer to the following screenshot while reading this section:

Examining the MainActivity.kt file

The first thing to note is that I have added a few empty lines in among the code to space...

Adding buttons to the main layout file

Here, we will add a couple of buttons to the screen and will then explore a quick way to make them do something. We will add a button in two different ways; first, using the visual designer, and second, by adding to and editing the XML code directly.

Adding a button via the visual designer

To get started adding our first button, switch back to the design view by clicking on the Design tab underneath the XML code that we have just been discussing. The button is highlighted in the following screenshot:

Adding a button via the visual designer

Notice that in the left-hand side of the layout, we have a window that is called Palette:

Adding a button via the visual designer

The palette window is divided into two parts. The left-hand list has the categories of the UI elements and allows you to select a category, while the right-hand side shows you all the available UI elements from the currently selected category.

Make sure that the Common category is selected, as shown in the previous screenshot. Now, left-click and hold on the Button widget...

Leaving comments in our Kotlin code

In programming, it is always a clever idea to write messages known as code comments, and sprinkle them liberally amongst your code. This is to remind us what we were thinking at the time we wrote the code. To do this, you simply append a double forward slash and then type your comment, as follows:

// This is a comment and it could be useful

In addition, we can use comments to comment out a line of code. Suppose we have a line of code that we temporarily want to disable; we can do so by adding two forward slashes, as follows:

// The code below used to send a message
// Log.i("info","our message here")
// But now it doesn't do anything
// And I am getting ahead of where I should be

Tip

Using comments to comment out code should only be a temporary measure. Once you have found the correct code to use, commented-out code should be cut to keep the code file clean and organized.

Let's take a look at two separate ways to send messages...

Coding messages to the user and the developer

In the introduction to this chapter and in the previous chapter, we talked a bit about using other people's code, specifically via the classes and their functions of the Android API. We saw that we could do some quite complex things with fairly insignificant amounts of code (such as talking to satellites).

To get us started on coding, we are going to use two different classes from the Android API that allow us to output messages. The first class, Log, allows us to output messages to the logcat window. The second class, Toast, is not a tasty breakfast treat, but it will produce a toast-shaped pop up message for our app's user to see.

Here is the code we need to write to send a message to logcat:

Log.i("info","our message here")

Exactly why this works will become clearer in Chapter 10, Object-Oriented Programming, but for now, we just need to know that whatever we put between the two sets of quote marks will be output...

Writing our first Kotlin code

So, we now know the code that will output to logcat or the user's screen. However, where do we put the code? To answer this question, we need to understand that the onCreate function in MainActivity.kt executes as the app is preparing to be shown to the user. So, if we put our code at the end of this function, it will run just as the user sees it; that sounds good.

Tip

We know that to execute the code in a function, we need to call it. We have wired our buttons up to call a couple of functions, such as topClick and bottomClick. Soon, we will write these functions. But who or what is calling onCreate? The answer to this mystery is that Android itself calls onCreate in response to the user clicking on the app icon to run the app. In Chapter 6, The Android Lifecycle, we will look deeper, and it will be clear what exactly the code executes and when. You don't need to completely comprehend this now; I just wanted to give you an overview of what was going...

Frequently asked questions

Q.1) Can you remind me what functions are?

A) Functions are containers for our code that can be executed (called) from other parts of our code.

Q.2) Like the first, I found this chapter tough going. Do I need to re-read it?

A) No, if you managed to build the app, you have made enough progress to handle all of the next chapter. All the blanks in our knowledge will be steadily filled in and replaced with glorious moments of realization as the book progresses.

Examining the log output


In the previous chapter, we mentioned that our app was running in debug mode on the emulator or real device; this is so that we can monitor it and get feedback when things go wrong. So, where is all this feedback?

You might have noticed a lot of scrolling text at the bottom of the Android Studio window. If not, click on the logcat tab, as shown by the highlighted area labeled as 1 in the following screenshot:

Note

Note that the emulator must be running, or a real device must be attached in debugging mode, for you to see the following window. Furthermore, if you restarted Android Studio for some reason and have not yet executed the app, then the logcat window will be empty. Refer to the first chapter to get the app running on an emulator or a real device:

You can drag the window to make it taller, just as you can in most other Windows applications, if you want to see more.

This window is called logcat, or, sometimes, it is referred to as console. It is our app's way of...

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Start your Android programming career, or just have fun publishing apps on Google Play marketplace
  • The first-principle introduction to Kotlin through Android, to start building easy-to-use apps
  • Learn by example and build four real-world apps and dozens of mini-apps

Description

Android is the most popular mobile operating system in the world and Kotlin has been declared by Google as a first-class programming language to build Android apps. With the imminent arrival of the most anticipated Android update, Android 10 (Q), this book gets you started building apps compatible with the latest version of Android. It adopts a project-style approach, where we focus on teaching the fundamentals of Android app development and the essentials of Kotlin by building three real-world apps and more than a dozen mini-apps. The book begins by giving you a strong grasp of how Kotlin and Android work together before gradually moving onto exploring the various Android APIs for building stunning apps for Android with ease. You will learn to make your apps more presentable using different layouts. You will dive deep into Kotlin programming concepts such as variables, functions, data structures, Object-Oriented code, and how to connect your Kotlin code to the UI. You will learn to add multilingual text so that your app is accessible to millions of more potential users. You will learn how animation, graphics, and sound effects work and are implemented in your Android app. By the end of the book, you will have sound knowledge about significant Kotlin programming concepts and start building your own fully featured Android apps.

Who is this book for?

This book is for people who are new to Kotlin, Android and want to develop Android apps.It also acts as a refresher for those who have some experience in programming with Android and Kotlin.

What you will learn

  • Learn how Kotlin and Android work together
  • Build a graphical drawing app using Object-Oriented Programming (OOP) principles
  • Build beautiful, practical layouts using ScrollView, RecyclerView, NavigationView, ViewPager and CardView
  • Write Kotlin code to manage an apps data using different strategies including JSON and the built-in Android SQLite database
  • Add user interaction, data captures, sound, and animation to your apps
  • Implement dialog boxes to capture input from the user
  • Build a simple database app that sorts and stores the user s data

Product Details

Country selected
Publication date, Length, Edition, Language, ISBN-13
Publication date : Apr 30, 2019
Length: 698 pages
Edition : 1st
Language : English
ISBN-13 : 9781789800883
Vendor :
Google
Category :
Languages :
Tools :

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Product feature icon AI Assistant (beta) to help accelerate your learning
OR
Modal Close icon
Payment Processing...
tick Completed

Billing Address

Product Details

Publication date : Apr 30, 2019
Length: 698 pages
Edition : 1st
Language : English
ISBN-13 : 9781789800883
Vendor :
Google
Category :
Languages :
Tools :

Packt Subscriptions

See our plans and pricing
Modal Close icon
R$50 billed monthly
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Simple pricing, no contract
R$500 billed annually
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts
R$800 billed in 18 months
Feature tick icon Unlimited access to Packt's library of 7,000+ practical books and videos
Feature tick icon Constantly refreshed with 50+ new titles a month
Feature tick icon Exclusive Early access to books as they're written
Feature tick icon Solve problems while you work with advanced search and reference features
Feature tick icon Offline reading on the mobile app
Feature tick icon Choose a DRM-free eBook or Video every month to keep
Feature tick icon PLUS own as many other DRM-free eBooks or Videos as you like for just R$25 each
Feature tick icon Exclusive print discounts

Frequently bought together


Stars icon
Total R$ 681.97
Learn Kotlin Programming
R$217.99
Android Programming with Kotlin for Beginners
R$217.99
Mastering Kotlin
R$245.99
Total R$ 681.97 Stars icon
Banner background image

Table of Contents

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

Customer reviews

Top Reviews
Rating distribution
Full star icon Full star icon Full star icon Half star icon Empty star icon 3.7
(19 Ratings)
5 star 31.6%
4 star 31.6%
3 star 21.1%
2 star 5.3%
1 star 10.5%
Filter icon Filter
Top Reviews

Filter reviews by




N/A Jan 25, 2024
Full star icon Full star icon Full star icon Full star icon Full star icon 5
very good book, i'm on chapter 4 now, keeps you motivating to go step by step
Feefo Verified review Feefo
Amazon Customer Feb 04, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
Nicely laid out and very informative. Easy to read and understand.
Amazon Verified review Amazon
Hurricane Lake Jun 10, 2020
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This book is Great!. I am joyfully engaged in it. Your detailed explanations of the workings of Android Studio and how Android Studio works with Kotlin, i.e., how the UI and functions work, and what goes where and why is greatly appreciated. Thanks for taking the time to go that deep. Your book was such an education about the underlying workings of Android Studio and the workings of Android Studio using Kotlin that I am able to tweak all examples to work on the later Android Studio version - Android Studio 4. Thanks again.
Amazon Verified review Amazon
Michael O. Jul 12, 2019
Full star icon Full star icon Full star icon Full star icon Full star icon 5
This should be the first book for fledgling developers of Android apps using Kotlin. I have read other books and had limited success in understanding key concepts. I liked Mr. Horton's conversational style and attention to detail. He purposefully doesn't burden the reader with more Kotlin language idioms than are needed for each current project. He explains concepts very well. He understands his core audience and gently addresses the limitations of beginning coders.
Amazon Verified review Amazon
GOBINDA R. Mar 18, 2021
Full star icon Full star icon Full star icon Full star icon Full star icon 5
I brought this book as kindle addition. The book writer language is very easy to understand. And the content of the book is very helpful for me, as I just started to learn the code. This book is very useful also for beginners. It covers every topic in details. So, if you are searching for a cotlin programming book, then you must give this book a try, trust me.
Amazon Verified review Amazon
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.