Vous êtes sur la page 1sur 9

Murach’s Android Programming

(2nd Edition)
Introduction xv

Section 1 Get started fast with Android


Chapter 1 An introduction to Android and Android Studio 3
Chapter 2 How to start your first Android app 29
Chapter 3 How to finish your first Android app 55
Chapter 4 How to test and debug an Android app 95

Section 2 Essential Android skills


Chapter 5 How to work with layouts and widgets 119
Chapter 6 How to handle events 165
Chapter 7 How to work with themes and styles 197
Chapter 8 How to work with menus and preferences 227
Chapter 9 How to work with fragments 259

Section 3 The News Reader app


Chapter 10 How to work with threads, files, adapters, and intents 287
Chapter 11 How to work with services and notifications 339
Chapter 12 How to work with broadcast receivers 381

Section 4 The Task List app


Chapter 13 How to work with SQLite databases 397
Chapter 14 How to work with tabs and custom adapters 435
Chapter 15 How to work with content providers 467
Chapter 16 How to work with app widgets 501

Section 5 Advanced Android skills


Chapter 17 How to deploy an app 523
Chapter 18 How to work with locations and maps 543

Appendixes
Appendix A How to set up Windows for this book 601
Appendix B How to set up Mac OS X for this book 619
Appendix C How to set up Linux for this book 637
Expanded contents vii

Expanded contents
Section 1 Get started fast with Android
Chapter 1 An introduction to Android and Android Studio
An overview of Android.......................................................................4
Types of devices...............................................................................................................4
Types of apps....................................................................................................................6
A brief history..................................................................................................................8
Versions..........................................................................................................................10
System architecture........................................................................................................12
How apps are compiled and run.....................................................................................14
An introduction to Android Studio................................................... 16
How to work with the Welcome page............................................................................16
How to open an existing project.....................................................................................16
How to view the user interface for an app......................................................................18
How to view the code for an app....................................................................................20
How to run an app on a physical device.........................................................................22
How to run an app on an emulator.................................................................................22
The user interface...........................................................................................................24

Chapter 2 How to start your first Android app


The Tip Calculator app......................................................................30
The user interface...........................................................................................................30
How to create a new project...........................................................................................32
How to develop the user interface...................................................34
How to work with a layout.............................................................................................34
How to add widgets to a layout......................................................................................36
How to set the display text.............................................................................................38
How to work with resource files....................................................................................42
How to set properties.....................................................................................................44
Common properties........................................................................................................46
The XML for the user interface.....................................................................................48

Chapter 3 How to finish your first Android app


How to write the Java code...............................................................56
How to work with an activity.........................................................................................56
How to remove the v7 appcompat support library.........................................................58
How to get references to widgets...................................................................................60
How to handle the EditorAction event...........................................................................62
How to get and set the text for widgets..........................................................................66
The lifecycle of an activity.............................................................................................70
How to save and restore values......................................................................................72
The Java code for the app...............................................................................................74
More skills for finishing an app........................................................80
How to work with the Gradle build script......................................................................80
How to work with dependencies....................................................................................82
How to work with the Android manifest........................................................................84
How to set the launcher icon for an app.........................................................................86
How to use the documentation for the Android API......................................................88
viii Expanded contents

Chapter 4 How to test and debug an Android app


Basic skills for testing and debugging............................................96
Typical test phases..........................................................................................................96
How to check the layout.................................................................................................98
How to handle runtime errors......................................................................................100
How to trace code execution..........................................................102
How to use LogCat ......................................................................................................102
How to use toasts.........................................................................................................104
How to use the debugger................................................................106
How to set and remove breakpoints.............................................................................106
How to step through code............................................................................................108
How to inspect variables..............................................................................................108
How to inspect the stack trace......................................................................................108
How to create emulators................................................................. 110
How to create an emulator for a tablet.........................................................................110
How to create an emulator for a phone with a hard keyboard and DPad.....................112

Section 2 Essential Android skills


