Discover millions of ebooks, audiobooks, and so much more with a free trial

Only $11.99/month after trial. Cancel anytime.

My First Mobile App for Students: A comprehensive guide to Android app development for beginners (English Edition)
My First Mobile App for Students: A comprehensive guide to Android app development for beginners (English Edition)
My First Mobile App for Students: A comprehensive guide to Android app development for beginners (English Edition)
Ebook644 pages3 hours

My First Mobile App for Students: A comprehensive guide to Android app development for beginners (English Edition)

Rating: 0 out of 5 stars

()

Read preview

About this ebook

In the digital age, mobile apps are the primary way for businesses and individuals to connect with their audience. Android is the leading platform, with a 71% market share worldwide and over 2.87 billion apps. If you are an aspiring app developer, this book is the perfect place to start.

The book focuses on hands-on learning, taking you through the process of transforming your ideas into reality. Starting with the basics, you will learn how to set up Android Studio and master Kotlin fundamentals. You will then build on the Android Jetpack library to create a strong architectural foundation for your apps. Along the way, you will create six fully-functional apps, complete with UI and coding logic, all powered by Kotlin.

By the end of this book, you will have the skills and knowledge you need to create innovative apps and thrive in the dynamic app development landscape.
LanguageEnglish
Release dateSep 16, 2023
ISBN9789355515940
My First Mobile App for Students: A comprehensive guide to Android app development for beginners (English Edition)

Related to My First Mobile App for Students

Related ebooks

Programming For You

View More

Related articles

Reviews for My First Mobile App for Students

Rating: 0 out of 5 stars
0 ratings

0 ratings0 reviews

What did you think?

Tap to rate

