Basics of Android, First App: Helloandroid
Total Page:16
File Type:pdf, Size:1020Kb
© Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION CHAPTER ONE © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Basics of Android, © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC First App:NOT FOR SALE OR DISTRIBUTION HelloAndroidNOT FOR SALE OR DISTRIBUTION CHAPTER CONTENTS © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT Introduction FOR SALE OR DISTRIBUTION 1.4 NOTRunning FOR the SALE App InsideOR DISTRIBUTION the Emulator 1.1 Smartphones and Their Operating 1.5 Debugging the App with Logcat Systems 1.6 Using the Debugger 1.1.1 Smartphones 1.7 Testing and Running the App on an © Jones & Bartlett Learning, 1.1.2 LLCAndroid Phones © Jones & BartlettActual Learning, Device LLC NOT FOR SALE OR DISTRIBUTION 1.1.3 Apps and Google Play NOT FOR 1.8SALE The OR App DISTRIBUTION Manifest and the Gradle 1.2 Development Environment for Build System Android Apps 1.8.1 The AndroidManifest.xml 1.3 Our First App: HelloAndroid File: App Icon and Device Orientation 1.3.1© Jones Skeleton & Bartlett App Learning, LLC © Jones & Bartlett Learning, LLC 1.8.2 The Gradle Build System 1.3.2NOT GUIFOR Preview SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Chapter Summary 1.3.3 XML Files: activity_main.xml, styles.xml, strings.xml, Exercises, Problems, and Projects dimens.xml © Jones & Bartlett 1.3.4 The Learning, MainActivity LLC Class © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1 Chapter opener image: © Fon_nongkran/Shutterstock © Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 9781284093650_CH01_001_038.indd 1 28/10/16 3:43 PM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 2 CHAPTER 1 Basics of Android, First App: HelloAndroid © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC IntroductionNOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Today, it seems that almost everybody has a smartphone and is using apps. There are apps to check our email, check the weather, play games, calculate a mortgage payment, translate one language into another, learn algebra, or even apps for websites, social © Jones &media, Bartlett or news Learning, organizations LLC such as Facebook, Twitter,© Jonesor CNN. &In Bartlettthis chapter, Learning, we learn LLC NOT FOR howSALE to developOR DISTRIBUTION our first app for Android devices. NOT FOR SALE OR DISTRIBUTION 1.1 Smartphones and Their Operating Systems © Jones & Bartlett Learning,1.1.1 LLCSmartphones © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION A smartphone is an intelligent cellular phone, or a cellular phone with a computer inside, as well as some extra hardware. Thus, programmers can write application software for them; such appli- cation software is called an app. A smartphone has the typical components of a regular computer: a CPU,© Jones memory, & a storageBartlett device, Learning, an operating LLC system, and other devices, such© Jones as a camera, & Bartlett an Learning, LLC accelerometer, or a GPS. NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION The two most well-known operating systems are the Android operating system, from Google, and iOS, from Apple. Other popular operating systems for smartphones include BlackBerry, Windows, and Symbian. The annual number of smartphones sold worldwide is now over one billion. Furthermore, smartphones comprise an increasing percentage of the mobile phones sold. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE1.1.2 ORAndroid DISTRIBUTION Phones NOT FOR SALE OR DISTRIBUTION There are over 100 different varieties of Android phones or tablets. They can have a different CPU, a different screen resolution, and a different amount of memory available. This makes it difficult for developers to test their apps on actual devices. The dimensions of the various components of the © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC user interface of an app can be different depending on the Android phone or tablet. Furthermore, NOT FOR SALE OR DISTRIBUTIONin complex games where speed is an issue,NOT an app FOR can runSALE differently OR DISTRIBUTIONon an old and slow Android device as compared to a more recent one. This is something to keep in mind as we develop apps for the Android market. 1.1.3© Jones Apps & andBartlett Google Learning, Play LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Android apps are distributed via Google Play (https://play.google.com), which used to be called the Android Market. Google Play is actually a store for digital content, much like the Apple’s app store, not just apps. In order to distribute apps on Google Play, you need to become a registered © Jones &developer, Bartlett which Learning, costs $25. LLC © Jones & Bartlett Learning, LLC There are now over one million apps in Google Play, and the vast majority of those apps are NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION free. Top categories include entertainment (games), personalization, tools, books, and references. Most of the downloaded apps are free apps. One should also be aware that the Android operating © Jones & Bartlett Learning, LLC. NOT FOR SALE OR DISTRIBUTION. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 9781284093650_CH01_001_038.indd 2 28/10/16 3:43 PM © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.3 Our First App: HelloAndroid 3 system is open,© Jonesso anyone & can Bartlett easily copy Learning, an app from LLC one Android device to another.© JonesThere is & Bartlett Learning, LLC little protectionNOT for intellectualFOR SALE property. OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION 1.2 Development Environment for Android Apps © JonesThe typical & Bartlett and recommended Learning, development LLC environment for Android© Jones apps is comprised& Bartlett of the Learning, LLC NOTfollowing: FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION ▸ Java Development Kit (JDK) ▸ Android Studio ▸ Android Standard Development Kit (Android SDK) © Jones & Bartlett Learning,It is not a requirement LLC to use Android Studio© to Jonesdevelop Android& Bartlett apps, weLearning, can run our LLC code NOT FOR SALE fromOR DISTRIBUTIONthe command line or use another integratedNOT development FOR SALE environment, OR DISTRIBUTION such as Eclipse. However, Android Studio is Google’s official development environment and is likely to quickly become the industry standard; we use Android Studio in this book. In order to set up a complete Android app development environment, we need to: ▸ Dow©nload Jones and install & Bartlett the latest JavaLearning, SDK (if we LLChave not done it yet), which we can© Jones find at & Bartlett Learning, LLC http://www.oracle.com/technetwork/java/javase/downloads/index.htmlNOT FOR SALE OR DISTRIBUTION . NOT FOR SALE OR DISTRIBUTION ▸ Download and install Android Studio, including the IDE, SDK tools, and the emulator sys- tem, which we can find at http://developer.android.com/sdk/index.html © Jones1.3 Our& Bartlett First Learning, App: HelloAndroid LLC © Jones & Bartlett Learning, LLC NOT FOR SALE OR DISTRIBUTION NOT FOR SALE OR DISTRIBUTION Let’s create our first Android app. 1.3.1. Skeleton App © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC Open Android Studio. The first time we run Android Studio, the environment checks that it is up NOT FOR SALE toOR date, DISTRIBUTION asks us to download components if needed.NOT Once FOR that is SALE done and OR we clickDISTRIBUTION on Finish (see FIGURE 1.1), we see the screen shown in FIGURE 1.2. Recent projects are listed on the left side; there will not be any the first time we use Android Studio. To start a new project, click on Start a new Android Studio project. In the dialog© Jones box shown & inBartlett FIGURE 1.3 Learning,, we type in our LLC project name (we choose HelloAndroid)© Jones & Bartlett Learning, LLC and our domainNOT (we FORchose jblearning.com;SALE OR DISTRIBUTION if we do not have a domain name, we can NOTchoose FORany SALE OR DISTRIBUTION name); the other two fields (package name and project location) are automatically filled. If we prefer a different project location, we can edit that field. Note that the package name is our domain name in reverse. It is typical for developers to name their package with their domain name in reverse; it insures that it is unique. When we are done, we click Next. © Jones & Bartlett Learning, LLC © Jones & Bartlett Learning, LLC The next dialog box (FIGURE 1.4) is used to specify the minimum SDK for this project; NOTdepending FOR SALE on our OR app, DISTRIBUTION this can be important. For example, if weNOT incorporate FOR advertising, SALE OR this DISTRIBUTION may require a higher SDK level than the default level; however, the more recent the