Chapter 5 How to work with layouts and widgets
An introduction to layouts and widgets........................................ 120
A summary of layouts..................................................................................................120
A summary of widgets.................................................................................................120
The View hierarchy......................................................................................................122
How to work with layouts................................................................ 124
How to use a linear layout............................................................................................124
How to use a table layout.............................................................................................128
How to use a frame layout...........................................................................................130
How to nest layouts......................................................................................................132
How to provide a landscape layout..............................................................................134
How to work with widgets............................................................... 136
How to use editable text views.....................................................................................136
How to use check boxes...............................................................................................140
How to use radio buttons..............................................................................................142
How to use spinners.....................................................................................................144
How to use seek bars....................................................................................................148
How to display images.................................................................................................150
How to show and hide widgets....................................................................................152
How to add scroll bars..................................................................................................154
How to display web content.........................................................................................156

Chapter 6 How to handle events


A summary of listeners...................................................................166
High-level events..........................................................................................................166
Low-level events..........................................................................................................166
Four techniques for handling events.............................................168
How to use the current class as the listener..................................................................168
How to use a named class as the listener.....................................................................168
How to use an anonymous class as the listener............................................................170
Expanded contents ix

How to use an anonymous inner class as the listener..................................................170


When to use each technique.........................................................................................170
How to handle high-level events.................................................... 172
How to handle events for check boxes and radio buttons............................................172
How to handle events for radio groups........................................................................174
How to handle events for spinners...............................................................................176
How to handle events for seek bars..............................................................................178
How to handle low-level events......................................................180
How to handle Key events............................................................................................180
How to handle Touch events........................................................................................182
The Tip Calculator app....................................................................184
The user interface.........................................................................................................184
The Java code for the activity.......................................................................................186

Chapter 7 How to work with themes and styles


An introduction to themes and styles...........................................198
Three common themes.................................................................................................198
How to supply different themes for different APIs......................................................200
How to convert an existing project to the AppCompat theme......................................202
How to work with styles..................................................................204
How to define a style....................................................................................................204
How to apply a style.....................................................................................................206
How to create a style sheet...........................................................................................208
How to work with themes................................................................ 210
How to modify a built-in theme...................................................................................210
How to modify the AppCompat theme........................................................................212
How to modify a theme depending on the API............................................................214
How to modify the text appearance for a theme..........................................................216
A summary of built-in themes......................................................................................218
How to apply themes....................................................................................................220
How to work with colors..................................................................222
How to define colors....................................................................................................222
How to apply colors.....................................................................................................222

Chapter 8 How to work with menus and preferences


How to work with menus.................................................................228
An introduction to menus.............................................................................................228
How to define a menu..................................................................................................232
How to display an options menu..................................................................................234
How to handle option menu events..............................................................................234
How to start a new activity...........................................................................................236
How to work with preferences........................................................238
An introduction to preferences.....................................................................................238
How to define preferences............................................................................................240
How to display preferences..........................................................................................242
How to get preferences.................................................................................................244
How to use preferences................................................................................................246
More skills for working with preferences......................................248
How to group preferences............................................................................................248
How to enable and disable preferences........................................................................250
How to use Java to work with preferences...................................................................252
x Expanded contents

Chapter 9 How to work with fragments


An introduction to fragments.........................................................260
Single-pane and multi-pane layouts.............................................................................260
The lifecycle methods of a fragment............................................................................262
How to use single-pane layouts for small screens......................264
How to create the layout for a fragment.......................................................................264
How to create the class for a fragment.........................................................................266
How to display a fragment in an activity.....................................................................268
How to create a preference fragment...........................................................................270
How to display a preference fragment in an activity...................................................270
How to use multi-pane layouts for large screens.........................272
How to add multiple fragments to a layout..................................................................272
How to detect screen width..........................................................................................274
How to control the soft keyboard.................................................................................276
Other skills for working with fragments........................................278
How to get a reference to a fragment...........................................................................278
How to replace one fragment with another..................................................................280

Section 3 The News Reader app


Chapter 10 How to work with threads, files, adapters,
and intents
An introduction to the News Reader app......................................288
The user interface.........................................................................................................288
The XML for an RSS feed...........................................................................................290
How to work with threads...............................................................292
How threads work........................................................................................................292
How to execute asynchronous tasks.............................................................................294
How to execute timed tasks..........................................................................................298
How to update the UI thread........................................................................................300
How to work with files.....................................................................302
How to download a file from the Internet....................................................................302
How to parse an XML file............................................................................................304
The RSSFeedHandler class..........................................................................................306
The RSSFeed class.......................................................................................................310
The RSSItem class.......................................................................................................310
How to work with adapters............................................................. 314
How to create the layout for a list view.......................................................................314
How to use an adapter to display data in a list view....................................................316
How to handle events for an adapter............................................................................318
How to work with intents.................................................................320
How to pass data between activities.............................................................................320
How to view a URL in a web browser.........................................................................322
How to dial or call a phone number.............................................................................322
The News Reader app.....................................................................324
The activity_items layout.............................................................................................324
The ItemsActivity class................................................................................................326
The FileIO class...........................................................................................................330
The activity_item layout..............................................................................................330
The ItemActivity class.................................................................................................334
Expanded contents xi

Chapter 11 How to work with services and notifications


How to work with the Application object.......................................340
How to define the Application object...........................................................................340
How to register the Application object.........................................................................342
How to use the Application object...............................................................................342
How to work with services..............................................................344
The lifecycle of a service.............................................................................................344
How to create a service................................................................................................346
How to register a service..............................................................................................348
How to start and stop a service....................................................................................348
How to use threads with services.................................................................................350
How to test a service....................................................................................................352
How to view all services..............................................................................................354
How to work with notifications.......................................................356
How notifications work................................................................................................356
How to create a pending intent.....................................................................................358
How to create a notification.........................................................................................360
How to work with system services................................................362
How to display or remove a notification......................................................................362
How to check if a network connection is available......................................................364
The News Reader app.....................................................................366
The NewsReaderService class.....................................................................................366
The ItemsActivity class................................................................................................370
The FileIO class...........................................................................................................370

Chapter 12 How to work with broadcast receivers


How to work with system broadcasts............................................382
A summary of the system broadcasts...........................................................................382
How to code a receiver for the boot completed broadcast...........................................384
How to code a receiver for the connectivity changed broadcast..................................386
How to work with custom broadcasts...........................................388
How to create and send a custom broadcast.................................................................388
How to code a receiver for a custom broadcast............................................................390

Section 4 The Task List app


Chapter 13 How to work with SQLite databases
An introduction to databases.........................................................398
The user interface for the Task List app.......................................................................398
An introduction to SQLite............................................................................................400
An introduction to the Task List database....................................................................402
The business objects for the Task List app...................................................................404
How to create a database class......................................................408
How to define the constants for a database..................................................................408
How to define the SQL statements that create a database............................................410
How to create or upgrade a database............................................................................412
How to open and close a database connection.............................................................414
How to add public methods to a database class.......................... 416
How to retrieve multiple rows from a table..................................................................416
How to retrieve a single row from a table....................................................................418
How to get data from a cursor......................................................................................418
How to insert, update, and delete rows........................................................................420
xii Expanded contents

How to test the database................................................................424


How to test the database class......................................................................................424
How to clear test data from a device............................................................................426
How to work with the database file..............................................................................426
How to use DB Browser for SQLite............................................................................428

Chapter 14 How to work with tabs and custom adapters


How to use tabs...............................................................................436
How to add the TabManager class to your project.......................................................436
The layout for an activity that displays tabs.................................................................436
The class for an activity that displays tabs...................................................................438
The class for a fragment that displays tab content.......................................................440
How to use a custom adapter.........................................................442
A layout for a list view item.........................................................................................442
A class that extends the layout for a list view item......................................................444
A class for a custom adapter........................................................................................448
A class for a fragment that uses a custom adapter.......................................................450
The Task List app.............................................................................452
The user interface.........................................................................................................452
The activity_task_list menu.........................................................................................454
The TaskListActivity class...........................................................................................454
The activity_add_edit and spinner_list layout.............................................................458
The activity_add_edit menu.........................................................................................458
The AddEditActivity class...........................................................................................460

Chapter 15 How to work with content providers


