CHAPTER 1 Introduction Chapter objectives • Understand what Android is • Learn the differences between Java and Android Java • Examine the Android project structure • Build a basic application using Android Studio • Learn about the Model-View-Controller design 1.1 Android Platforms • First-generation Android phones did not include many of the features users are familiar with • Since 2008, Android has been enhanced in a multitude of ways, improving its performance, the user interface design, and adding many features, such as voice searching • By 2015, Android applications were including fluid animations, bold color palettes, and sophisticated multitasking menus that featured voice interactions Table 1-1 shows the evolution of Android • Google services were integrated as part of the Android operating system since the first installment • Two of the most substantial inclusions of Android 1.0 were Google Maps and Notifications • Notifications remains an important part of the Android user interface • In current releases of Android, this feature has been refined to include chat messages and calendar events that support synchronization across all Android devices • By the time Cupcake appeared in 2009, refinements, such as the inclusion of selfrefreshing widgets and copy-and-paste, had been implemented • One of the most significant features that emerged with Cupcake was the introduction of a soft keyboard, which also brought the first forms of keyboard skins • Keyboard skins are created by third-party developers to provide enhancements to an existing soft keyboard • Unique to Android, skins allow users to personalize their keyboard • Skins have evolved alongside Android and have shifted beyond basic appearance and into more extensive behavior than originally seen in Cupcake • Today, Android device manufacturers regularly offer skins that enhance the user experience by adding functionality to the design. • Android 2.2 was created with the ability to execute far faster than previous versions • This was due to the introduction of the new Dalvik Just-In-Time (JIT) compiler, first seen on Froyo • Dalvik allowed for better CPU performance, which significantly enhanced processing power • Froyo’s browser came with a new JavaScript engine, making Internet browsing nearly three times faster than the previously released version of Android • Froyo also brought native support for tethering. • Gingerbread the first version of Android that backed multi-core processing on mobile devices • For application developers, Gingerbread brought support for new technologies, such as NFC (Near Field Communication), and SIP (Session Initiation Protocol) • The SIP API provides developers with tools to create applications that perform video conferencing and instant messaging • Honeycomb was released as the first version of Android specifically implemented for tablets • Prior to Honeycomb, Android tablets were running on phone operating systems that were stretched to fit the screen of a larger tablet • Ice Cream Sandwich combined the best characteristics of Gingerbread and Honeycomb into a single operating system that would work for tablets and phones • Ice Cream Sandwich was able to bring many of the design elements of Honeycomb to smartphones, while refining the Android experience • Jelly Bean it was a faster and smoother Android version • Along with its Google Voice Search feature, Jelly Bean was a jump in magnitude of performance • This version of Android was often referred to as a turning point for Android, where services and customization options met responsive design guidelines • KitKat looked similar to Jelly Bean; however, due to a 512 MB size it was able to run on a much larger array of devices • Lollipop is the latest version of Android being used by developers • Lollipop and 64-bit chips provide enhanced performance with graphics, decoding and encoding of high-resolution video, and algorithms for facial recognition and speech interaction 1.2 Java vs. Android Java • A large number of Java libraries are available in the Android platform • The SDK includes a debugger, software libraries, and an emulator for an Android device Differences between the Java API and the Android API • There is no Java virtual machine in the Android platform • Dalvik is a specialized virtual machine • Once Java has been compiled into bytecode by the Java compiler, it is compiled again into a proprietary bytecode • Bytecode loaded by the Dalvik virtual machine should not be confused with Java bytecode • The dex bytecode is a compact Dalvik executable format designed for Android systems, which are constrained in terms of memory and processor speed • The Android SDK includes the Dalvik dx tool, used to translate java bytecode into Dalvik bytecode 1.3 Android Studio IDE • Android Studio is the official IDE for building Android applications • Developed by Google, this environment focuses exclusively on Android development and comes bundled with the Android Software Development Kit, SDK • The SDK provides developers with a packaged set of developer tools and API libraries for building complete applications, testing them on virtual devices, and performing debugging and optimization • • • • • • Java Editor Layout Editor Android SDK Android SDK Archive AVD Gradle 1.4 The Basics of App Design • The process for building an application 1.5 The Anatomy of an Android Project • A project built in Android Studio, will be structured similar to Figure 1-6 • Project structure is organized by source code, the application’s resources, manifest settings, and build files • All of these files are eventually packaged into an apk file, which represents the final 1.5.1 Android Manifest File • An AndroidManifest.xml, is required for every Android application • This file uses XML code to define specific application information • This information can include general application-wide settings such as the application’s style and launch icon. 1.5.2 Java Source Code and Activity class files • The Java source code of an application is placed in the java directory of the project structure • A main file, often named MainActivity or sometimes MyActivity, is a java file that is auto-generated when the project is first built • It may have a different name, depending on the Android Studio version you are using 1.5.3 Drawable Resources • The drawable folder is located in the res directory • res contains the application resources • Drawable resources are image files, such as application icons, buttons, and background textures Proper Scaling of Images • Android runs on variety of devices ranging from low-density to high-density screens – – – – xlarge screens are at least 960dp x 720dp large screens are at least 640dp x 480dp normal screens are at least 470dp x 320dp small screens are at least 426dp x 320dp • A density independent pixel is equivalent to one physical pixel on a 160-dpi )dots per inch) screen. • Conversion of dp units to screen pixels (px) is: Px = dp * dpi/160 Generalized screen sizes • To properly scale application images, it is best to start with a raw resource in vector format and generate images for each density using the following scale: xxhdpi – 3.0 xhdpi – 2.0 hdpi – 1.5 mdpi – 1.0 (baseline) 1.5.4 Layout XML files • User interface screens are visually designed and coded as XML layout files • The design and arrangement of the elements on the screen are implemented using XML code in a layout file. 1.7 Model-View-Controller • Android applications tend to rely on the Model-View-Controller design architecture • This architecture assigns one of three roles that objects can play in an application • For example, an object can be a Model, View, or Controller 1.9 Sharing your Android Applications • Android requires the application to be digitally signed with a certificate • Android uses this certificate to identify the author • Android apps typically use self-signed certificates, in which the app developer holds the certificate’s private key