Mobile Application Development (InTe3114) Lecture Notes PDF
Document Details
Uploaded by Deleted User
Hawassa University
Areg. T.
Tags
Summary
These lecture notes cover mobile application development, focusing on Android. They provide an overview of Android development concepts and architectures, discussing the different types of mobile apps, components, and the Android build process. The document also outlines important terminology associated with mobile app development.
Full Transcript
HAWASSA UNIVERSITY Institute of Technology (IOT) Faculty of Informatics Mobile Application Development (InTe3114) Compile by: Areg. T. 1 Mobile Application Development Chapter_01: Intro...
HAWASSA UNIVERSITY Institute of Technology (IOT) Faculty of Informatics Mobile Application Development (InTe3114) Compile by: Areg. T. 1 Mobile Application Development Chapter_01: Introduction Compile by: Areg. T. 2 Outline Introduction to Android Android Application Structure Android UI Architecture [Application context, intent, activity lifecycle] UI Components [Text control…] Compile by: Areg. T. 3 Introduction to Android What is Android? Android is an open-source and Linux-based operating system designed with mobile in mind. But can also be used for tablet computers, wear OS, Television, and Automative. Android is the world’s most popular mobile platform. At the last count, there were over 3 billion active Android devices worldwide, and that number is growing rapidly. Compile by: Areg. T. 4 Introduction to Android What is Android? It is a powerful development framework that includes everything you need to build great Android apps using various languages (like a mix of Java/Kotlin and XML). Android is developed by a consortium of developers known as the OHA (Open Handset Alliance) and commercially sponsored by Google. Compile by: Areg. T. 5 Introduction to Android What is Android? Android is NOT a phone, or an application, or a programming language, but it is an open-source operating system based on the Linux kernel. Compile by: Areg. T. 6 Android History In 2003, Android Inc. and started the development of the Android operating system. Later, in 2005, Google purchased it. On November 5, 2007, Google released the beta version of Android operating system. Compile by: Areg. T. 7 Android History Later, on November 12, 2007, the Android software development kit (SDK) was released. Again, in October 2008, the first Android mobile was publicly released. This mobile was released with Android 1.0 version. Compile by: Areg. T. 8 Android Versions, Code Names, and API Levels Version Numbers Code Name Release Date API Level Android 1.0 No code name September 23, 2008 1 1.1 >> February 9, 2009 2 1.5 Cupcake April 27, 2009 3 1.6 Donut September 15, 2009 4 2.0 – 2.1 Éclair October 26, 2009 5–7 2.2 – 2.2.3 Froyo May 20, 2010 8 2.3 – 2.3.7 Gingerbread December 6, 2010 9 – 10 3.0 – 3.2.6 Honeycomb February 22, 2011 11 – 13 4.0 – 4.0.4 Ice Cream Sandwich October 18, 2011 14 – 15 4.1 – 4.3.1 Jelly Bean July 9, 2012 16 – 18 4.4 – 4.4.4 KitKat October 31, 2013 19 – 20 5.0 – 5.1.1 Lollipop November 12, 2014 21 – 22 Compile by: Areg. T. 9 Android Versions, Code Names, and API Levels Version Numbers Code Name Release Date API Level 6.0 – 6.0.1 Marshmallow October 5, 2015 23 7 Nougat August 22, 2016 24 7.1.0 – 7.1.2 >> October 4, 2016 25 8 Oreo August 21, 2017 26 8.1 >> December 5, 2017 27 9 Pie August 6, 2018 28 10 Quince Tart September 3, 2019 29 11 Red Velvet Cake 2020 30 12 – 12L Snow Cone 2021 31 – 32 13 Tiramisu Aug 15, 2022 33 14 Upside Down Cake February 2023 34 15 Vanilla Ice Cream TBD 35 Compile by: Areg. T. 10 Types of Mobile Apps Mobile applications are of three main types: Native Apps, Web Apps, and Hybrid Apps. There are three main options for creating a mobile app. You can create a native, hybrid, or web app (also called a browser-based app). Compile by: Areg. T. 11 Types of Mobile Apps Native Apps A native app only runs on a specific mobile operating system. It won’t run on other mobile operating systems. Native apps are “native” to one platform – you’ll need to create different apps for Android, iOS, etc. You can distribute native apps through play store/app store and install them on devices. Native development environments include: for Android (Java/Kotlin with XML), and for iOS (swift/Objective-C). Compile by: Areg. T. 12 Types of Mobile Apps Native Apps However, this comes with a trade-off. If you want your app to run on Android and iOS, you will have to develop the app twice, once for each OS. This can make the development process both slower and more expensive. Compile by: Areg. T. 13 Types of Mobile Apps Hybrid Apps Hybrid ones work on any device regardless of the operating system. You can distribute hybrid apps through play store/app store and install them on devices. Hybrid development environments include: Ionic, React Native, Xamarin. Compile by: Areg. T. 14 Types of Mobile Apps Web Apps A web app or browser-based app can deliver similar functionality to an app as a website. On the other hand, web apps operate in browsers, and you can’t distribute the app through a play store/app store and install them on devices. Web app’s development language includes: web technologies (like HTML, CSS, and JavaScript). Compile by: Areg. T. 15 Types of Mobile Apps Compile by: Areg. T. 16 Setup the Environment In order to Setup the Environment that we are working on, we need to install the followings: 1. Java Development Kit (JDK) : Download and install JDK from oracle’s official website: https://www.oracle.com/in/java/technologies/downloads/ 2. Download and install Android studio from the android developer's official website: https://developer.android.com/studio. This software suite is an IDE which includes both the Java JDK and Android SDK. Compile by: Areg. T. 17 Creating a New Android App (Project) Compile by: Areg. T. 18 Creating a New Android App (Project) Compile by: Areg. T. 19 Creating a New Android App (Project) Compile by: Areg. T. 20 Creating a New Android App (Project) Compile by: Areg. T. 21 Creating a New Android App (Project) Compile by: Areg. T. 22 Creating a New Android App (Project) Compile by: Areg. T. 23 Android Studio Anatomy Compile by: Areg. T. 24 Android Application Structure The app Folder: ✓ manifest ✓ java ✓ res The Gradle Folder: ✓ build.gradle (Project level) ✓ build.gradle (Module level) Compile by: Areg. T. 25 Android App – Different XML Files Android Manifest XML file: Every Android app project must have an AndroidManifest.xml file (with precisely that name) at the root of the project source set. The manifest file defines essential information about your app to the Android build tools, Android operating system, and Google Play. E.g. package name, activities, permissions, etc. Compile by: Areg. T. 26 Android App – Different XML Files Layout XML file: A layout resource defines the architecture for the UI in an Activity or a component of UI. File location: res/layout/filename.xml, where the filename will be used as the resource ID. Resource reference: In Java: R.layout.filename In XML: @[package:]layout/filename Compile by: Areg. T. 27 Android App – Different XML Files Strings XML file: A single string that can be referenced from the application or from other resource files ( such as XML layout). This xml file is used to replace the Hard-coded strings with a single string. This file enhances the reusability of the code. Note: A string is a simple resource that is referenced using the value provided in the name attribute. So, we can combine string resources with other simple resources in one XML file, under one element. Compile by: Areg. T. 28 Android App – Different XML Files File location: res/values/filename.xml, where the filename is arbitrary. The element’s name will be used as the resource ID. Resource reference: In Java: R.string.string_name In XML: @string/string_name Compile by: Areg. T. 29 Android App – Different XML Files Styles XML file: A style resource defines the format and look for a UI. A style can be applied to an individual View (from within a layout file) or to an entire Activity or application (from within the manifest file). A style is a simple resource that is referenced using the value provided in the name attribute. As such, you can combine style resources with other simple resources in one XML file, under one element. Compile by: Areg. T. 30 Android App – Different XML Files File location: res/values/filename.xml, where the filename is arbitrary. The element’s name will be used as the resource ID. Resource reference: In XML: @[package:]style/style_name Compile by: Areg. T. 31 Android App – Different XML Files Color XML file: The color resource is used to define the color value that we used in our application. We can use a color resource any place that accepts a hexadecimal color value. Note: A color is a simple resource that is referenced using the value provided in the name attribute. As such, you can combine color resources with other simple resources in one XML file, under one element. Compile by: Areg. T. 32 Android App – Different XML Files File location: res/values/colors.xml, where the filename is arbitrary. The element’s name will be used as the resource ID. Resource reference: In Java: R.color.color_name In XML: @[package:]color/color_name Compile by: Areg. T. 33 Android System Architecture Figure 1.1: Android System Architecture Compile by: Areg. T. 34 Android System Architecture Compile by: Areg. T. 35 Android System Architecture Application Layer Android comes with a set of core apps (system apps) for email, SMS messaging, calendars, internet browsing, contacts, and more. Apps included with the platform have NO special status among the apps the user chooses to install. So, a third-party app can become the user’s default web browser, default SMS messenger, or even the default keyboard. Compile by: Areg. T. 36 Android System Architecture Application Programming Interface (API) Layer The API framework layer is what developers use to build Android apps. This layer provides all the necessary tools and APIs to create, manage, and deploy apps. Example: when developing a messaging app, the developer might use the API framework to create the UI, manage message storage with SQLite, and send notifications when a new message is received. Compile by: Areg. T. 37 Android System Architecture Android’s Middle Layer Native Libraries: are compiled libraries written in C and C++ languages that provide core functionalities to the Android system, including graphics rendering, media playback, networking, and more. These libraries are accessible through the Android Application Framework. Example: OpenGL ES library uses to render its 3D graphics, or Media Framework handles the background music and sound effects. Compile by: Areg. T. 38 Android System Architecture Android’s Middle Layer Android Runtime (ART): is the environment where Android applications run. It replaces the older Dalvik runtime (DRT) and offers several improvements, particularly in performance. ART uses a compilation technique called Ahead-of-Time (AOT) that compiles apps into native code at the time of installation, rather than during execution or launching. This leads to faster app startup times and more efficient memory and battery usage. Compile by: Areg. T. 39 Android System Architecture Android’s Middle Layer Prior to Android 5.0, Android used the DVM for running applications. DVM used Just-in-Time (JIT) compilation to convert bytecode into machine code during runtime. Starting from Android 5.0, Android transitioned to the ART runtime, which replaced the DVM. Example: when you install an app, ART compiles the app’s bytecode into machine code, making it ready for execution. Compile by: Areg. T. 40 Android System Architecture Hardware Abstraction Layer (HAL) The HAL provides standard interfaces that expose device hardware capabilities to the higher-lever API framework. The HAL consists of multiple library modules, each of which implements an interface for a specific type of hardware component, such as the camera or Bluetooth module. When a framework API makes a call to access device hardware, the Android system loads the library module for that hardware component. Compile by: Areg. T. 41 Android System Architecture Linux Kernel Layer The Linux Kernel is the foundation of the Android architecture or platform. It’s responsible for managing the core system services, including, security management, process management, memory management, and device drivers. It manages all the drivers needed during the runtime of an Android device, such as camera drivers, display drivers, audio drivers, Wi-Fi drivers, Bluetooth drivers, and memory drivers, among others. Compile by: Areg. T. 42 Android Build Process Compile by: Areg. T. 43 Android Application File (APK file) APK stands for… Android Package Kit, or Android Package, or Android Application Package Compile by: Areg. T. 44 Android App Architecture As Android apps grow in size, it is important to define an architecture that allows the app to scale, increases the app’s robustness, and makes the app easier to test. Considering the common architectural principles, each application should have at least two layers. The UI layer that displays application data to the screen. The data layer that contains the business logic of your app and exposes application data. Compile by: Areg. T. 45 Android App Architecture You can add additional layer called the domain layer to simplify and reuse the interactions between the UI and data layers. Figure 1.2: Android Application Architecture Compile by: Areg. T. 46 Android App Architecture The domain layer is optional because not all apps will have these requirements. You can use it only when needed – for example, to handle complexity or favor reusability. Compile by: Areg. T. 47 Important Android Terminology Developing Android applications for mobile devices with limited resources requires a detailed understanding of the application lifecycle, and how Android applications function and interact with one another. Android uses its own terminology for these application building blocks – terms such as context, activity, and intent. Compile by: Areg. T. 48 Important Android Terminology Context Context is an interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. Compile by: Areg. T. 49 Important Android Terminology Context Context is a layer (interface) which stands behind its component (Activity, Application) and component’s lifecycle, which provides access to various functionalities which are supported by application environment and Android framework. There are mainly two types of context: which are Application context and Activity context. Compile by: Areg. T. 50 Important Android Terminology Application Context In application context, the context is tied to application and its lifecycle. Returns the context which is linked to application which holds all activities running inside it. The function for retrieving an application context is: getApplicationContext(). We can think of it as a layer behind the whole application. As long as user doesn’t kill the application, it’s alive. Compile by: Areg. T. 51 Important Android Terminology Activity Context In Activity context, the context is tied to an Activity and its lifecycle. Returns the context which is linked to the Activity from which it is called. The function for retrieving an activity context is: getContext() or sometimes we can use “this” keyword (refers to the current instance of a class). We can think of it as a layer which stands behind activity and it will live as long as activity lives. The moment the Activity dies, the Context will too. Compile by: Areg. T. 52 Important Android Terminology Context In general, Context can… Application Context Activity Context Show a Dialog NO YES Start an Activity NO YES Layout Inflation NO YES Start a Service YES YES Bind to a Service YES YES Send a Broadcast YES YES Register BrodcastReceiver YES YES Load Resources Values YES YES Compile by: Areg. T. 53 Important Android Terminology Intent An intent is a message sent to the system or another application to request that an action be performed. Basically, an intent can be used to start an activity, start a service or send a broadcast. Starting an activity: An activity represents a single screen in an application. Compile by: Areg. T. 54 Important Android Terminology Intent You can start a new instance of an Activity by passing an Intent to startActivity(). The Intent describes the activity to start and carries any necessary data. There are two types of Intent. Explicit Intent and Implicit Intent. Compile by: Areg. T. 55 Important Android Terminology Explicit Intent Specify which application will satisfy the intent, by supplying either the target app’s package name or a fully-qualified component class name. Implicit Intent Don’t name a specific component, but instead declare a general action to perform, which allows a component from another app to handle it. Compile by: Areg. T. 56 Important Android Terminology Activity An activity is a window containing user interface (UI) components that the user can interact with. An activity provides the window in which the application draws its UI (user interface). Starting an activity often means displaying a window. An activity is implemented as a subclass of the Activity Java class. Compile by: Areg. T. 57 Important Android Terminology Activity The first window that the application creates is called the main activity and serves as the entry point to the application. An Android application may contain multiple activities and you specify the main activity by declaring it in the application manifest file. Compile by: Areg. T. 58 Activity Lifecycle Over a course of its lifetime, an activity goes through a number of states. To navigate transitions between stages of the activity lifecycle, the Activity class provides a core set of 7 callback methods like onCreate(), onStart(), onPause(), onRestart(), onResume(), onStop(), and onDestroy() to handle transitions between states. The system invokes each of these callbacks as an activity enters a new state. Compile by: Areg. T. 59 Activity Lifecycle Figure 1.3: Activity Lifecycle Compile by: Areg. T. 60 Activity Lifecycle Depending on the complexity of your activity, you probably don’t need to implement all the lifecycle methods. However, it’s important that you understand each one and implement these that ensure your app behaves the way users expect. In general, the movement or transition through an activity’s lifecycle looks like in the table below: Compile by: Areg. T. 61 Activity Lifecycle Method Description onCreate() o Called / Invoked when the activity if first created. o In this method, perform basic application startup logic (like create views) that happens only once for the entire life of the activity. o This method also provides you with a Bundle containing the activity’s previously frozen state, if there was one. onStart() o Called / Invoked when the activity is becoming visible to the user. onResume() o Called / Invoked when the activity will start interacting with the user. o At this point your activity is at the top of its activity stack, with user input going to it. Compile by: Areg. T. 62 Activity Lifecycle Method Description onPause() o Called / Invoked when the activity loses foreground state, is no longer focusable or before transition to stopped/hidden or destroyed state. o The activity is still visible to user, so it’s recommended to keep it visually active and continue updating the UI. o Implementations of this method must be very quick because the next activity will not be resumed until this method returns. onStop() o Called / Invoked when the activity is no longer visible to the user. o This may happen either because a new activity is being started on top, or an existing one is being brought in front of this one, or this one is being about to be destroyed. o This is typically used to stop animations and refreshing the UI, etc. Compile by: Areg. T. 63 Activity Lifecycle Method Description onRestart() o Called / Invoked after your activity has been stopped, prior to it being started again. onDestroy() o The final call you receive before your activity is exited. o The system invokes this callback for one of two reasons: o (1) the activity is finishing, due to the user completely dismissing the activity or due to finish() being called on the activity. o (2) the system is temporarily destroying the activity due to a configuration change, such as device rotation in (portrait or landscape) etc. Compile by: Areg. T. 64 Brainstorming Question 1. Identify the type of Views? Compile by: Areg. T. 65 Brainstorming Question 2. Identify the type of Views? Compile by: Areg. T. 66 Brainstorming Question 3. Discuss about Views and View Groups? 4 “Hiking” TextView and “We hiked up…” TextView Compile by: Areg. T. 67 UI Components Android offers a sophisticated and powerful componentized model for building your UI, based on the fundamental layout classes: View and ViewGroup. To start with, the platform includes a variety of prebuilt View and ViewGroup subclasses – called widgets and layouts, respectively – that you can use to construct your UI. In android UI components are the interactive or View components that are used to design the user interface of an application. Compile by: Areg. T. 68 UI Components The figure below shows the pictorial representation of some popular ones of UI components in android application. Compile by: Areg. T. 69 UI Components 1. EditText: A user interface element for entering and modifying text. When you define an edit text widget, you must specify the input type attribute. For example, for plain text input set inputType to “text”. 2. SeekBar: A SeekBar is an extension of ProgressBar that adds a draggable thumb. The user can touch the thumb and drag left to right to set the current progress level or use the arrow keys. Compile by: Areg. T. 70 UI Components 3. CheckBox: A checkbox is a specific type of two-states button that can be either checked or unchecked. Checkboxes allow the user to select one or more options from a set. 4. RadioGroup of RadioButton: A radio button is a two-states button that can be either checked or unchecked. When the radio button is unchecked, the user can press or click it to check it. However, contrary to a check box, a radio button cannot be unchecked by the user once checked. Radio buttons are normally used together in a radio group. Compile by: Areg. T. 71 UI Components 5. Switch: A switch is a two-state toggle widget. Users can drag the switch “thumb” back and forth to select either of two options or simply tap the switch to toggle between options. 6. Spinner: A view that displays one child at a time and lets the user pick among them. In the default state, a spinner shows its currently selected value. Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one. Compile by: Areg. T. 72 UI Components 7. Button: A button is a UI element that the user can tap or click to perform an action. To specify an action when the button is pressed, set a click listener on the button object in the corresponding activity code: 8. TextView: A text view is a user interface element that displays text to the user. Compile by: Areg. T. 73 UI Components 7. ImageView: An image view class/component is used to display an image file in to the application. 8. RatingBar: A rating bar is used to get the rating from the application user. A user can simply touch, drag, or click on the stars to set the rating value. Compile by: Areg. T. 74 UI Components Generally, in android the user interface of an app is made with a collection of View and ViewGroup objects. The View is a base class for all UI components in android and it is used to create interactive UI components such as TextView, EditText, Checkbox, RadioButton, etc. and it is responsible for event handling and drawing. The ViewGroup is a subclass of View and it will act as a base class for layouts and layout parameters. Compile by: Areg. T. 75 UI Components The ViewGroup will provide invisible containers to hold other Views or ViewGroups and to define the layout properties. In android, we can define a UI components in two ways, those are: Declare UI elements in XML Create UI elements at runtime (using Java code) The android framework will allow us to use either or both of these methods to define our application’s UI. Compile by: Areg. T. 76 Layouts Layouts are important because they directly affect the look and feel of your application. Technically, a layout is a view that arranges child views added to it. Android comes with a number of build-in layouts, ranging from Linear layout, which is the easiest to use, to Constraint layout, which is the most powerful. Compile by: Areg. T. 77 Layouts An important Android component, a layout defines the visual structure of your UI components. A layout is a subclass of android.view.ViewGroup, which in turn derives from android.view.View. A ViewGroup is a special view that can contain other views. A layout can be declared in a layout file or added programmatically at runtime. Compile by: Areg. T. 78 Layouts The following are some of the layouts in Android: Linear layout Relative layout Constraint layout Compile by: Areg. T. 79 Layouts Linear Layout: is a layout that arranges its children either horizontally or vertically, depending on the value of its orientation property. The linear layout is the easiest layout to use. The layout in the figure below, is an examples of linear layout with horizontal orientation and vertical orientation respectively. It contains three children, an ImageButton, a TextView and a Button. Compile by: Areg. T. 80 Layouts Compile by: Areg. T. 81 Layouts Compile by: Areg. T. 82 Layouts Note that each view in a layout can have a layout_gravity attribute to determine its position within its axis. For instance, setting the layout_gravity attribute to center will center it. A linear layout can also have a gravity attribute that affects its gravity. Compile by: Areg. T. 83 Layouts Relative Layout: A layout where the positions of the children can be described in relation to each other or to the parent. Relative layout is a view group that displays child views in relative positions. The position of each view can be specified as relative to sibling elements (such as to the left-of or below another view) or in positions relative to the parent relative layout area (such as aligned to the bottom, left or center). Compile by: Areg. T. 84 Layouts Relative layout lets child views specify their position relative to the parent view or to each other (specified by ID). So you can align two elements by right border, or make one below another, centered in the screen, centered left, and so on. By default, all child views are drawn at the top-left of the layout, so you must define the position of each view using the various layout properties. Compile by: Areg. T. 85 Layouts Some of the many layout properties available to views in a relative layout include: android:layout_alignParentTop If “true”, makes the top edge of this view match the top edge of the parent. android:layout_centerVertical If “true”, centers this child vertically within its parent. Compile by: Areg. T. 86 Layouts android:layout_below Positions the top edge of this view below the view specified with a resource ID. android:layout_toRightOf Positions the left edge of this view to the right of the view specified with a resource ID. Compile by: Areg. T. 87 Layouts Constraint Layout: allows you to create large and complex layouts with a flat view hierarchy (no nested view groups). It’s similar to Relative Layout in that all views are laid out according to relationships between sibling views and the parent layout, but it’s more flexible than Relative Layout and easier to use with Android studio’s layout editor. Compile by: Areg. T. 88 Layouts All the power of Constraint Layout is available directly from the layout editor’s visual tools, because the layout API and the layout editor were specially built for each other. So you can build your layout with Constraint layout entirely by drag- and-dropping instead of editing the XML. Compile by: Areg. T. 89 Layouts To define a view’s position in constraint layout, you must add at least one horizontal and one vertical constraint for the view. Each constraint represents a connection or alignment to another view, the parent layout, or an invisible guideline. Each constraint defines the view’s position along either the vertical or horizontal axis; so each view must have a minimum of one constraint for each axis, but often more are necessary. Compile by: Areg. T. 90 R.java File R.java file is an auto-generated file by aapt (Android Asset Packaging Tool) that contains resource IDs for all the resources of res/ directory. When you create any component in the xml file, id for the corresponding component is automatically created in this file. The id can be used in the activity source file to perform any action on the component. Compile by: Areg. T. 91