Book Image

Learning Kotlin by building Android Applications

By : Eunice Adutwumwaa Obugyei, Natarajan Raman
Book Image

Learning Kotlin by building Android Applications

By: Eunice Adutwumwaa Obugyei, Natarajan Raman

Overview of this book

<p>Today Kotlin is an official programming language for Android development and is widely adopted. Kotlin is expressive, concise, and powerful. It also ensures seamless interoperability with existing Android languages like JAVA and C++, which means that it's even easier for developers to use.</p> <p>This book adopts a project-style approach, where we focus on teaching Android development by building three different Android Application: a Tic-Tac-Toe application, a location- based alarm and a To-Do list application.</p> <p>The book begins by giving you a strong grasp of the Kotlin language and its APIs as a preliminary to building stunning applications for Android. You'll learn to set up an environment and as you progress through the chapters and the building of the different applications, the difficulty level will steadily grow.</p> <p>The book also introduces you to the Android Studio IDE, which plays an integral role in Android Development. It covers Kotlin's basic programming concepts such as functions, lambdas, properties, object-oriented code, safety aspects and type parameterization, testing, and concurrency, and helps you write Kotlin code to production.</p> <p>Finally, you'll be taken through the process of releasing your app on the Google Play Store. You will also be introduced to other app distribution channels such as Amazon App Store.</p> <p>As a bonus chapter, you will also learn how to use the Google Faces API to detect faces and add fun functionalities.</p>
Table of Contents (21 chapters)
Title Page
Packt Upsell
Contributors
Preface
Index

Understanding the code


We could successfully run the app, and now it is time to delve deeper into the code and understand how it works.

Let's start with the MapsActivity.kt Kotlin class file.

The MapActivity class extends the AppCompatActivity class and also implements the OnMapReadCallback interface. We have a couple of variables, GoogleMap, mMap and btnbutton initialized.

Overriding the onCreate method, the app is set to load the content from the XML file, activity_maps.xml, as and when the app is launched.

The resources for the mapFragment and btn are set from the resources file:

class MapsActivity : AppCompatActivity(), OnMapReadyCallback {

private lateinit var mMap: GoogleMap

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
         setContentView(R.layout.activity_maps)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
   ...