AndroidProgrammingCookbook i Android Programming Cookbook AndroidProgrammingCookbook ii Contents 1 AndroidTutorialForBeginners 1 1.1 WhatisAndroid? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.2 InstallingAndroidStudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 1.3 AndroidversionsandAndroidSDKManager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.4 Supportingdifferentscreensizes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 1.5 AndroidProjectStructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8 1.6 Create"HelloAndroidWorld"application . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.6.1 CreateaNewAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 1.6.2 CreatethesourcecodeofasimpleFirstAndroidApplicationActivity . . . . . . . . . . . . . . . . . . . . 13 1.6.3 Createthelayoutoftheproject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6.4 AndroidManifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14 1.6.5 EdittheFirstAndroidApplicationdimensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.6.6 EdittheFirstAndroidApplicationstrings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.6.7 Addthedrawableforeveryscreendensity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 1.6.8 Build,compileandrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 1.7 DownloadtheAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 1.8 Howtocontinue? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 2 AndroidProjectmigrationfromEclipsetoAndroidStudio 19 2.1 WhytouseAndroidStudiooverEclipseADT? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 2.2 AndroidStudionewprojectstructure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.3 Gradleandbuild.gradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 2.4 SimpleEclipseADTprojectmigrationtoAndroidStudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21 2.5 Javacodeandresourcesmigration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 2.6 AndroidManifest.xmlandbuild.gradlefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 2.7 DownloadtheAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 AndroidProgrammingCookbook iii 3 AndroidGoogleMapsv2Tutorial 33 3.1 CreateaGoogleMapsAPIkey . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 3.2 CreateaNewAndroidApplicationProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 3.3 ImportingGooglePlayServicesinyourproject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 3.4 CreatethelayoutofthemainGoogleMapsv2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 3.5 CreatethesourcecodeofthemainAndroidGoogleMapsActivity . . . . . . . . . . . . . . . . . . . . . . . . . . 52 3.6 CreatingthesourcecodeofthehelperclassCustomMarker.java . . . . . . . . . . . . . . . . . . . . . . . . . . 58 3.7 CreatingthesourcecodeofthehelperclassLatLngInterpolator.java . . . . . . . . . . . . . . . . . . . . . . . . 59 3.8 CreatingthesourcecodeofthehelperclassMarkerAnimation.java . . . . . . . . . . . . . . . . . . . . . . . . . 61 3.9 ModifyingtheAndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 3.10 Build,compileandrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 3.11 DownloadtheEclipseProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 4 AndroidStartServiceonBootExample 67 4.1 CreateaNewAndroidStudioProject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67 4.2 CreatethelayoutandthesourcecodeofasimpleAndroidStartServiceOnBootActivity . . . . . . . . . . . . . . 71 4.3 CreatingthesourcecodeoftheBroadcastReceiverOnBootCompleteService . . . . . . . . . . . . . . . . . . . . 72 4.4 CreatingthesourcecodeoftheAndroidServiceStartOnBootService . . . . . . . . . . . . . . . . . . . . . . . . 72 4.5 EditingtheAndroidManifestxml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 4.6 Build,compileandrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74 4.7 DownloadtheAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 5 AndroidBluetoothConnectionExample 80 5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.2 CreateaNewAndroidStudioProject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 5.3 CreatethelayoutoftheBluetoothChat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84 5.4 CreatethesourcecodeoftheBluetoothChat . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 5.5 CreatethesourcecodeoftheBluetoothChatService . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 5.6 CreatethelayoutoftheDeviceListActivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96 5.7 CreatethesourcecodeoftheDeviceListActivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97 5.8 AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 5.9 build.gradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.10 Build,compileandrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 5.11 DownloadtheAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 104 AndroidProgrammingCookbook iv 6 AndroidMultitouchExample 105 6.1 CreateaNewAndroidStudioProject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 6.2 Createthelayoutoftheproject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 6.3 CreatingthesourcecodeoftheTouchableFrameLayoutFrameLayout . . . . . . . . . . . . . . . . . . . . . . . 110 6.4 CreatingthesourcecodeofthemainAndroidMultitouchActivityActivity . . . . . . . . . . . . . . . . . . . . . 113 6.5 Createthestrings.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6.6 AndroidManifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6.7 build.gradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115 6.8 Build,compileandrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 116 6.9 DownloadtheAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118 7 AndroidStackViewExample 119 7.1 CreateaNewAndroidStudioProject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119 7.2 CreatethelayoutoftheAndroidStackViewActivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 7.3 CreatethelayoutoftheStackViewitems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 7.4 CreatethesourcecodeoftheStackItems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 7.5 CreatethesourcecodeoftheStackAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124 7.6 CreatethesourcecodeoftheAndroidStackViewActivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 125 7.7 AndroidManifest.xml . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 7.8 build.gradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 7.9 Build,compileandrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 7.10 DownloadtheAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129 8 AndroidViewPagerExample 130 8.1 CreateaNewAndroidStudioProject. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 8.2 CreatethelayoutofthemainAndroidViewPagerExample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 134 8.3 CreatethesourcecodeofthemainAndroidViewPagerExampleActivity . . . . . . . . . . . . . . . . . . . . . . 135 8.4 CreatethelayoutofthemainFragmentViewPager . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 8.5 CreatethesourcecodeofthemainFragmentViewPagersupport.v4.app.Fragment . . . . . . . . . . . . . . . . . 136 8.6 AndroidManifest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137 8.7 Composingbuild.gradlefile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.8 Build,compileandrun . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138 8.9 DownloadtheAndroidStudioProject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143 AndroidProgrammingCookbook v Copyright (c) Exelixis Media P.C., 2016 All rights reserved. Without limiting the rights under copyright reserved above, no part of this publication may be reproduced, stored or introduced into a retrieval system, or transmitted, in any form or by any means (electronic, mechanical, photocopying, recording or otherwise), without the prior written permission of the copyright owner. AndroidProgrammingCookbook vi Preface AndroidisamobileoperatingsystemdevelopedbyGoogle,basedontheLinuxkernelanddesignedprimarilyfortouchscreen mobile devices such as smartphones and tablets. Android’s user interface is mainly based on direct manipulation, using touch gesturesthatlooselycorrespondtoreal-worldactions,suchasswiping,tappingandpinching,tomanipulateon-screenobjects, alongwithavirtualkeyboardfortextinput. Inadditiontotouchscreendevices,GooglehasfurtherdevelopedAndroidTVfortelevisions,AndroidAutoforcars,andAndroid Wearforwristwatches,eachwithaspecializeduserinterface. VariantsofAndroidarealsousedonnotebooks,gameconsoles, digitalcameras,andotherelectronics. Androidhasthelargestinstalledbaseofalloperatingsystemsofanykind. AndroidhasbeenthebestsellingOSontabletssince 2013,andonsmartphonesitisdominantbyanymetric. (Source: https://en.wikipedia.org/wiki/Android_(operating_system)) Inthisebook,weprovideacompilationofAndroidprogrammingexamplesthatwillhelpyoukick-startyourownwebprojects. Wecoverawiderangeoftopics,fromServicesandViews,toGoogleMapsandBluetoothfunctionality.Withourstraightforward tutorials,youwillbeabletogetyourownprojectsupandrunninginminimumtime. AndroidProgrammingCookbook vii About the Author ChryssaisaComputerSciencegraduatefromAthensUniversityofEconomicandBusiness. Duringherstudies,Chryssacarried outagreatvarietyofprojectsrangingfromnetworkingtosoftwareengineering. Sheisverykeenonfrontenddevelopmentespeciallyonmobiletechnologiesandwebapplications. Shehasworkedasajunior SoftwareEngineerinthetelecommunicationsareaandcurrentlyworksasanAndroidDeveloper. AndroidProgrammingCookbook 1/143 Chapter 1 Android Tutorial For Beginners There are lots of reasons why more and more people are interested in learing how to be able to develop Android applications. Unarguably,Androidisthemostpopularmobileoperatingsystem,withalmost2billiondevicesactivatedanditoffersaunified approachtoapplicationdevelopmentformobiledevices. Thatmeans,thatdevelopersneedonlydevelopforAndroid,andtheirapplicationswillbeabletorunondifferentdevicespowered byAndroid. ThisparticularassetgivesAndroidendlesspossibilites! Thismeansthatandapplicationthatisdesignedtoworkon mobilephonedevicescanbealsotransferedtoAndroidpoweredTVsetsorAndroidCarsystems. Thisiswhy,Androidisanexcitingspacetomakeappsthatcanhelpyouineveryaspectofyourlife,canhelpyoucommunicate, organize,educate,entertainorjusttomakeyourlifeeasierineverydevicethattheymightrunon! Inthisspecialexample,wearegoingtosetourAndroidDevelopmentStudioIDE,makeourveryfirstAndroidapplication anddiscovertheAndroidDevelopmentworldintheeasiestpossibleway. The mobile development world can be very fun, because the direct results we see when creating our own application, can be highlymotivatingandrewarding. 1.1 What is Android? Android is a mobile operating system currently developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets. And as we said before, Android offers a unified approach to applicationdevelopmentformobiledevices. Androidisanopen-sourceoperatingsystemnamedAndroid.Googlehasmadethecodeforallthelow-level"stuff"aswellasthe neededmiddlewaretopoweranduseanelectronicdevice,andgaveAndroidfreelytoanyonewhowantstowritecodeandbuild theoperatingsystemfromit. Thereisevenafullapplicationframeworkincluded,sothird-partyappscanbebuiltandinstalled, thenmadeavailablefortheusertorunastheylike. The"proper"nameforthisistheAndroidOpenSourceProject,andthisiswhatpeoplemeanwhentheysaythingslikeAndroid isopenandfree. Android,inthisiteration,isfreeforanyonetouseastheylike. 1.2 Installing Android Studio In order to write an Android application, we are going to need a development environment. Google has made a very useful toolforallAndroidDevelopers, theAndroidStudio. AndroidStudioistheofficialIDEforAndroiddevelopment, andwitha singledownloadincludeseverythingyouneedtobegindevelopingAndroidapps. Includedinthedownloadkit,aretheSoftware DevelopmentKit(SDK),withalltheAndroidlibrarieswemayneed, andtheinfrastructuretodownloadthemanyAndroid emulatorinstances,sothatwecaninitiallyrunourapplication,withoutneedingarealdevice. So,wearegoingtodownloadandinstallAndroidStudio. AndroidProgrammingCookbook 2/143 First we have to have installed the Java Development Kit (JDK) from Oracle. If you do not, please you should download the latestJDKfromtheOracle’sspecialsectionhere. Figure1.1: AndroidStudioInstallation-step1
Description: