Introduction 1
How to Use This Book 1
Conventions Used in This Book 2
What You Dont Have to Read 2
Foolish Assumptions 3
How This Book Is Organized 4
Part 1: Getting Started with Java Programming for Android Developers 4
Part 2: Writing Your Own Java Programs 5
Part 3: Working with the Big Picture: Object-Oriented Programming 5
Part 4: Powering Android with Java Code 5
Part 5: The Part of Tens 5
More on the web! 6
Icons Used in This Book 6
Beyond the Book 7
Where to Go from Here 7
Part 1: Getting Started with Java Programming For Android Developers 9
Chapter 1: All about Java and Android 11
The Consumer Perspective 12
The Many Faces of Android 13
The Developer Perspective 15
Java 15
Xml 18
Linux 19
From Development to Execution with Java 20
What is a compiler? 20
What is a virtual machine? 24
Java, Android, and Horticulture 26
Chapter 2: Getting the Tools That You Need 27
The Stuff You Need 28
If You Dont Like to Read the Instructions . 29
Getting This Books Sample Programs 32
Setting Up Java 33
Setting Up Android Studio and the Android SDK 37
Launching the Android Studio IDE 38
Opening One of This Books Sample Programs 40
Using Android Studio 42
Starting up 42
The main window 43
Things You Might Eventually Have to Do 48
Installing new versions (and older versions) of Android 49
Creating an Android virtual device 50
Chapter 3: Creating and Running an Android App 55
Creating Your First App 56
First things first 57
Launching your first app 61
If the Emulator Doesnt Behave 63
Running third-party emulators 64
Testing apps on a physical device 65
The Project Tool Window 68
The app/manifests branch 68
The app/java branch 69
The app/res branches 69
The Gradle scripts branch 70
Dragging, Dropping, and Otherwise Tweaking an App 70
Creating the look 71
Coding the behavior 83
What All That Java Code Does 88
Finding the EditText and TextView components 88
Responding to a button click 90
The rest of the code 91
Going Pro 93
Part 2: Writing Your Own Java Programs 95
Chapter 4: An Ode to Code 97
Hello, Android! 97
The Java Class 99
The names of classes 103
Why Java Methods Are Like Meals at a Restaurant 105
What does Moms Restaurant have to do with Java? 106
Method declaration 106
Method call 108
Method parameters 108
The chicken or the egg 109
How many parameters? 109
Method declarations and method calls in an Android program 111
Punctuating Your Code 116
Comments are your friends 119
Whats Barrys excuse? 122
All About Android Activities 123
Extending a class 124
Overriding methods 124
An activitys workhorse methods 125
Chapter 5: Javas Building Blocks 129
Info Is As Info Does 130
Variable names 133
Type names 133
Assignments and initializations 134
Expressions and literals 136
How to string characters together 139
Javas primitive types 140
Things You Can Do with Types 142
Add letters to numbers (Huh?) 144
Javas exotic assignment operators 146
True bit 147
Java isnt like a game of horseshoes 148
Use Javas logical operators 150
Parenthetically speaking 155
Chapter 6: Working with Java Types 157
Working with Strings 157
Going from primitive types to strings 158
Going from strings to primitive types 159
Getting input from the user 160
Practice Safe Typing 163
Widening is good; narrowing is bad 165
Incompatible types 166
Using a hammer to bang a peg into a hole 167
Chapter 7: Though These Be Methods, Yet There Is Madness int 169
Minding Your Types When You Call a Method 170
Method parameters and Java types 173
If at first you dont succeed 174
Return types 174
The great void 175
Displaying numbers 176
Primitive Types and Pass-by Value 177
Whats a developer to do? 181
A final word 183
Chapter 8: What Java Does (and When) 187
Making Decisions 187
Java if statements 189
Choosing among many alternatives 191
Some formalities concerning Java switch statements 198
Repeating Instructions Over and Over Again 199
Check, and then repeat 200
Repeat, and then check 207
Count, count, count 211
Whats Next? 214
Part 3: Working with the Big Picture: Object-oriented Programming 215
Chapter 9: Why Object-Oriented Programming Is Like Selling Cheese 217
Classes and Objects 219
What is a class, really? 220
What is an object? 222
Creating objects 223
Reusing names 227
Calling a constructor 230
More About Classes and Objects (Adding Methods to the Mix) 232
Constructors with parameters 235
The default constructor 239
This is it! 240
Giving an object more responsibility 242
Members of a class 245
Reference types 246
Pass by reference 247
Javas Modifiers 251
Public classes and default-access classes 251
Access for fields and methods 253
Using getters and setters 257
What does static mean? 260
To dot, or not 263
A bad example 264
Whats Next? 265
Chapter 10: Saving Time and Money: Reusing Existing Code 267
The Last Word on Employees Or Is It? 268
Extending a class 269
Overriding methods 272
Javas super keyword 278
Java annotations 279
More about Javas Modifiers 281
Keeping Things Simple 285
Using an interface 286
Some Observations about Androids Classes 291
Javas super keyword, revisited 292
Casting, again 293
Part 4: Powering Android with Java Code 295
Chapter 11: The Inside Story 297
A Button-Click Example 297
This is a callback 302
Android string resources (A slight detour) 302
Introducing Inner Classes 307
No Publicity, Please! 309
Lambda Expressions 313
Chapter 12: Dealing with a Bunch of Things at a Time 317
Creating a Collection Class 318
More casting 320
Java generics 321
Javas wrapper classes 325
Stepping Through a Collection 326
Using an iterator 326
The enhanced for statement 328
A cautionary tale 329
Functional programming techniques 331
Javas Many Collection Classes 332
Arrays 333
String resource arrays 336
Javas varargs 337
Using Collections in an Android App 340
The listener 343
The adapter 343
Chapter 13: An Android Social Media App 345
The Twitter Apps Files 346
The Twitter4J API jar file 346
The manifest file 348
The main activitys layout file 349
How to Talk to the Twitter Server 352
Using OAuth 353
Making a ConfigurationBuilder 353
Getting OAuth keys and tokens 355
The Applications Main Activity 357
The onCreate method 362
The button listener methods 363
The trouble with threads 363
Understanding Androids AsyncTask 366
My Twitter apps AsyncTask classes 368
Cutting to the chase, at last 370
Javas Exceptions 372
Catch clauses 374
A finally clause 375
Passing the buck 376
Chapter 14: Hungry Burds: A Simple Android Game 381
Introducing the Hungry Burds Game 382
The Main Activity 385
The code, all the code, and nothing but the code 388
Measuring the display 392
Constructing a Burd 395
Android animation 398
Creating menus 400
Shared preferences 403
Informing the user 404
Its Been Fun 405
Part 5: the Part of Tens 407
Chapter 15: Ten Ways to Avoid Mistakes 409
Putting Capital Letters Where They Belong 409
Breaking Out of a switch Statement 410
Comparing Values with a Double Equal Sign 410
Adding Listeners to Handle Events 411
Defining the Required Constructors 411
Fixing Nonstatic References 412
Staying within Bounds in an Array 412
Anticipating Null Pointers 412
Using Permissions 414
The Activity Not Found 414
Chapter 16: Ten Websites for Developers 415
This Books Websites 415
The Horses Mouth 416
Finding News and Reviews 416
Index 417