Review must be at least 10 words

    Book preview

    My First Mobile App for Students - Zaid Kamil

    C

    HAPTER

    1

    Welcome, Future App Developer

    Introduction

    Welcome, developer of the future! You are reading this chapter so that you can get an idea about the history of the Android operating system and the different versions of the Android operation system, and its impact on the mobile OS market. Then you are going to understand how to correctly install Android studio and its SDK on your pc/laptop. Finally, you will learn the language Kotlin, the official language for developing Android apps. To summarize, this chapter will provide you with a basic idea about Android, installation of Android studio, and coding in Kotlin. Concentrate on the coding portion of Kotlin, which will be very useful, as we are going to develop a lot of Android apps through each chapter. So, let us get started.

    Structure

    In this chapter, we will cover the following topics:

    History of Android OS

    History of Kotlin

    Market impact of Mobile OSes

    Setting up Android Studio

    Android SDK setup

    Kotlin classes and objects

    Functions and variables

    Condition and loops

    Objectives

    By the end of this chapter, the reader will know about the Android OS history, the market impact of Android, how to setup Android Studio IDE, and setup the software for developing Android apps. Finally, the reader will learn about Kotlin language and its main concepts, such as classes and objects, functions, conditions, and loops.

    History of Android OS

    It has been more than a decade since the first Android phone came out, and with it came the Android operating system. However, the real beginning of Android was in the year 2003, well before Apple iOS. Android Inc was founded by Andy Rubin, Rich Miner, Nick Sears and Chris White, and their aim was to develop smarter mobile devices that are more aware of its owner’s location and preferences. Originally, the Android OS was meant to improve digital cameras but due to declining digital camera market, Android Inc, changed their target to mobile phones. The biggest change, however, came in 2005, when Google bought the Android Inc. company.

    All the founding members continued developing the OS under the ownership of Google. Google made Android an open-source operating system that allowed it to become highly popular with third-party phone manufacturers. In September 2008, the first Android smartphone was released: T-Mobile G1, or the HTC Dream, with Android 1.0, and the rest became history. Over the years, various Android versions were released, and every major Android OS update had a special public name based on deserts. Following is the list of major Android versions and what they bring to the table:

    Table 1.1: Android versions over the years

    As you can see, Google continues to develop Android, adding new features and fixing problems and bugs with each new release.

    History of Kotlin

    Google, in 2019, made the Kotlin programming language an official language for Android app development. They made Kotlin the preferred language for app development. So, Kotlin is now the third language that can be used to develop Android apps after Java and C++.

    Kotlin was developed by JetBrains in the year 2010, and it was released under the name of Project Kotlin in the year 2011. The first official version of Kotlin was released in 2016. The current version of Kotlin is 1.7, published in 2022.

    The language is a cross-platform, statically typed, and general-purpose programming language that is fully interoperable with Java. This is beneficial because Java and Kotlin both run inside Java Virtual Machine (JVM).

    There are many benefits of choosing Kotlin over Java when doing Android App development; a few of them are listed as follows, but they will be detailed in the upcoming chapters:

    Concise code: As a rough estimate, code is 40% less than similar Java code.

    Type safety: Kotlin supports non-nullable types, which make apps less prone to the dreaded Null Pointer Exception (NPE) (a runtime error that crashes software/apps when a variable that is used is null) known to every Java developer.

    Smart casting: A feature that reduces code lines by allowing Kotlin to track conditions inside an expression.

    Extension functions: A Kotlin feature that helps to add functionality to a predefined class with the sub-classing technique that is used in Java language.

    Higher-order functions: Kotlin allows functions to accept other functions as parameters.

    Lambdas with receivers: Another Kotlin feature that makes code concise and makes it more readable and understandable.

    So, looking at the preceding list of jargon, you might start to think that Kotlin would be hard to learn. But it is not. Instead, Kotlin is inspired by existing languages such as Java, C#, JavaScript, Scala, Groovy, and Python, and by doing so, the JetBrains developer cherry picked the best features from each language while skipping the problems seen in the other languages. Kotlin is easy to learn, and so, be it newcomers or experienced, both types of coders can learn in a matter of days. A few advanced features may take some time, but overall, Kotlin is not a complicated language. You can check the official website https://kotlinlang.org/ to know more about Kotlin.

    Market impact of Mobile operating systems

    The Android OS left a significant impact on the market because of its dominance as the leading mobile OS worldwide. Its open-source nature encouraged diverse device offerings and a vast app ecosystem. This spurred economic growth, global accessibility, and healthy competition. Android’s integration with Google services bolstered Google’s online dominance, whereas its affordability expanded internet access and digital services. Overall, Android’s influence spans hardware innovation, app development, and increased digital inclusion.

    When it comes to the market share worldwide of the mobile operating system, Android has been dominating the market since 2012, as the highest third-party smartphone manufacturers tend to select Android as their operating system.

    The latest report from statista.com tells us that Google’s Android is leading the market with approximately 71%, with Apple’s iOS at second position with 28.3 %, and in third place is KaiOS, a special operating system for feature phones with 0.07%. Refer to Figure 1.1 for an illustration of the Worldwide Mobile OS Market share:

    Figure 1.1: Word wide mobile OS market share

    Setting up Android Studio

    To begin developing Android applications on your pc, you have to install and setup only one software, which is the officially supported Android Integrated Development Environment (IDE) and the Android Studio. The Android Studio is developed by the JetBrains company, specifically for developing Android apps easily, and is also free to use.

    It provides benefits such as follows:

    A single environment where you can develop for all Android devices.

    A fast and feature-rich emulator.

    A Gradle build system.

    Testing tools.

    Intelligent code editor.

    Integration with Play Store, and much more.

    You can download Android Studio from https://developer.android.com/studio, Android Studio can be installed on Microsoft Windows, MacOS, Linux, and Chrome OS. You can install it in a few simple clicks. Refer to the Figure 1.2:

    Figure 1.2: Installing Android studio

    Before installation, make sure the internet is available and you are connected.

    Note: For optimal performance, ensure your system meets Android Studio’s recommended hardware requirements: at least 8 GB of RAM, a strong processor, and an SSD for storage.

    Figure 1.3 features the various download options for the installation of Android Studio:

    Figure 1.3: Download options for Android Studio installation

    Windows installation

    To install Android Studio on Windows, download the .exe file and double-click to launch it. Follow the setup wizard in Android studio and install the recommended SDK packages. After installing, press Finish to launch the Android studio. A dialog might popup asking for importing previous settings (not for first-time installation) or not; select and click OK.

    Mac installation

    To install Android studio on your Mac, proceed with downloading the Android Studio DMG file. After downloading, drag and drop the file into the Applications folder, then launch Android Studio. Select whether to import the previous settings (not for first time installation) or not, then click OK. The setup wizard guides you through the rest of the setup, and then it will download the Android SDK components required for development. On macOS Mojave or later, you might see a popup to allow calendar, photos, and contacts.

    Linux installation

    To install Android studio on a Linux OS, you will first have to download the suggested ZIP file to your system. Extract the ZIP file to an appropriate location such as /usr/local/ or /opt/. To launch Android Studio, open the Terminal, navigate to the directory where you extracted Android studio and then go install android-studio/bin directory and run the studio.sh file. Select whether to import previous Android studio settings or not, and then click OK. After this, the Android studio wizard will start and guide you through the rest of the setup. Proceed with the download on Android SDK components that are required.

    To get more information about the installation of Android Studio on various OS, make sure to visit the official documentation at: https://developer.android.com/studio/install.

    After the successful completion of installation, you will see the Android studio launch screen. Your Android studio version might vary, as the Android studio gets regular updates and new versions.

    Note: As a developer, you should always update to the most stable version of Android studio to get the best feature set available.

    Android SDK setup

    You might be thinking that now you can try making some apps. However, before that, it is suggested that you should update the SDK setup to the latest stable Android API version and some other things:

    Click on the rightmost menu icon ⋮ (vertical three dots) for more actions. Then select the SDK manager option. Refer to Figure 1.4:

    Figure 1.4: The Android SDK manager window

    Select the most stable version of Android, which might be at the top of the list. Check if the latest SDK and sources are available, and if not, check the box. Also, do not check every checkbox because each checkbox heading is an Android version, and as a developer, you should use the most recent and stable version of the Android. Remember that you should not download preview versions of Android SDK, as they are unstable and not intended for new developers. Refer to Figure 1.5:

    Figure 1.5: Configuring the Android SDK and SDK tools

    Now go to the SDK tools tab, where you might need to check the checkbox to download or update Android SDK build tools. A download icon will be displayed on the checked checkbox. Select the Google Play services and Google USB driver; they will be very useful later while developing and installing apps. Refer to Figure 1.6:

    Figure 1.6: SDK installation progress window.

    Finally, click on the OK button (blue color) at the bottom of the screen. This will launch a dialog asking you to confirm the download process. Another window will popup showing the download and install progress. Wait for some time to complete the installation. Click Finish to complete. Refer to Figure 1.7:

    Figure 1.7: Confirmation dialog for confirming the changes made in Android SDK

    After everything is done, your Android studio is now ready for work. However, we will create your first app in the next chapter. First, you will need to learn the basics of programming language for development, also known as Kotlin.

    Kotlin classes and objects

    The only way to learn a new language is to jump directly into coding, and we are going to do just that. For this chapter, you are going to code in the Web browser using the online compiler for Kotlin at https://play.kotlinlang.org/, which is a free-to-use Kotlin testing environment.

    When you first visit the website, you will be greeted by a sample code. You can run this code by clicking the purple Run button in the top right corner. Refer to Figure 1.8:

    Figure 1.8: Sample code on Kotlin testing environment

    A light gray area with a popup at the bottom will contain the output of your code or an error in red color, as shown in Figure 1.9:

    Figure 1.9: Output area on Kotlin testing environment

    You can see the error message with an exclamation icon !, and also an exclamation icon in the code area, showing in which line you have the error, as shown in Figure 1.10. For example, the error message is saying that it is expecting a closing bracket ) in line 3 of the code, and you can see that we missed the bracket at the end of the line. Try making and fixing errors in this little code and understanding error messages; this will help you get better at fixing them later:

    Figure 1.10: Error display in Kotlin testing environment

    Kotlin is a functional as well as object-oriented language, meaning you can write code in two ways in Kotlin:

    Write code in a function and use them as you want.

    Write code in classes and then create objects to use.

    We are going to develop Android apps, and thus, we are going to focus more on the second style of code, which is generally called the Object-Oriented Principal (OOP). There are various concepts related to OOP, which we are going to tackle as they come, with proper explanations. We could give you technical definitions of each concept, but that will not do much good. So, let us go over examples for these concepts and then some practical code so that you can use your imagination to visualize and understand the concepts better.

    Class in Kotlin

    In OOP, a class is the core concept upon which other concepts rely. To understand, let us consider an example. Think of the word cat: close your eyes and think about the cat, you might visualize a pet cat, a stray cat, a furry cat or Tom or Oggy, or some thought related to cats.

    But the word that was given was actually a cat and not some specific character like Oggy or Tom. This is because the word cat here represents all the cats that you might have seen or not seen; all the cats in the world. Similarly, think of a pen, mobile phone, book, dog, vegetable, fruit, or anything that, in general, denotes multiple items.

    So, back to the cat, and to make a point here, a cat denotes a small domesticated carnivorous mammal with soft fur, a short snout, and retractable claws. It is widely kept as a pet or for catching mice, and many breeds have been developed. A cat would have some properties and behavior.

    In terms of programming language, we can say cat is a class. And the cat class will have some properties and methods (behaviors). Let us see a Kotlin-style example for the so-called cat class.

    Refer to Figure 1.11. Here, we have four lines of code. The first two lines are important here, so let us break it down:

    Figure 1.11: A Sample class with the constructor

    Class is a keyword in Kotlin. Keywords are words in programming language that have a predefined meaning. Here, the class keyword is written before the word Cat, telling Kotlin to make a class named Cat.

    Cat is the name of the class we are going to create, which will be used to make objects later. The class name should be written in UpperCamelCase. Moreover, we cannot provide spaces or special characters in the classname. Classname can be anything.

    After that, we opened and closed round brackets and wrote some content inside it. That is called the class constructor, which helps in creating the object and setting the values of properties. This is explained by examples in the object part.

    Inside the constructor, you will see two elements separated by a comma. These are called properties/fields/attributes. val is a keyword, telling Kotlin that breed is a property whose value will not change when a value is set.

    breed:String informs us about two things: one breed is the name of the property, and second, the value of the breed will be textual, aka string.

    val age: Int is saying that age is a property whose value is numeric (integer), and it will not change because it is declared using val keyword.

    The opening and closing braces inform Kotlin where the class starts and where it ends.

    We can add behaviors (technically methods or functions) inside the area curly braces, and then those things will be part of the Cat class.

    Finally, there are the last two lines of code where we can use our Cat class to create objects.

    Object in Kotlin

    To understand objects, you need to have a little idea about variables. A variable can be simply understood as a container inside a computer’s memory, where we can put data for later use. It is better understood in terms of math, like X = 15, meaning variable X now holds 15 as the value, and whenever you are going to use X after the line, it is going to give you 15. Similarly, objects are special types of variables that can hold more complex information in a better way.

    Let us now refer to Figure 1.12. When you update the code and add the lines as displayed, and run the code finally, you will see a similar output. Let us break it down:

    Figure 1.12: Creating objects of a class

    The val snowbell is a variable that will store data and will not change its values while the program is running.

    val snowbell = Cat(persian, 4) informs Kotlin that the variable snowbell is actually going to store information about a Cat, which is literally a Persian cat with four years of age. Cat is the name of the class we created in the example accompanying Figure 1.12and so it is valid. Nonetheless, try Dog(husky, 5) after equals to and you get an error, because you have not written any code for Dog class.

    The variable snowbell will be called an object because it is using a class to get the information it will hold.

    The same thing follows about Tom (an object) of the Cat class, but with a different breed and age.

    println() is a function that is used to output the values of variables, string messages, or objects.

    We can see two weird outputs that actually represent snowbell and tom objects.

    Let us make the output cleaner by accessing the data held by snowbell and tom for breed and age.

    Refer to Figure 1.13:

    Figure 1.13: Accessing class properties via objects

    Objects created allow us access to properties, but each object has different values in each property. Thus, when we use tom or snowbell, we are talking specifically about tom or snowbell, and thus, it is an object, not a class.

    tom.breed tells Kotlin to load the tom object and get the breed of tom.

    Let us take a look at another simple class and object, and your task is to understand what is happening. We did not add the curly braces in the class Person because if there is no code inside the class, we can skip them.

    Try the following code shown in Figure 1.14, by removing all the previous code:

    Figure 1.14: Another class and object example

    Functions and variables in Kotlin

    Classes

    Enjoying the preview?
    Page 1 of 1