You are previewing Java for Android, Second Edition.
O'Reilly logo
Java for Android, Second Edition

Book Description

This book is for individuals wishing to learn Java and specialize in Android application development. This book consists of two parts. Part I is focused on Java and Part II explains how to build Android applications effectively. The Java tutorial has been updated to cover the new features in Java 8, the latest version of Java. The Android application examples were developed using Android Studio, the official Android IDE from Google.

Table of Contents

  1. Introduction
    1. Java, the Language and the Technology
    2. An Overview of Object-Oriented Programming
    3. About This Book
    4. Downloading Program Examples
  2. Chapter 1: Getting Started with Java
    1. Downloading and Installing Java
    2. Your First Java Program
    3. Java Code Conventions
    4. Integrated Development Environments (IDEs)
    5. Summary
  3. Chapter 2: Language Fundamentals
    1. ASCII and Unicode
    2. Separators
    3. Primitives
    4. Variables
    5. Constants
    6. Literals
    7. Primitive Conversions
    8. Operators
    9. Comments
    10. Summary
  4. Chapter 3: Statements
    1. Overview
    2. The if Statement
    3. The while Statement
    4. The do-while Statement
    5. The for Statement
    6. The break Statement
    7. The continue Statement
    8. The switch Statement
    9. Summary
  5. Chapter 4: Objects and Classes
    1. What Is An Object?
    2. Java Classes
    3. Creating An Object
    4. The null Keyword
    5. Memory Allocation for Objects
    6. Java Packages
    7. Encapsulation and Access Control
    8. The this Keyword
    9. Using Other Classes
    10. Final Variables
    11. Static Members
    12. Static Final Variables
    13. Static import
    14. Variable Scope
    15. Method Overloading
    16. Static Factory Methods
    17. By Value or By Reference?
    18. Loading, Linking, and Initialization
    19. Object Creation Initialization
    20. The Garbage Collector
    21. Summary
  6. Chapter 5: Core Classes
    1. java.lang.Object
    2. java.lang.String
    3. java.lang.StringBuffer and java.lang.StringBuilder
    4. Primitive Wrappers
    5. java.lang.Class
    6. java.lang.System
    7. java.util.Scanner
    8. Summary
  7. Chapter 6: Arrays
    1. Overview
    2. Iterating over an Array
    3. The java.util.Arrays Class
    4. Changing an Array Size
    5. Searching An Array
    6. Passing a String Array to main
    7. Multidimensional Arrays
    8. Summary
  8. Chapter 7: Inheritance
    1. Overview
    2. Accessibility
    3. Method Overriding
    4. Calling the Constructors of the Superclass
    5. Calling the Hidden Members of the Superclass
    6. Type Casting
    7. Final Classes
    8. The instanceof Operator
    9. Summary
  9. Chapter 8: Error Handling
    1. Catching Exceptions
    2. try without catch
    3. Catching Multiple Exceptions
    4. The try-with-resources Statement
    5. The java.lang.Exception Class
    6. Throwing an Exception from a Method
    7. User-Defined Exceptions
    8. Note on Exception Handling
    9. Summary
  10. Chapter 9: Working with Numbers
    1. Boxing and Unboxing
    2. Number Parsing
    3. Number Formatting
    4. Number Parsing with java.text.NumberFormat
    5. The java.lang.Math Class
    6. Working with Money
    7. Generating Random Numbers
    8. Summary
  11. Chapter 10: Interfaces and Abstract Classes
    1. The Concept of Interface
    2. The Interface, Technically Speaking
    3. Default Methods
    4. Static Methods
    5. Base Classes
    6. Abstract Classes
    7. Summary
  12. Chapter 11: Polymorphism
    1. Overview
    2. Polymorphism in Action
    3. Polymorphism and Reflection
    4. Summary
  13. Chapter 12: Enums
    1. Overview
    2. Enums in a Class
    3. The java.lang.Enum Class
    4. Iterating Enumerated Values
    5. Switching on Enum
    6. Enum Members
    7. Summary
  14. Chapter 13: Working with Dates and Times
    1. Overview
    2. The Instant Class
    3. LocalDate
    4. Period
    5. LocalDateTime
    6. Time Zones
    7. ZonedDateTime
    8. Duration
    9. Formatting A Date-Time
    10. Parsing A Date-Time
    11. Using the Old Date and Time API
    12. Summary
  15. Chapter 14: The Collections Framework
    1. An Overview of the Collections Framework
    2. The Collection Interface
    3. List and ArrayList
    4. Iterating Over a Collection with Iterator and for
    5. Set and HashSet
    6. Queue and LinkedList
    7. Collection Conversion
    8. Map and HashMap
    9. Making Objects Comparable and Sortable
    10. Summary
  16. Chapter 15: Generics
    1. Life without Generics
    2. Generic Types
    3. Using Generic Types without Type Parameters
    4. Using the ? Wildcard
    5. Using Bounded Wildcards in Methods
    6. Generic Methods
    7. Writing Generic Types
    8. Summary
  17. Chapter 16: Input/Output
    1. File Systems and Paths
    2. File and Directory Handling and Manipulation
    3. Input/Output Streams
    4. Reading Binary Data
    5. Writing Binary Data
    6. Writing Text (Characters)
    7. Reading Text (Characters)
    8. Logging with PrintStream
    9. Random Access Files
    10. Object Serialization
    11. Summary
  18. Chapter 17: Annotations
    1. Overview
    2. Standard Annotations
    3. Common Annotations
    4. Standard Meta-Annotations
    5. Custom Annotation Types
    6. Summary
  19. Chapter 18: Nested and Inner Classes
    1. An Overview of Nested Classes
    2. Static Nested Classes
    3. Member Inner Classes
    4. Local Inner Classes
    5. Anonymous Inner Classes
    6. Behind Nested and Inner Classes
    7. Summary
  20. Chapter 19: Java Threads
    1. Introduction to Java Threads
    2. Creating A Thread
    3. Working with Multiple Threads
    4. Thread Priority
    5. Stopping a Thread
    6. Synchronization
    7. Visibility
    8. Thread Coordination
    9. Using Timers
    10. Summary
  21. Chapter 20: Concurrency Utilities
    1. Atomic Variables
    2. Executor and ExecutorService
    3. Callable and Future
    4. Locks
    5. Summary
  22. Chapter 21: Internationalization
    1. Locales
    2. Internationalizing Applications
    3. An Internationalized Swing Application
    4. Summary
  23. Chapter 22: Java Networking
    1. An Overview of Networking
    2. The Hypertext Transfer Protocol (HTTP)
    3. java.net.URL
    4. java.net.URLConnection
    5. java.net.Socket
    6. java.net.ServerSocket
    7. A Web Server Application
    8. Summary
  24. Chapter 23: Introduction to Android
    1. Overview
    2. Application Development in Brief
    3. Android Versions
    4. Online Reference
    5. Which Java Versions Can I Use?
  25. Chapter 24: Getting Started with Android
    1. Downloading and Installing Android Studio
    2. Creating An Application
    3. Running the Application on the Emulator
    4. The Application Structure
    5. Debuging Your Application
    6. The Android SDK Manager
    7. Creating An Android Virtual Device
    8. Running An Application on A Physical Device
    9. Opening A Project in Android Studio
    10. Using Java 8
    11. Getting Rid of the Support Library
    12. Summary
  26. Chapter 25: Activities
    1. The Activity Lifecycle
    2. ActivityDemo Example
    3. Changing the Application Icon
    4. Using Android Resources
    5. Starting Another Activity
    6. Activity-Related Intents
    7. Summary
  27. Chapter 26: UI Components
    1. Overview
    2. Using the Android Studio UI Tool
    3. Using Basic Components
    4. Toast
    5. AlertDialog
    6. Notifications
    7. Summary
  28. Chapter 27: Layouts
    1. Overview
    2. LinearLayout
    3. RelativeLayout
    4. FrameLayout
    5. TableLayout
    6. Grid Layout
    7. Creating A Layout Programmatically
    8. Summary
  29. Chapter 28: Listeners
    1. Overview
    2. Using the onClick Attribute
    3. Implementing A Listener
    4. Summary
  30. Chapter 29: The Action Bar
    1. Overview
    2. Adding Action Items
    3. Adding Dropdown Navigation
    4. Going Back Up
    5. Summary
  31. Chapter 30: Menus
    1. Overview
    2. The Menu File
    3. The Options Menu
    4. The Context Menu
    5. The Popup Menu
    6. Summary
  32. Chapter 31: ListView
    1. Overview
    2. Creating A ListAdapter
    3. Using A ListView
    4. Extending ListActivity and Writing A Custom Adapter
    5. Styling the Selected Item
    6. Summary
  33. Chapter 32: GridView
    1. Overview
    2. Using the GridView
    3. Summary
  34. Chapter 33: Styles and Themes
    1. Overview
    2. Using Styles
    3. Using Themes
    4. Summary
  35. Chapter 34: Bitmap Processing
    1. Overview
    2. Bitmap Processing
    3. Summary
  36. Chapter 35: Graphics and Custom Views
    1. Overview
    2. Hardware Acceleration
    3. Creating A Custom View
    4. Drawing Basic Shapes
    5. Drawing Text
    6. Transparency
    7. Shaders
    8. Clipping
    9. Using Paths
    10. The CanvasDemo Application
    11. Summary
  37. Chapter 36: Fragments
    1. The Fragment Lifecycle
    2. Fragment Management
    3. Using A Fragment
    4. Extending ListFragment and Using FragmentManager
    5. Summary
  38. Chapter 37: Multi-Pane Layouts
    1. Overview
    2. A Multi-Pane Example
    3. Summary
  39. Chapter 38: Animation
    1. Overview
    2. Property Animation
    3. An Animation Project
    4. Summary
  40. Chapter 39: Preferences
    1. SharedPreferences
    2. The Preference API
    3. Using Preferences
    4. Summary
  41. Chapter 40: Working with Files
    1. Overview
    2. Creating a Notes Application
    3. Accessing the Public Storage
    4. Summary
  42. Chapter 41: Working with the Database
    1. Overview
    2. The Database API
    3. Example
    4. Summary
  43. Chapter 42: Taking Pictures
    1. Overview
    2. Using Camera
    3. The Camera API
    4. Using the Camera API
    5. Summary
  44. Chapter 43: Making Videos
    1. Using the Built-in Intent
    2. MediaRecorder
    3. Using MediaRecorder
    4. Summary
  45. Chapter 44: The Sound Recorder
    1. The MediaRecorder Class
    2. Example
    3. Summary
  46. Chapter 45: Handling the Handler
    1. Overview
    2. Example
    3. Summary
  47. Chapter 46: Asynchronous Tasks
    1. Overview
    2. Example
    3. Summary
  48. Chapter 47: Services
    1. Overview
    2. The Service API
    3. Declaring A Service
    4. A Service Example
    5. Summary
  49. Chapter 48: Broadcast Receivers
    1. Overview
    2. BroadcastReceiver-based Clock
    3. Canceling A Notification
    4. Summary
  50. Chapter 49: The Alarm Service
    1. Overview
    2. Example
    3. Summary
  51. Chapter 50: Content Providers
    1. Overview
    2. The ContentProvider Class
    3. Creating A Content Provider
    4. Consuming A Content Provider
    5. Summary
  52. Appendix A: javac
    1. Options
    2. Command Line Argument Files
  53. Appendix B: java
    1. Options
  54. Appendix C: jar
    1. Syntax
    2. Options
    3. Examples
    4. Setting an Application Entry Point
  55. Appendix D: NetBeans
    1. Download and Installation
    2. Creating a Project
    3. Creating a Class
    4. Running a Java Class
    5. Adding Libraries
    6. Debugging Code
  56. Appendix E: Eclipse
    1. Download and Installation
    2. Adding A JRE
    3. Creating a Java Project
    4. Creating a Class
    5. Running a Java Class
    6. Adding Libraries
    7. Debugging Code
    8. Useful Shortcuts