ebook img

Kotlin for Android Developers: Learn Kotlin the easy way while developing an Android App PDF

152 Pages·2016·1.04 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Kotlin for Android Developers: Learn Kotlin the easy way while developing an Android App

Kotlin for Android Developers Learn Kotlin the easy way while developing an Android App © 2015 - 2016 Antonio Leiva Tweet This Book! PleasehelpAntonioLeivabyspreadingthewordaboutthisbookonTwitter! Thesuggestedhashtagforthisbookis#kotlinandroiddev. Findoutwhatotherpeoplearesayingaboutthebookbyclickingonthislinktosearchforthis hashtagonTwitter: https://twitter.com/search?q=#kotlinandroiddev Thisbookisdedicatedtoalltheloyalreadersofantonioleiva.com,whomademebelievethatwriting about Android development was a powerful tool to help others learn about it. I felt this book as a necessarystepforward. IalsowanttodoaspecialmentiontoLuisHerreroJiménez,whohasdesignedtheawesomecover of this book, and to Gautier Mechling for helping me so much by reviewing this book. It’s thanks to himthatthispagesarenotfulloftyposandmistakes. And, of course, this is specially dedicated to you. With your support and your help this book is growing,andIhopeitwillbecomeareference.Soanyclaimorsuggestionyouthinkitwillimprove thequalityofthisbookwillbewelcomed.Feelfreetowriteanytimetocontact@antonioleiva.com. Contents I.Aboutthisbook . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1 II.Isthisbookforyou? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2 III.Abouttheauthor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 1Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.1WhatisKotlin? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4 1.2WhatdowegetwithKotlin? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5 2Gettingready . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.1AndroidStudio . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9 2.2InstallKotlinplugin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10 3Creatinganewproject . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.1CreatetheprojectinAndroidStudio . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.2ConfigureGradle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11 3.3ConvertMainActivitytoKotlincode . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 3.4Testthateverythingworks . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 4Classesandfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.1Howtodeclareaclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.2Classinheritance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 4.3Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16 4.4Constructorandfunctionsparameters . . . . . . . . . . . . . . . . . . . . . . . . . . 16 5Writingyourfirstclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 5.1Creatingthelayout . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 5.2TheRecyclerAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20 6Variablesandproperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 6.1Basictypes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22 6.2Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23 6.3Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24 7AnkoandExtensionFunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 CONTENTS 7.1WhatisAnko? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 7.2StartusingAnko . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 7.3Extensionfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28 8RetrievingdatafromAPI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 8.1Performingarequest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29 8.2Performingtherequestoutofthemainthread . . . . . . . . . . . . . . . . . . . . . . 30 9DataClasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 9.1Extrafunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 9.2Copyingadataclass . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 9.3Mappinganobjectintovariables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32 10Parsingdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 10.1Convertingjsontodataclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33 10.2Shapingthedomainlayer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 10.3DrawingthedataintheUI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36 11Operatoroverloading . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 11.1Operatorstables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38 11.2Theexample . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 11.3Operatorsinextensionfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 12Makingtheforecastlistclickable . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 13Lambdas . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 13.1SimplifyingsetOnClickListener() . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46 13.2ClicklistenerforForecastListAdapter . . . . . . . . . . . . . . . . . . . . . . . . . . 47 13.3Extendingthelanguage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48 14VisibilityModifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 14.1Modifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50 14.2Constructors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 14.3Revisingourcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51 15KotlinAndroidExtensions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52 15.1HowtouseKotlinAndroidExtensions . . . . . . . . . . . . . . . . . . . . . . . . . 52 15.2Refactoringourcode . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53 16ApplicationSingletonandDelegatedProperties . . . . . . . . . . . . . . . . . . . . . . 56 16.1ApplicationSingleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 56 16.2DelegatedProperties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 16.3StandardDelegates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 58 16.4Howtocreateacustomdelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 61 16.5ReimplementingAppSingleton . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 62 CONTENTS 17CreatinganSQLiteOpenHelper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 17.1ManagedSqliteOpenHelper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64 17.2Tablesdefinition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65 17.3ImplementingSqliteOpenHelper . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66 17.4Dependencyinjection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 18Collectionsandfunctionaloperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70 18.1Aggregateoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71 18.2Filteringoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 18.3Mappingoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75 18.4Elementsoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76 18.5Generationoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78 18.6Orderingoperations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79 19Savingandrequestingdatafromdatabase . . . . . . . . . . . . . . . . . . . . . . . . . 81 19.1Creatingdatabasemodelclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81 19.2Writingandrequestingdata . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 82 20NullsafetyinKotlin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 20.1HowNulltypeswork . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 20.2NullityandJavalibraries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 91 21Creatingthebusinesslogictodataaccess . . . . . . . . . . . . . . . . . . . . . . . . . . 93 22.Flowcontrolandranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 22.1IfExpression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 98 22.2Whenexpression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 99 22.3Forloops. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 100 22.4Whileanddo/whileloops . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 22.5Ranges . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101 23CreatingaDetailActivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 23.1Preparingtherequest . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 23.2Providinganewactivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 105 23.3Startanactivity:reifiedfunctions . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110 24InterfacesandDelegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 24.1Interfaces . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 112 24.2Delegation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 113 24.3ImplementinganexampleinourApp . . . . . . . . . . . . . . . . . . . . . . . . . . 114 25Generics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 25.1Basics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120 25.2Variance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 25.3Genericsexamples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 123 CONTENTS 26SettingsScreen . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 26.1Creatingthesettingsactivity . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126 26.2AccessingSharedPreferences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127 26.3Genericpreferencedelegate . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130 27TestingyourApp . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 27.1Unittesting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133 27.2Instrumentationtests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136 28Extraconcepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 28.1Nestedclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 28.2Enumclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 140 28.3Sealedclasses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141 28.4Exceptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142 29Conclusion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 144 I. About this book Inthisbook,I’llbecreatinganAndroidappfromgroundupusingKotlinasthemainlanguage.The idea is to learn the language by example, instead of following a regular reference book structure. I’ll be stopping to explain the most interesting concepts and ideas about Kotlin, comparing it with Java7.Thisway,youcanseewhatthedifferencesareandwhichpartsofthelanguagewillhelpyou speedupyourwork. Thisbookisnotmeanttobealanguagereference,butatoolforAndroiddeveloperstolearnKotlin and be able to continue with their own projects by themselves. I’ll be solving many of the typical problemswehavetofaceinourdailylivesbymakinguseofthelanguageexpressivenessandsome other really interesting tools and libraries. However, the text covers most Kotlin features, so by the endofthereadingyouwillhaveadeepknowledgeaboutthelanguage. The book is very practical, so it is recommended to follow the examples and the code in front of a computerandtryeverythingit’ssuggested.Youcould,however,takeafirstreadtogetabroadidea andthendiveintopractice. As you could read in previous pages (and probably the site where you downloaded), this is a lean publication. This means that the book has been progressing thanks to the readers comments. Even though it is now finished, I will review it from time to time to keep it up to date with new Kotlin versions.Sofeelfreetowriteandtellmewhatyouthinkaboutthebook,orwhatcouldbeimproved. I want this book to be the perfect tool for Android developers, and as such, help and ideas will be welcomed. Thanksforbecomingpartofthisexcitingproject. 1 II. Is this book for you? This book was written to be useful to Android developers who are interested in learning Kotlin language. Thisbookisforyouifyouareinsomeofthefollowingsituations: • YouhavesomebasicknowledgeaboutAndroidDevelopmentandtheAndroidSDK. • YouwanttolearnhowtodevelopAndroidappsusingKotlinbyfollowinganexample. • YouneedaguideonhowtosolvemanyofthecommonchallengesanAndroiddeveloperfinds everyday,byusingacleanerandmoreexpressivelanguage. Ontheotherhand,thisbookmaynotbeforyou.Thisiswhatyouwon’tfindinit: • ThisisnotaKotlinBible.I’llexplainalllanguagebasics,andevenmorecomplexideaswhen theycomeoutduringtheprocess,justwhenweneedthem.Soyouwilllearnbyexampleand nottheotherwayround. • I will not explain how to develop an Android app. You won’t need a deep knowledge of the platform,butyouwillneedsomebasics,suchassomeknowledgeofAndroidStudio,Gradle, Java programming and Android SDK. You may even learn some new Android things in the process! • Thisisnotaguidetolearnfunctionalprogramming.Ofcourse,I’llexplainwhatyouneed,as Java7isnotfunctionalatall,butIwon’tdivedeepinfunctionaltopics. 2

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.