An introduction to content providers............................................468
URIs for content providers...........................................................................................468
MIME types for content providers...............................................................................468
How to add supporting methods to the database class.................................................470
How to create a content provider...................................................472
How to start a content provider class...........................................................................472
How to provide for querying........................................................................................474
How to provide for inserting rows...............................................................................476
How to provide for updating rows...............................................................................478
How to provide for deleting rows.................................................................................478
How to register a content provider...............................................................................480
How to use a content provider.......................................................482
How to use a custom content provider.........................................................................482
How to use a built-in content provider.........................................................................484
How to work with a dialog box.......................................................486
How to import the dialog class and interface...............................................................486
How to build and show the dialog box.........................................................................486
The Task History app.......................................................................488
The user interface.........................................................................................................488
The XML for the layouts..............................................................................................490
The Java code for the activity.......................................................................................492

Chapter 16 How to work with app widgets


An introduction to app widgets......................................................502
A Home screen that has app widgets...........................................................................502
How to remove app widgets.........................................................................................502
How to add app widgets...............................................................................................502
Expanded contents xiii

How to create app widgets.............................................................504


How to create the layout..............................................................................................504
How to modify the database class................................................................................508
How to create the provider class..................................................................................510
How to configure an app widget..................................................................................514
How to register an app widget......................................................................................516
How to test an app widget............................................................................................516

Section 5 Advanced Android skills


Chapter 17 How to deploy an app
An introduction to distribution and monetization........................524
How distribution works................................................................................................524
How monetization works.............................................................................................526
How to create a release build.........................................................528
How to prepare an app for release................................................................................528
How to create the signed APK file...............................................................................530
How to distribute directly to the user............................................532
How to distribute via a website....................................................................................532
How distribute via email..............................................................................................532
How to publish on Google Play......................................................534
How to set up a publisher account...............................................................................534
How to use the Developer Console to publish an app..................................................536
How to view the listing for an app...............................................................................538

Chapter 18 How to work with locations and maps


An introduction to locations and maps.........................................544
The user interface for the Run Tracker app..................................................................544
An introduction to determining location......................................................................546
An introduction to maps...............................................................................................548
An introduction to the Google Maps Android API......................................................550
How to configure the Google Maps Android API v2....................552
How to create a new Google Maps project..................................................................552
How to add Google Play services to an existing project..............................................554
How to get a Google Maps API key.............................................................................556
How to set permissions and features............................................................................558
How to work with locations.............................................................562
How to connect to Google Play services......................................................................562
How to get the current location....................................................................................566
How to handle a failed connection...............................................................................568
How to get location updates.........................................................................................570
How to make sure GPS is enabled...............................................................................574
How to work with Google Maps......................................................576
How to add a map fragment to a layout.......................................................................576
How to display a map...................................................................................................578
How to zoom in on a location......................................................................................580
How to add markers.....................................................................................................582
How to add lines...........................................................................................................584
The Run Tracker app.......................................................................586
The activity_run_map layout.......................................................................................586
The RunMapActivity class...........................................................................................588
The RunTrackerService class.......................................................................................594
xiv Expanded contents

Appendix A How to set up Windows for this book


How to install the source code for this book................................................................602
How to install the Java SE JDK...................................................................................604
How to install Android Studio......................................................................................606
How to use the Android SDK Manager.......................................................................608
How to create an emulator...........................................................................................610
How to configure a device for development.................................................................612
How to verify that your system is set up correctly.......................................................614

Appendix B How to set up Mac OS X for this book


How to install the source code for this book................................................................620
How to install the Java SE JDK...................................................................................622
How to install Android Studio......................................................................................624
How to use the Android SDK Manager.......................................................................626
How to create an emulator...........................................................................................628
How to configure a device for development.................................................................630
How to verify that your system is set up correctly.......................................................632

Appendix C How to set up Linux for this book


How to install the source code for this book................................................................638
How to install the Java SE JDK and required native libraries......................................640
How to get Android Studio..........................................................................................642
How to use the Android SDK Manager.......................................................................644
How to create an emulator...........................................................................................646
How to configure a device for development.................................................................648
How to verify that your system is set up correctly.......................................................650

Vous aimerez peut-être aussi