Statistics
71
Views
0
Downloads
0
Donations
Support
Share
Uploader

高宏飞

Shared on 2025-11-21

AuthorGoalKicker.com

The Android™ Notes for Professionals book is compiled from Stack Overflow Documentation, the content is written by the beautiful people at Stack Overflow. Text content is released under Creative Commons BY-SA. See credits at the end of this book whom contributed to the various chapters. Images may be copyright of their respective owners unless otherwise specified Book created for educational purposes and is not affiliated with Android™ group(s), company(s) nor Stack Overflow. All trademarks belong to their respective company owners.

Tags
No tags
Publisher: Goalkicker.com and Stack Overflow Documentation
Publish Year: 2019
Language: 英文
Pages: 1329
File Format: PDF
File Size: 11.9 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Android Notes for ProfessionalsAndroid™ Notes for Professionals GoalKicker.com Free Programming Books Disclaimer This is an unocial free book created for educational purposes and is not aliated with ocial Android™ group(s) or company(s). All trademarks and registered trademarks are the property of their respective owners 1000+ pages of professional hints and tricks
Contents About 1 ................................................................................................................................................................................... Chapter 1: Getting started with Android 2 ........................................................................................................... Section 1.1: Creating a New Project 2 .............................................................................................................................. Section 1.2: Setting up Android Studio 13 ....................................................................................................................... Section 1.3: Android programming without an IDE 14 .................................................................................................. Section 1.4: Application Fundamentals 18 ...................................................................................................................... Section 1.5: Setting up an AVD (Android Virtual Device) 19 ......................................................................................... Chapter 2: Layouts 23 ..................................................................................................................................................... Section 2.1: LayoutParams 23 ......................................................................................................................................... Section 2.2: Gravity and layout gravity 26 .................................................................................................................... Section 2.3: CoordinatorLayout Scrolling Behavior 28 ................................................................................................. Section 2.4: Percent Layouts 30 ...................................................................................................................................... Section 2.5: View Weight 31 ............................................................................................................................................ Section 2.6: Creating LinearLayout programmatically 32 ........................................................................................... Section 2.7: LinearLayout 33 ........................................................................................................................................... Section 2.8: RelativeLayout 34 ........................................................................................................................................ Section 2.9: FrameLayout 36 .......................................................................................................................................... Section 2.10: GridLayout 37 ............................................................................................................................................. Section 2.11: CoordinatorLayout 39 ................................................................................................................................. Chapter 3: Gradle for Android 41 ............................................................................................................................. Section 3.1: A basic build.gradle file 41 ........................................................................................................................... Section 3.2: Define and use Build Configuration Fields 43 ........................................................................................... Section 3.3: Centralizing dependencies via "dependencies.gradle" file 46 ................................................................. Section 3.4: Sign APK without exposing keystore password 47 .................................................................................. Section 3.5: Adding product flavor-specific dependencies 49 ..................................................................................... Section 3.6: Specifying dierent application IDs for build types and product flavors 49 ......................................... Section 3.7: Versioning your builds via "version.properties" file 50 ............................................................................. Section 3.8: Defining product flavors 51 ........................................................................................................................ Section 3.9: Changing output apk name and add version name: 51 .......................................................................... Section 3.10: Adding product flavor-specific resources 52 .......................................................................................... Section 3.11: Why are there two build.gradle files in an Android Studio project? 52 ................................................. Section 3.12: Directory structure for flavor-specific resources 53 ............................................................................... Section 3.13: Enable Proguard using gradle 53 ............................................................................................................. Section 3.14: Ignoring build variant 54 ............................................................................................................................ Section 3.15: Enable experimental NDK plugin support for Gradle and AndroidStudio 54 ....................................... Section 3.16: Display signing information 56 ................................................................................................................. Section 3.17: Seeing dependency tree 57 ....................................................................................................................... Section 3.18: Disable image compression for a smaller APK file size 58 .................................................................... Section 3.19: Delete "unaligned" apk automatically 58 ................................................................................................ Section 3.20: Executing a shell script from gradle 58 ................................................................................................... Section 3.21: Show all gradle project tasks 59 ............................................................................................................... Section 3.22: Debugging your Gradle errors 60 ............................................................................................................ Section 3.23: Use gradle.properties for central versionnumber/buildconfigurations 61 ......................................... Section 3.24: Defining build types 62 .............................................................................................................................. Chapter 4: RecyclerView onClickListeners 63 .................................................................................................... Section 4.1: Kotlin and RxJava example 63 ................................................................................................................... Section 4.2: RecyclerView Click listener 64 ....................................................................................................................
Section 4.3: Another way to implement Item Click Listener 65 ................................................................................... Section 4.4: New Example 67 .......................................................................................................................................... Section 4.5: Easy OnLongClick and OnClick Example 68 ............................................................................................. Section 4.6: Item Click Listeners 71 ................................................................................................................................. Chapter 5: NavigationView 73 .................................................................................................................................... Section 5.1: How to add the NavigationView 73 ............................................................................................................ Section 5.2: Add underline in menu elements 77 .......................................................................................................... Section 5.3: Add seperators to menu 78 ........................................................................................................................ Section 5.4: Add menu Divider using default DividerItemDecoration 79 ................................................................... Chapter 6: Intent 81 ......................................................................................................................................................... Section 6.1: Getting a result from another Activity 81 ................................................................................................... Section 6.2: Passing data between activities 83 ............................................................................................................ Section 6.3: Open a URL in a browser 84 ....................................................................................................................... Section 6.4: Starter Pattern 85 ........................................................................................................................................ Section 6.5: Clearing an activity stack 86 ...................................................................................................................... Section 6.6: Start an activity 86 ....................................................................................................................................... Section 6.7: Sending emails 87 ........................................................................................................................................ Section 6.8: CustomTabsIntent for Chrome Custom Tabs 87 ..................................................................................... Section 6.9: Intent URI 88 ................................................................................................................................................. Section 6.10: Start the dialer 89 ....................................................................................................................................... Section 6.11: Broadcasting Messages to Other Components 89 .................................................................................. Section 6.12: Passing custom object between activities 90 .......................................................................................... Section 6.13: Open Google map with specified latitude, longitude 92 ......................................................................... Section 6.14: Passing dierent data through Intent in Activity 92 ............................................................................... Section 6.15: Share intent 94 ............................................................................................................................................ Section 6.16: Showing a File Chooser and Reading the Result 94 ............................................................................... Section 6.17: Sharing Multiple Files through Intent 96 ................................................................................................... Section 6.18: Start Unbound Service using an Intent 96 ............................................................................................... Section 6.19: Getting a result from Activity to Fragment 97 ........................................................................................ Chapter 7: JSON in Android with org.json 99 ..................................................................................................... Section 7.1: Creating a simple JSON object 99 .............................................................................................................. Section 7.2: Create a JSON String with null value 99 ................................................................................................... Section 7.3: Add JSONArray to JSONObject 99 ............................................................................................................ Section 7.4: Parse simple JSON object 100 ................................................................................................................... Section 7.5: Check for the existence of fields on JSON 101 ......................................................................................... Section 7.6: Create nested JSON object 101 ................................................................................................................. Section 7.7: Updating the elements in the JSON 102 ................................................................................................... Section 7.8: Using JsonReader to read JSON from a stream 102 ............................................................................. Section 7.9: Working with null-string when parsing json 104 ...................................................................................... Section 7.10: Handling dynamic key for JSON response 105 ...................................................................................... Chapter 8: Android Studio 107 ................................................................................................................................... Section 8.1: Setup Android Studio 107 ............................................................................................................................ Section 8.2: View And Add Shortcuts in Android Studio 107 ........................................................................................ Section 8.3: Android Studio useful shortcuts 108 .......................................................................................................... Section 8.4: Android Studio Improve performance tip 109 .......................................................................................... Section 8.5: Gradle build project takes forever 110 ...................................................................................................... Section 8.6: Enable/Disable blank line copy 110 .......................................................................................................... Section 8.7: Custom colors of logcat message based on message importance 111 ............................................... Section 8.8: Filter logs from UI 112 ................................................................................................................................. Section 8.9: Create filters configuration 113 .................................................................................................................
Section 8.10: Create assets folder 114 ........................................................................................................................... Chapter 9: Resources 116 ............................................................................................................................................. Section 9.1: Define colors 116 .......................................................................................................................................... Section 9.2: Color Transparency(Alpha) Level 117 ...................................................................................................... Section 9.3: Define String Plurals 117 ............................................................................................................................. Section 9.4: Define strings 118 ........................................................................................................................................ Section 9.5: Define dimensions 119 ................................................................................................................................ Section 9.6: String formatting in strings.xml 119 ........................................................................................................... Section 9.7: Define integer array 120 ............................................................................................................................. Section 9.8: Define a color state list 120 ........................................................................................................................ Section 9.9: 9 Patches 121 ............................................................................................................................................... Section 9.10: Getting resources without "deprecated" warnings 124 ......................................................................... Section 9.11: Working with strings.xml file 124 ............................................................................................................... Section 9.12: Define string array 125 .............................................................................................................................. Section 9.13: Define integers 126 .................................................................................................................................... Section 9.14: Define a menu resource and use it inside Activity/Fragment 126 ....................................................... Chapter 10: Data Binding Library 128 ..................................................................................................................... Section 10.1: Basic text field binding 128 ........................................................................................................................ Section 10.2: Built-in two-way Data Binding 129 ........................................................................................................... Section 10.3: Custom event using lambda expression 130 .......................................................................................... Section 10.4: Default value in Data Binding 132 ............................................................................................................ Section 10.5: Databinding in Dialog 132 ......................................................................................................................... Section 10.6: Binding with an accessor method 132 ..................................................................................................... Section 10.7: Pass widget as reference in BindingAdapter 133 ................................................................................... Section 10.8: Click listener with Binding 134 ................................................................................................................... Section 10.9: Data binding in RecyclerView Adapter 135 ............................................................................................. Section 10.10: Databinding in Fragment 136 ................................................................................................................. Section 10.11: DataBinding with custom variables(int,boolean) 137 ............................................................................ Section 10.12: Referencing classes 137 ........................................................................................................................... Chapter 11: Exceptions 139 ............................................................................................................................................ Section 11.1: ActivityNotFoundException 139 .................................................................................................................. Section 11.2: OutOfMemoryError 139 .............................................................................................................................. Section 11.3: Registering own Handler for unexpected exceptions 139 ...................................................................... Section 11.4: UncaughtException 141 .............................................................................................................................. Section 11.5: NetworkOnMainThreadException 141 ...................................................................................................... Section 11.6: DexException 143 ........................................................................................................................................ Chapter 12: Getting Calculated View Dimensions 144 .................................................................................... Section 12.1: Calculating initial View dimensions in an Activity 144 ............................................................................. Chapter 13: AsyncTask 145 ........................................................................................................................................... Section 13.1: Basic Usage 145 .......................................................................................................................................... Section 13.2: Pass Activity as WeakReference to avoid memory leaks 147 .............................................................. Section 13.3: Download Image using AsyncTask in Android 148 ................................................................................ Section 13.4: Canceling AsyncTask 151 .......................................................................................................................... Section 13.5: AsyncTask: Serial Execution and Parallel Execution of Task 151 .......................................................... Section 13.6: Order of execution 154 .............................................................................................................................. Section 13.7: Publishing progress 154 ............................................................................................................................. Chapter 14: SharedPreferences 156 ........................................................................................................................ Section 14.1: Implementing a Settings screen using SharedPreferences 156 ............................................................. Section 14.2: Commit vs. Apply 158 ................................................................................................................................ Section 14.3: Read and write values to SharedPreferences 158 ..................................................................................
Section 14.4: Retrieve all stored entries from a particular SharedPreferences file 159 ............................................ Section 14.5: Reading and writing data to SharedPreferences with Singleton 160 ................................................... Section 14.6: getPreferences(int) VS getSharedPreferences(String, int) 164 ............................................................. Section 14.7: Listening for SharedPreferences changes 164 ....................................................................................... Section 14.8: Store, Retrieve, Remove and Clear Data from SharedPreferences 165 .............................................. Section 14.9: Add filter for EditTextPreference 165 ....................................................................................................... Section 14.10: Supported data types in SharedPreferences 166 ................................................................................. Section 14.11: Dierent ways of instantiating an object of SharedPreferences 166 .................................................. Section 14.12: Removing keys 167 ................................................................................................................................... Section 14.13: Support pre-Honeycomb with StringSet 167 ......................................................................................... Chapter 15: Emulator 169 .............................................................................................................................................. Section 15.1: Taking screenshots 169 .............................................................................................................................. Section 15.2: Simulate call 174 ......................................................................................................................................... Section 15.3: Open the AVD Manager 174 ..................................................................................................................... Section 15.4: Resolving Errors while starting emulator 174 ......................................................................................... Chapter 16: Material Design 176 ................................................................................................................................ Section 16.1: Adding a Toolbar 176 ................................................................................................................................. Section 16.2: Buttons styled with Material Design 177 .................................................................................................. Section 16.3: Adding a FloatingActionButton (FAB) 178 ............................................................................................... Section 16.4: RippleDrawable 179 ................................................................................................................................... Section 16.5: Adding a TabLayout 184 ........................................................................................................................... Section 16.6: Bottom Sheets in Design Support Library 186 ........................................................................................ Section 16.7: Apply an AppCompat theme 189 ............................................................................................................. Section 16.8: Add a Snackbar 190 ................................................................................................................................... Section 16.9: Add a Navigation Drawer 191 ................................................................................................................... Section 16.10: How to use TextInputLayout 194 ............................................................................................................. Chapter 17: Lint Warnings 195 .................................................................................................................................... Section 17.1: Using tools:ignore in xml files 195 ............................................................................................................. Section 17.2: Configure LintOptions with gradle 195 ..................................................................................................... Section 17.3: Configuring lint checking in Java and XML source files 196 .................................................................. Section 17.4: How to configure the lint.xml file 196 ....................................................................................................... Section 17.5: Mark Suppress Warnings 197 ................................................................................................................... Section 17.6: Importing resources without "Deprecated" error 197 ............................................................................ Chapter 18: Service 199 .................................................................................................................................................. Section 18.1: Lifecycle of a Service 199 ........................................................................................................................... Section 18.2: Defining the process of a service 200 ...................................................................................................... Section 18.3: Creating an unbound service 200 ............................................................................................................. Section 18.4: Starting a Service 203 ................................................................................................................................ Section 18.5: Creating Bound Service with help of Binder 203 ..................................................................................... Section 18.6: Creating Remote Service (via AIDL) 204 .................................................................................................. Chapter 19: Storing Files in Internal & External Storage 206 ...................................................................... Section 19.1: Android: Internal and External Storage - Terminology Clarification 206 .............................................. Section 19.2: Using External Storage 210 ....................................................................................................................... Section 19.3: Using Internal Storage 211 ........................................................................................................................ Section 19.4: Fetch Device Directory : 211 ..................................................................................................................... Section 19.5: Save Database on SD Card (Backup DB on SD) 213 ............................................................................. Chapter 20: WebView 215 ............................................................................................................................................. Section 20.1: Troubleshooting WebView by printing console messages or by remote debugging 215 ................. Section 20.2: Communication from Javascript to Java (Android) 216 ...................................................................... Section 20.3: Communication from Java to Javascript 217 .......................................................................................
Section 20.4: Open dialer example 217 ......................................................................................................................... Section 20.5: Open Local File / Create dynamic content in Webview 218 ................................................................ Section 20.6: JavaScript alert dialogs in WebView - How to make them work 218 ................................................. Chapter 21: Project SDK versions 220 ..................................................................................................................... Section 21.1: Defining project SDK versions 220 ............................................................................................................ Chapter 22: RecyclerView 221 .................................................................................................................................... Section 22.1: Adding a RecyclerView 221 ....................................................................................................................... Section 22.2: Smoother loading of items 222 ................................................................................................................ Section 22.3: RecyclerView with DataBinding 223 ........................................................................................................ Section 22.4: Animate data change 224 ........................................................................................................................ Section 22.5: Popup menu with recyclerView 228 ........................................................................................................ Section 22.6: Using several ViewHolders with ItemViewType 230 .............................................................................. Section 22.7: Filter items inside RecyclerView with a SearchView 231 ....................................................................... Section 22.8: Drag&Drop and Swipe with RecyclerView 232 ...................................................................................... Section 22.9: Show default view till items load or when data is not available 233 ................................................... Section 22.10: Add header/footer to a RecyclerView 235 ........................................................................................... Section 22.11: Endless Scrolling in Recycleview 238 ...................................................................................................... Section 22.12: Add divider lines to RecyclerView items 238 ......................................................................................... Chapter 23: Google Maps API v2 for Android 241 ............................................................................................. Section 23.1: Custom Google Map Styles 241 ................................................................................................................ Section 23.2: Default Google Map Activity 252 ............................................................................................................. Section 23.3: Show Current Location in a Google Map 253 ......................................................................................... Section 23.4: Change Oset 259 ..................................................................................................................................... Section 23.5: MapView: embedding a GoogleMap in an existing layout 259 ............................................................. Section 23.6: Get debug SHA1 fingerprint 261 ............................................................................................................... Section 23.7: Adding markers to a map 262 ................................................................................................................. Section 23.8: UISettings 262 ............................................................................................................................................ Section 23.9: InfoWindow Click Listener 263 ................................................................................................................. Section 23.10: Obtaining the SH1-Fingerprint of your certificate keystore file 264 ................................................... Section 23.11: Do not launch Google Maps when the map is clicked (lite mode) 265 ............................................... Chapter 24: PorterDu Mode 266 ............................................................................................................................ Section 24.1: Creating a PorterDu ColorFilter 266 ...................................................................................................... Section 24.2: Creating a PorterDu XferMode 266 ...................................................................................................... Section 24.3: Apply a radial mask (vignette) to a bitmap using PorterDuXfermode 266 ..................................... Chapter 25: 9-Patch Images 268 ............................................................................................................................... Section 25.1: Basic rounded corners 268 ....................................................................................................................... Section 25.2: Optional padding lines 268 ....................................................................................................................... Section 25.3: Basic spinner 269 ....................................................................................................................................... Chapter 26: Android NDK 270 ...................................................................................................................................... Section 26.1: How to log in ndk 270 ................................................................................................................................ Section 26.2: Building native executables for Android 270 .......................................................................................... Section 26.3: How to clean the build 271 ....................................................................................................................... Section 26.4: How to use a makefile other than Android.mk 271 ............................................................................... Chapter 27: RecyclerView Decorations 272 ......................................................................................................... Section 27.1: Add divider to RecyclerView 272 .............................................................................................................. Section 27.2: Drawing a Separator 274 ......................................................................................................................... Section 27.3: How to add dividers using and DividerItemDecoration 275 ................................................................. Section 27.4: Per-item margins with ItemDecoration 275 ............................................................................................ Section 27.5: ItemOsetDecoration for GridLayoutManager in RecycleView 276 ...................................................
Chapter 28: Camera 2 API 278 .................................................................................................................................... Section 28.1: Preview the main camera in a TextureView 278 ..................................................................................... Chapter 29: ViewPager 287 .......................................................................................................................................... Section 29.1: ViewPager with a dots indicator 287 ........................................................................................................ Section 29.2: Basic ViewPager usage with fragments 289 .......................................................................................... Section 29.3: ViewPager with PreferenceFragment 290 .............................................................................................. Section 29.4: Adding a ViewPager 291 .......................................................................................................................... Section 29.5: Setup OnPageChangeListener 292 .......................................................................................................... Section 29.6: ViewPager with TabLayout 293 ............................................................................................................... Chapter 30: CardView 296 ............................................................................................................................................ Section 30.1: Getting Started with CardView 296 .......................................................................................................... Section 30.2: Adding Ripple animation 297 ................................................................................................................... Section 30.3: Customizing the CardView 297 ................................................................................................................ Section 30.4: Using Images as Background in CardView (Pre-Lollipop device issues) 298 ..................................... Section 30.5: Animate CardView background color with TransitionDrawable 300 ................................................... Chapter 31: HttpURLConnection 301 ....................................................................................................................... Section 31.1: Creating an HttpURLConnection 301 ........................................................................................................ Section 31.2: Sending an HTTP GET request 301 ........................................................................................................... Section 31.3: Reading the body of an HTTP GET request 302 ..................................................................................... Section 31.4: Sending an HTTP POST request with parameters 302 ........................................................................... Section 31.5: A multi-purpose HttpURLConnection class to handle all types of HTTP requests 303 ...................... Section 31.6: Use HttpURLConnection for multipart/form-data 306 .......................................................................... Section 31.7: Upload (POST) file using HttpURLConnection 309 ................................................................................. Chapter 32: SQLite 311 ................................................................................................................................................... Section 32.1: onUpgrade() method 311 .......................................................................................................................... Section 32.2: Reading data from a Cursor 311 ............................................................................................................. Section 32.3: Using the SQLiteOpenHelper class 313 ................................................................................................... Section 32.4: Insert data into database 314 .................................................................................................................. Section 32.5: Bulk insert 314 ............................................................................................................................................ Section 32.6: Create a Contract, Helper and Provider for SQLite in Android 315 ..................................................... Section 32.7: Delete row(s) from the table 319 ............................................................................................................. Section 32.8: Updating a row in a table 320 .................................................................................................................. Section 32.9: Performing a Transaction 320 ................................................................................................................. Section 32.10: Create Database from assets folder 321 .............................................................................................. Section 32.11: Store image into SQLite 323 .................................................................................................................... Section 32.12: Exporting and importing a database 325 .............................................................................................. Chapter 33: ADB (Android Debug Bridge) 327 .................................................................................................... Section 33.1: Connect ADB to a device via WiFi 327 ..................................................................................................... Section 33.2: Direct ADB command to specific device in a multi-device setting 329 ............................................... Section 33.3: Taking a screenshot and video (for kitkat only) from a device display 329 ....................................... Section 33.4: Pull (push) files from (to) the device 330 ................................................................................................ Section 33.5: Print verbose list of connected devices 331 ............................................................................................ Section 33.6: View logcat 331 .......................................................................................................................................... Section 33.7: View and pull cache files of an app 332 .................................................................................................. Section 33.8: Clear application data 332 ....................................................................................................................... Section 33.9: View an app's internal data (data/data/<sample.package.id>) on a device 333 .............................. Section 33.10: Install and run an application 333 .......................................................................................................... Section 33.11: Sending broadcast 333 ............................................................................................................................. Section 33.12: Backup 334 ................................................................................................................................................ Section 33.13: View available devices 335 ......................................................................................................................
Section 33.14: Connect device by IP 335 ........................................................................................................................ Section 33.15: Install ADB on Linux system 336 ............................................................................................................. Section 33.16: View activity stack 336 ............................................................................................................................. Section 33.17: Reboot device 336 .................................................................................................................................... Section 33.18: Read device information 337 .................................................................................................................. Section 33.19: List all permissions that require runtime grant from users on Android 6.0 337 ................................ Section 33.20: Turn on/o Wifi 337 ................................................................................................................................ Section 33.21: Start/stop adb 337 ................................................................................................................................... Chapter 34: ButterKnife 338 ........................................................................................................................................ Section 34.1: Configuring ButterKnife in your project 338 ............................................................................................ Section 34.2: Unbinding views in ButterKnife 340 ......................................................................................................... Section 34.3: Binding Listeners using ButterKnife 340 .................................................................................................. Section 34.4: Android Studio ButterKnife Plugin 341 .................................................................................................... Section 34.5: Binding Views using ButterKnife 342 ....................................................................................................... Chapter 35: Supporting Screens With Dierent Resolutions, Sizes 345 ............................................... Section 35.1: Using configuration qualifiers 345 ............................................................................................................ Section 35.2: Converting dp and sp to pixels 345 ......................................................................................................... Section 35.3: Text size and dierent android screen sizes 346 ................................................................................... Chapter 36: Glide 347 ....................................................................................................................................................... Section 36.1: Loading an image 347 ............................................................................................................................... Section 36.2: Add Glide to your project 348 .................................................................................................................. Section 36.3: Glide circle transformation (Load image in a circular ImageView) 348 .............................................. Section 36.4: Default transformations 349 .................................................................................................................... Section 36.5: Glide rounded corners image with custom Glide target 350 ................................................................ Section 36.6: Placeholder and Error handling 350 ........................................................................................................ Section 36.7: Preloading images 351 .............................................................................................................................. Section 36.8: Handling Glide image load failed 351 ..................................................................................................... Section 36.9: Load image in a circular ImageView without custom transformations 352 ....................................... Chapter 37: Retrofit2 353 .............................................................................................................................................. Section 37.1: A Simple GET Request 353 ......................................................................................................................... Section 37.2: Debugging with Stetho 355 ...................................................................................................................... Section 37.3: Add logging to Retrofit2 356 .................................................................................................................... Section 37.4: A simple POST request with GSON 356 ................................................................................................... Section 37.5: Download a file from Server using Retrofit2 358 ................................................................................... Section 37.6: Upload multiple file using Retrofit as multipart 360 .............................................................................. Section 37.7: Retrofit with OkHttp interceptor 363 ........................................................................................................ Section 37.8: Header and Body: an Authentication Example 363 ............................................................................... Section 37.9: Uploading a file via Multipart 364 ............................................................................................................ Section 37.10: Retrofit 2 Custom Xml Converter 364 .................................................................................................... Section 37.11: Reading XML form URL with Retrofit 2 366 ............................................................................................ Chapter 38: Dialog 369 ................................................................................................................................................... Section 38.1: Adding Material Design AlertDialog to your app using Appcompat 369 ............................................. Section 38.2: A Basic Alert Dialog 369 ............................................................................................................................ Section 38.3: ListView in AlertDialog 370 ....................................................................................................................... Section 38.4: Custom Alert Dialog with EditText 371 .................................................................................................... Section 38.5: DatePickerDialog 372 ................................................................................................................................ Section 38.6: DatePicker 372 ........................................................................................................................................... Section 38.7: Alert Dialog 373 ......................................................................................................................................... Section 38.8: Alert Dialog with Multi-line Title 374 ........................................................................................................ Section 38.9: Date Picker within DialogFragment 376 .................................................................................................
Section 38.10: Fullscreen Custom Dialog with no background and no title 378 ........................................................ Chapter 39: ACRA 380 ...................................................................................................................................................... Section 39.1: ACRAHandler 380 ....................................................................................................................................... Section 39.2: Example manifest 380 ............................................................................................................................... Section 39.3: Installation 381 ........................................................................................................................................... Chapter 40: GreenDAO 382 .......................................................................................................................................... Section 40.1: Helper methods for SELECT, INSERT, DELETE, UPDATE queries 382 ................................................... Section 40.2: Creating an Entity with GreenDAO 3.X that has a Composite Primary Key 384 ................................ Section 40.3: Getting started with GreenDao v3.X 385 ................................................................................................. Chapter 41: Formatting Strings 387 ........................................................................................................................ Section 41.1: Format a string resource 387 .................................................................................................................... Section 41.2: Formatting data types to String and vise versa 387 .............................................................................. Section 41.3: Format a timestamp to string 387 ............................................................................................................ Chapter 42: Notifications 388 ..................................................................................................................................... Section 42.1: Heads Up Notification with Ticker for older devices 388 ....................................................................... Section 42.2: Creating a simple Notification 392 .......................................................................................................... Section 42.3: Set custom notification - show full content text 392 .............................................................................. Section 42.4: Dynamically getting the correct pixel size for the large icon 393 ........................................................ Section 42.5: Ongoing notification with Action button 393 .......................................................................................... Section 42.6: Setting Dierent priorities in notification 394 ......................................................................................... Section 42.7: Set custom notification icon using `Picasso` library 395 ........................................................................ Section 42.8: Scheduling notifications 396 ..................................................................................................................... Chapter 43: AlarmManager 398 ................................................................................................................................ Section 43.1: How to Cancel an Alarm 398 ..................................................................................................................... Section 43.2: Creating exact alarms on all Android versions 398 ............................................................................... Section 43.3: API23+ Doze mode interferes with AlarmManager 399 ........................................................................ Section 43.4: Run an intent at a later time 399 ............................................................................................................. Chapter 44: Fragments 400 ........................................................................................................................................ Section 44.1: Pass data from Activity to Fragment using Bundle 400 ........................................................................ Section 44.2: The newInstance() pattern 400 ................................................................................................................ Section 44.3: Navigation between fragments using backstack and static fabric pattern 401 ................................ Section 44.4: Sending events back to an activity with callback interface 402 .......................................................... Section 44.5: Animate the transition between fragments 403 .................................................................................... Section 44.6: Communication between Fragments 404 ............................................................................................... Chapter 45: Handler 409 ............................................................................................................................................... Section 45.1: HandlerThreads and communication between Threads 409 ................................................................ Section 45.2: Use Handler to create a Timer (similar to javax.swing.Timer) 409 ...................................................... Section 45.3: Using a Handler to execute code after a delayed amount of time 410 .............................................. Section 45.4: Stop handler from execution 411 ............................................................................................................ Chapter 46: Creating Custom Views 412 ............................................................................................................... Section 46.1: Creating Custom Views 412 ...................................................................................................................... Section 46.2: Adding attributes to views 414 ................................................................................................................. Section 46.3: CustomView performance tips 416 ......................................................................................................... Section 46.4: Creating a compound view 417 ............................................................................................................... Section 46.5: Compound view for SVG/VectorDrawable as drawableRight 420 ...................................................... Section 46.6: Responding to Touch Events 423 ............................................................................................................ Chapter 47: BroadcastReceiver 424 ....................................................................................................................... Section 47.1: Using LocalBroadcastManager 424 ......................................................................................................... Section 47.2: BroadcastReceiver Basics 424 .................................................................................................................
Section 47.3: Introduction to Broadcast receiver 425 ................................................................................................... Section 47.4: Using ordered broadcasts 425 ................................................................................................................. Section 47.5: Sticky Broadcast 426 ................................................................................................................................. Section 47.6: Enabling and disabling a Broadcast Receiver programmatically 426 ................................................ Section 47.7: Example of a LocalBroadcastManager 427 ........................................................................................... Section 47.8: Android stopped state 428 ....................................................................................................................... Section 47.9: Communicate two activities through custom Broadcast receiver 428 ................................................ Section 47.10: BroadcastReceiver to handle BOOT_COMPLETED events 429 .......................................................... Section 47.11: Bluetooth Broadcast receiver 430 ........................................................................................................... Chapter 48: Activity 431 ................................................................................................................................................ Section 48.1: Activity launchMode 431 ............................................................................................................................ Section 48.2: Exclude an activity from back-stack history 432 ................................................................................... Section 48.3: Android Activity LifeCycle Explained 432 ................................................................................................ Section 48.4: End Application with exclude from Recents 435 .................................................................................... Section 48.5: Presenting UI with setContentView 436 .................................................................................................. Section 48.6: Up Navigation for Activities 437 ............................................................................................................... Section 48.7: Clear your current Activity stack and launch a new Activity 438 ......................................................... Chapter 49: Snackbar 440 ............................................................................................................................................ Section 49.1: Creating a simple Snackbar 440 ............................................................................................................... Section 49.2: Custom Snack Bar 440 .............................................................................................................................. Section 49.3: Custom Snackbar (no need view) 441 .................................................................................................... Section 49.4: Snackbar with Callback 442 ..................................................................................................................... Section 49.5: Snackbar vs Toasts: Which one should I use? 442 ................................................................................. Section 49.6: Custom Snackbar 443 ............................................................................................................................... Chapter 50: Runtime Permissions in API-23 + 444 ............................................................................................ Section 50.1: Android 6.0 multiple permissions 444 ...................................................................................................... Section 50.2: Multiple Runtime Permissions From Same Permission Groups 445 ..................................................... Section 50.3: Using PermissionUtil 446 .......................................................................................................................... Section 50.4: Include all permission-related code to an abstract base class and extend the activity of this base class to achieve cleaner/reusable code 447 ............................................................................................... Section 50.5: Enforcing Permissions in Broadcasts, URI 449 ....................................................................................... Chapter 51: Logging and using Logcat 451 .......................................................................................................... Section 51.1: Filtering the logcat output 451 ................................................................................................................... Section 51.2: Logging 452 ................................................................................................................................................ Section 51.3: Using the Logcat 454 ................................................................................................................................. Section 51.4: Log with link to source directly from Logcat 455 ................................................................................... Section 51.5: Clear logs 455 ............................................................................................................................................. Section 51.6: Android Studio usage 455 ......................................................................................................................... Section 51.7: Generating Logging code 456 ................................................................................................................... Chapter 52: VectorDrawable and AnimatedVectorDrawable 458 .......................................................... Section 52.1: Basic VectorDrawable 458 ........................................................................................................................ Section 52.2: <group> tags 458 ....................................................................................................................................... Section 52.3: Basic AnimatedVectorDrawable 459 ....................................................................................................... Section 52.4: Using Strokes 460 ...................................................................................................................................... Section 52.5: Using <clip-path> 462 ................................................................................................................................ Section 52.6: Vector compatibility through AppCompat 462 ...................................................................................... Chapter 53: Tools Attributes 464 ............................................................................................................................... Section 53.1: Designtime Layout Attributes 464 ............................................................................................................ Chapter 54: Toast 465 ....................................................................................................................................................
Section 54.1: Creating a custom Toast 465 .................................................................................................................... Section 54.2: Set position of a Toast 466 ....................................................................................................................... Section 54.3: Showing a Toast Message 466 ................................................................................................................. Section 54.4: Show Toast Message Above Soft Keyboard 467 ................................................................................... Section 54.5: Thread safe way of displaying Toast (Application Wide) 467 ............................................................. Section 54.6: Thread safe way of displaying a Toast Message (For AsyncTask) 468 ............................................. Chapter 55: Interfaces 469 ........................................................................................................................................... Section 55.1: Custom Listener 469 ................................................................................................................................... Section 55.2: Basic Listener 470 ...................................................................................................................................... Chapter 56: Animators 472 ........................................................................................................................................... Section 56.1: TransitionDrawable animation 472 .......................................................................................................... Section 56.2: Fade in/out animation 472 ....................................................................................................................... Section 56.3: ValueAnimator 473 .................................................................................................................................... Section 56.4: Expand and Collapse animation of View 474 ......................................................................................... Section 56.5: ObjectAnimator 475 .................................................................................................................................. Section 56.6: ViewPropertyAnimator 475 ...................................................................................................................... Section 56.7: Shake animation of an ImageView 476 .................................................................................................. Chapter 57: Location 478 .............................................................................................................................................. Section 57.1: Fused location API 478 ............................................................................................................................... Section 57.2: Get Address From Location using Geocoder 482 .................................................................................. Section 57.3: Requesting location updates using LocationManager 483 ................................................................... Section 57.4: Requesting location updates on a separate thread using LocationManager 484 ............................. Section 57.5: Getting location updates in a BroadcastReceiver 485 .......................................................................... Section 57.6: Register geofence 486 .............................................................................................................................. Chapter 58: Theme, Style, Attribute 490 ............................................................................................................... Section 58.1: Define primary, primary dark, and accent colors 490 ........................................................................... Section 58.2: Multiple Themes in one App 490 .............................................................................................................. Section 58.3: Navigation Bar Color (API 21+) 492 ......................................................................................................... Section 58.4: Use Custom Theme Per Activity 492 ....................................................................................................... Section 58.5: Light Status Bar (API 23+) 493 ................................................................................................................. Section 58.6: Use Custom Theme Globally 493 ............................................................................................................. Section 58.7: Overscroll Color (API 21+) 493 .................................................................................................................. Section 58.8: Ripple Color (API 21+) 493 ......................................................................................................................... Section 58.9: Translucent Navigation and Status Bars (API 19+) 494 ......................................................................... Section 58.10: Theme inheritance 494 ............................................................................................................................ Chapter 59: The Manifest File 495 ............................................................................................................................ Section 59.1: Declaring Components 495 ....................................................................................................................... Section 59.2: Declaring permissions in your manifest file 495 .................................................................................... Chapter 60: Parcelable 497 .......................................................................................................................................... Section 60.1: Making a custom object Parcelable 497 .................................................................................................. Section 60.2: Parcelable object containing another Parcelable object 498 ............................................................... Section 60.3: Using Enums with Parcelable 499 ............................................................................................................ Chapter 61: MediaPlayer 501 ....................................................................................................................................... Section 61.1: Basic creation and playing 501 ................................................................................................................. Section 61.2: Media Player with Buer progress and play position 501 ..................................................................... Section 61.3: Getting system ringtones 503 ................................................................................................................... Section 61.4: Asynchronous prepare 504 ....................................................................................................................... Section 61.5: Import audio into androidstudio and play it 504 .................................................................................... Section 61.6: Getting and setting system volume 506 ..................................................................................................
Chapter 62: Multidex and the Dex Method Limit 508 ...................................................................................... Section 62.1: Enabling Multidex 508 ................................................................................................................................ Section 62.2: Multidex by extending Application 509 ................................................................................................... Section 62.3: Multidex by extending MultiDexApplication 509 ..................................................................................... Section 62.4: Multidex by using MultiDexApplication directly 510 ............................................................................... Section 62.5: Counting Method References On Every Build (Dexcount Gradle Plugin) 510 ..................................... Chapter 63: Data Synchronization with Sync Adapter 512 .......................................................................... Section 63.1: Dummy Sync Adapter with Stub Provider 512 ........................................................................................ Chapter 64: Menu 518 ..................................................................................................................................................... Section 64.1: Options menu with dividers 518 ................................................................................................................ Section 64.2: Apply custom font to Menu 518 ............................................................................................................... Section 64.3: Creating a Menu in an Activity 519 .......................................................................................................... Chapter 65: Instant Run in Android Studio 522 .................................................................................................. Section 65.1: Enabling or disabling Instant Run 522 ...................................................................................................... Section 65.2: Types of code Swaps in Instant Run 522 ................................................................................................ Section 65.3: Unsupported code changes when using Instant Run 523 ..................................................................... Chapter 66: Picasso 524 ................................................................................................................................................. Section 66.1: Adding Picasso Library to your Android Project 524 .............................................................................. Section 66.2: Circular Avatars with Picasso 524 ............................................................................................................ Section 66.3: Placeholder and Error Handling 526 ....................................................................................................... Section 66.4: Re-sizing and Rotating 526 ...................................................................................................................... Section 66.5: Disable cache in Picasso 527 .................................................................................................................... Section 66.6: Using Picasso as ImageGetter for Html.fromHtml 527 ......................................................................... Section 66.7: Cancelling Image Requests using Picasso 528 ....................................................................................... Section 66.8: Loading Image from external Storage 529 ............................................................................................ Section 66.9: Downloading image as Bitmap using Picasso 529 ................................................................................ Section 66.10: Try oine disk cache first, then go online and fetch the image 529 ................................................. Chapter 67: Bluetooth and Bluetooth LE API 531 ............................................................................................. Section 67.1: Permissions 531 .......................................................................................................................................... Section 67.2: Check if bluetooth is enabled 531 ............................................................................................................ Section 67.3: Find nearby Bluetooth Low Energy devices 531 .................................................................................... Section 67.4: Make device discoverable 536 ................................................................................................................. Section 67.5: Connect to Bluetooth device 536 ............................................................................................................. Section 67.6: Find nearby bluetooth devices 538 .......................................................................................................... Chapter 68: RoboGuice 539 .......................................................................................................................................... Section 68.1: Simple example 539 ................................................................................................................................... Section 68.2: Installation for Gradle Projects 539 ......................................................................................................... Section 68.3: @ContentView annotation 539 ................................................................................................................ Section 68.4: @InjectResource annotation 539 ............................................................................................................. Section 68.5: @InjectView annotation 540 ..................................................................................................................... Section 68.6: Introduction to RoboGuice 540 ................................................................................................................ Chapter 69: Memory Leaks 543 ................................................................................................................................. Section 69.1: Avoid leaking Activities with AsyncTask 543 ........................................................................................... Section 69.2: Common memory leaks and how to fix them 544 ................................................................................ Section 69.3: Detect memory leaks with the LeakCanary library 545 ........................................................................ Section 69.4: Anonymous callback in activities 545 ...................................................................................................... Section 69.5: Activity Context in static classes 546 ....................................................................................................... Section 69.6: Avoid leaking Activities with Listeners 547 .............................................................................................. Section 69.7: Avoid memory leaks with Anonymous Class, Handler, Timer Task, Thread 552 ...............................
Chapter 70: Universal Image Loader 554 ............................................................................................................. Section 70.1: Basic usage 554 .......................................................................................................................................... Section 70.2: Initialize Universal Image Loader 554 ..................................................................................................... Chapter 71: Volley 555 .................................................................................................................................................... Section 71.1: Using Volley for HTTP requests 555 .......................................................................................................... Section 71.2: Basic StringRequest using GET method 556 ........................................................................................... Section 71.3: Adding custom design time attributes to NetworkImageView 557 ....................................................... Section 71.4: Adding custom headers to your requests [e.g. for basic auth] 558 ..................................................... Section 71.5: Remote server authentication using StringRequest through POST method 559 ................................ Section 71.6: Cancel a request 561 .................................................................................................................................. Section 71.7: Request JSON 561 ...................................................................................................................................... Section 71.8: Use JSONArray as request body 561 ...................................................................................................... Section 71.9: Boolean variable response from server with json request in volley 562 .............................................. Section 71.10: Helper Class for Handling Volley Errors 563 .......................................................................................... Chapter 72: Widgets 565 ................................................................................................................................................ Section 72.1: Manifest Declaration - 565 ........................................................................................................................ Section 72.2: Metadata 565 ............................................................................................................................................. Section 72.3: AppWidgetProvider Class 565 .................................................................................................................. Section 72.4: Create/Integrate Basic Widget using Android Studio 566 .................................................................... Section 72.5: Two widgets with dierent layouts declaration 567 .............................................................................. Chapter 73: Date and Time Pickers 569 ................................................................................................................. Section 73.1: Date Picker Dialog 569 ............................................................................................................................... Section 73.2: Material DatePicker 569 ............................................................................................................................ Chapter 74: Integrate Google Sign In 572 ............................................................................................................ Section 74.1: Google Sign In with Helper class 572 ....................................................................................................... Chapter 75: In-app Billing 575 ..................................................................................................................................... Section 75.1: Consumable In-app Purchases 575 .......................................................................................................... Section 75.2: (Third party) In-App v3 Library 579 ......................................................................................................... Chapter 76: FloatingActionButton 581 ................................................................................................................... Section 76.1: How to add the FAB to the layout 581 ..................................................................................................... Section 76.2: Show and Hide FloatingActionButton on Swipe 582 .............................................................................. Section 76.3: Show and Hide FloatingActionButton on Scroll 584 ............................................................................... Section 76.4: Setting behaviour of FloatingActionButton 586 ..................................................................................... Chapter 77: ContentProvider 587 ............................................................................................................................. Section 77.1: Implementing a basic content provider class 587 .................................................................................. Chapter 78: Dagger 2 591 ............................................................................................................................................. Section 78.1: Component setup for Application and Activity injection 591 ................................................................. Section 78.2: Custom Scopes 592 ................................................................................................................................... Section 78.3: Using @Subcomponent instead of @Component(dependencies={...}) 593 ........................................ Section 78.4: Creating a component from multiple modules 593 ............................................................................... Section 78.5: How to add Dagger 2 in build.gradle 594 ............................................................................................... Section 78.6: Constructor Injection 595 .......................................................................................................................... Chapter 79: Realm 597 ................................................................................................................................................... Section 79.1: Sorted queries 597 ...................................................................................................................................... Section 79.2: Using Realm with RxJava 597 .................................................................................................................. Section 79.3: Basic Usage 598 ......................................................................................................................................... Section 79.4: List of primitives (RealmList<Integer/String/...>) 601 ............................................................................ Section 79.5: Async queries 602 ...................................................................................................................................... Section 79.6: Adding Realm to your project 602 ...........................................................................................................
Section 79.7: Realm Models 602 ..................................................................................................................................... Section 79.8: try-with-resources 603 .............................................................................................................................. Chapter 80: Unit testing in Android with JUnit 604 .......................................................................................... Section 80.1: Moving Business Logic Out of Android Componenets 604 .................................................................... Section 80.2: Creating Local unit tests 606 .................................................................................................................... Section 80.3: Getting started with JUnit 607 ................................................................................................................. Section 80.4: Exceptions 610 ........................................................................................................................................... Section 80.5: Static import 611 ....................................................................................................................................... Chapter 81: Android Versions 612 ............................................................................................................................. Section 81.1: Checking the Android Version on device at runtime 612 ........................................................................ Chapter 82: Wi-Fi Connections 613 ........................................................................................................................... Section 82.1: Connect with WEP encryption 613 ........................................................................................................... Section 82.2: Connect with WPA2 encryption 613 ........................................................................................................ Section 82.3: Scan for access points 614 ....................................................................................................................... Chapter 83: SensorManager 616 ............................................................................................................................... Section 83.1: Decide if your device is static or not, using the accelerometer 616 ..................................................... Section 83.2: Retrieving sensor events 616 ................................................................................................................... Section 83.3: Sensor transformation to world coordinate system 617 ...................................................................... Chapter 84: Localization with resources in Android 619 .............................................................................. Section 84.1: Configuration types and qualifier names for each folder under the "res" directory 619 .................. Section 84.2: Adding translation to your Android app 620 .......................................................................................... Section 84.3: Type of resource directories under the "res" folder 622 ....................................................................... Section 84.4: Change locale of android application programmatically 622 ............................................................. Section 84.5: Currency 625 .............................................................................................................................................. Chapter 85: ProgressBar 626 ...................................................................................................................................... Section 85.1: Material Linear ProgressBar 626 .............................................................................................................. Section 85.2: Tinting ProgressBar 628 ........................................................................................................................... Section 85.3: Customized progressbar 628 ................................................................................................................... Section 85.4: Creating Custom Progress Dialog 630 .................................................................................................... Section 85.5: Indeterminate ProgressBar 632 ............................................................................................................... Section 85.6: Determinate ProgressBar 633 .................................................................................................................. Chapter 86: Custom Fonts 635 ................................................................................................................................... Section 86.1: Custom font in canvas text 635 ................................................................................................................ Section 86.2: Working with fonts in Android O 635 ....................................................................................................... Section 86.3: Custom font to whole activity 636 ........................................................................................................... Section 86.4: Putting a custom font in your app 636 .................................................................................................... Section 86.5: Initializing a font 636 ................................................................................................................................. Section 86.6: Using a custom font in a TextView 636 ................................................................................................... Section 86.7: Apply font on TextView by xml (Not required Java code) 637 ............................................................ Section 86.8: Ecient Typeface loading 638 ................................................................................................................ Chapter 87: Vibration 639 ............................................................................................................................................. Section 87.1: Getting Started with Vibration 639 ........................................................................................................... Section 87.2: Vibrate Indefinitely 639 ............................................................................................................................. Section 87.3: Vibration Patterns 639 ............................................................................................................................... Section 87.4: Stop Vibrate 640 ........................................................................................................................................ Section 87.5: Vibrate for one time 640 ........................................................................................................................... Chapter 88: Google Awareness APIs 641 ............................................................................................................... Section 88.1: Get changes for location within a certain range using Fence API 641 ................................................. Section 88.2: Get current location using Snapshot API 642 .........................................................................................
Section 88.3: Get changes in user activity with Fence API 642 .................................................................................... Section 88.4: Get current user activity using Snapshot API 643 .................................................................................. Section 88.5: Get headphone state with Snapshot API 643 ......................................................................................... Section 88.6: Get nearby places using Snapshot API 644 ............................................................................................ Section 88.7: Get current weather using Snapshot API 644 ......................................................................................... Chapter 89: Text to Speech(TTS) 645 ..................................................................................................................... Section 89.1: Text to Speech Base 645 ........................................................................................................................... Section 89.2: TextToSpeech implementation across the APIs 646 ............................................................................. Chapter 90: UI Lifecycle 650 ........................................................................................................................................ Section 90.1: Saving data on memory trimming 650 .................................................................................................... Chapter 91: Spinner 651 .................................................................................................................................................. Section 91.1: Basic Spinner Example 651 ........................................................................................................................ Section 91.2: Adding a spinner to your activity 652 ...................................................................................................... Chapter 92: Data Encryption/Decryption 654 ................................................................................................... Section 92.1: AES encryption of data using password in a secure way 654 .............................................................. Chapter 93: Testing UI with Espresso 656 ............................................................................................................. Section 93.1: Overall Espresso 656 .................................................................................................................................. Section 93.2: Espresso simple UI test 658 ...................................................................................................................... Section 93.3: Open Close DrawerLayout 661 ................................................................................................................ Section 93.4: Set Up Espresso 662 .................................................................................................................................. Section 93.5: Performing an action on a view 663 ........................................................................................................ Section 93.6: Finding a view with onView 663 ............................................................................................................... Section 93.7: Create Espresso Test Class 663 ............................................................................................................... Section 93.8: Up Navigation 664 ..................................................................................................................................... Section 93.9: Group a collection of test classes in a test suite 664 ............................................................................. Section 93.10: Espresso custom matchers 665 .............................................................................................................. Chapter 94: Writing UI tests - Android 668 .......................................................................................................... Section 94.1: MockWebServer example 668 .................................................................................................................. Section 94.2: IdlingResource 670 .................................................................................................................................... Chapter 95: GreenRobot EventBus 674 .................................................................................................................. Section 95.1: Passing a Simple Event 674 ....................................................................................................................... Section 95.2: Receiving Events 675 ................................................................................................................................ Section 95.3: Sending Events 675 ................................................................................................................................... Chapter 96: OkHttp 676 .................................................................................................................................................. Section 96.1: Basic usage example 676 .......................................................................................................................... Section 96.2: Setting up OkHttp 676 ............................................................................................................................... Section 96.3: Logging interceptor 676 ............................................................................................................................ Section 96.4: Synchronous Get Call 677 ......................................................................................................................... Section 96.5: Asynchronous Get Call 677 ....................................................................................................................... Section 96.6: Posting form parameters 678 .................................................................................................................. Section 96.7: Posting a multipart request 678 ............................................................................................................... Section 96.8: Rewriting Responses 678 .......................................................................................................................... Chapter 97: Enhancing Android Performance Using Icon Fonts 680 ...................................................... Section 97.1: How to integrate Icon fonts 680 ............................................................................................................... Section 97.2: TabLayout with icon fonts 682 ................................................................................................................. Chapter 98: Handling Deep Links 684 ..................................................................................................................... Section 98.1: Retrieving query parameters 684 ............................................................................................................ Section 98.2: Simple deep link 684 ................................................................................................................................. Section 98.3: Multiple paths on a single domain 685 ....................................................................................................
Section 98.4: Multiple domains and multiple paths 685 ............................................................................................... Section 98.5: Both http and https for the same domain 686 ....................................................................................... Section 98.6: Using pathPrefix 686 ................................................................................................................................. Chapter 99: Canvas drawing using SurfaceView 687 ..................................................................................... Section 99.1: SurfaceView with drawing thread 687 ..................................................................................................... Chapter 100: Firebase 692 ............................................................................................................................................ Section 100.1: Add Firebase to Your Android Project 692 ............................................................................................. Section 100.2: Updating a Firebase users's email 693 .................................................................................................. Section 100.3: Create a Firebase user 694 ..................................................................................................................... Section 100.4: Change Password 695 ............................................................................................................................. Section 100.5: Firebase Cloud Messaging 696 ............................................................................................................... Section 100.6: Firebase Storage Operations 698 .......................................................................................................... Section 100.7: Firebase Realtime Database: how to set/get data 704 ....................................................................... Section 100.8: Demo of FCM based notifications 705 ................................................................................................... Section 100.9: Sign In Firebase user with email and password 715 ............................................................................ Section 100.10: Send Firebase password reset email 716 ............................................................................................ Section 100.11: Re-Authenticate Firebase user 718 ....................................................................................................... Section 100.12: Firebase Sign Out 719 ............................................................................................................................ Chapter 101: Crash Reporting Tools 720 ................................................................................................................ Section 101.1: Fabric - Crashlytics 720 ............................................................................................................................. Section 101.2: Capture crashes using Sherlock 725 ....................................................................................................... Section 101.3: Force a Test Crash With Fabric 726 ........................................................................................................ Section 101.4: Crash Reporting with ACRA 727 .............................................................................................................. Chapter 102: Check Internet Connectivity 729 ................................................................................................... Section 102.1: Check if device has internet connectivity 729 ........................................................................................ Section 102.2: How to check network strength in android? 729 .................................................................................. Section 102.3: How to check network strength 730 ...................................................................................................... Chapter 103: Facebook SDK for Android 733 ...................................................................................................... Section 103.1: How to add Facebook Login in Android 733 .......................................................................................... Section 103.2: Create your own custom button for Facebook login 735 .................................................................... Section 103.3: A minimalistic guide to Facebook login/signup implementation 736 ................................................ Section 103.4: Setting permissions to access data from the Facebook profile 737 .................................................. Section 103.5: Logging out of Facebook 737 ................................................................................................................. Chapter 104: Unzip File in Android 738 ................................................................................................................... Section 104.1: Unzip file 738 ............................................................................................................................................. Chapter 105: Android Places API 739 ....................................................................................................................... Section 105.1: Getting Current Places by Using Places API 739 ................................................................................... Section 105.2: Place Autocomplete Integration 740 ..................................................................................................... Section 105.3: Place Picker Usage Example 741 ........................................................................................................... Section 105.4: Setting place type filters for PlaceAutocomplete 742 .......................................................................... Section 105.5: Adding more than one google auto complete activity 743 ................................................................. Chapter 106: Creating your own libraries for Android applications 745 .............................................. Section 106.1: Create a library available on Jitpack.io 745 .......................................................................................... Section 106.2: Creating library project 745 .................................................................................................................... Section 106.3: Using library in project as a module 746 ............................................................................................... Chapter 107: Gson 747 .................................................................................................................................................... Section 107.1: Parsing JSON with Gson 747 ................................................................................................................... Section 107.2: Adding a custom Converter to Gson 749 .............................................................................................. Section 107.3: Parsing a List<String> with Gson 749 .....................................................................................................
Section 107.4: Adding Gson to your project 750 ............................................................................................................ Section 107.5: Parsing JSON to Generic Class Object with Gson 750 ......................................................................... Section 107.6: Using Gson with inheritance 751 ............................................................................................................. Section 107.7: Parsing JSON property to enum with Gson 753 ................................................................................... Section 107.8: Using Gson to load a JSON file from disk 753 ...................................................................................... Section 107.9: Using Gson as serializer with Retrofit 753 ............................................................................................. Section 107.10: Parsing json array to generic class using Gson 754 ........................................................................... Section 107.11: Custom JSON Deserializer using Gson 755 ........................................................................................... Section 107.12: JSON Serialization/Deserialization with AutoValue and Gson 756 ................................................... Chapter 108: Device Display Metrics 758 ............................................................................................................... Section 108.1: Get the screens pixel dimensions 758 ..................................................................................................... Section 108.2: Get screen density 758 ............................................................................................................................ Section 108.3: Formula px to dp, dp to px conversation 758 ....................................................................................... Chapter 109: TextView 759 ............................................................................................................................................ Section 109.1: Spannable TextView 759 .......................................................................................................................... Section 109.2: Strikethrough TextView 760 .................................................................................................................... Section 109.3: TextView with image 761 ......................................................................................................................... Section 109.4: Make RelativeSizeSpan align to top 761 ............................................................................................... Section 109.5: Pinchzoom on TextView 763 ................................................................................................................... Section 109.6: Textview with dierent Textsize 764 ...................................................................................................... Section 109.7: Theme and Style customization 764 ...................................................................................................... Section 109.8: TextView customization 766 ................................................................................................................... Section 109.9: Single TextView with two dierent colors 769 ...................................................................................... Chapter 110: ListView 771 .............................................................................................................................................. Section 110.1: Custom ArrayAdapter 771 ........................................................................................................................ Section 110.2: A basic ListView with an ArrayAdapter 772 ........................................................................................... Section 110.3: Filtering with CursorAdapter 772 ............................................................................................................. Chapter 111: Building Backwards Compatible Apps 774 ................................................................................. Section 111.1: How to handle deprecated API 774 .......................................................................................................... Chapter 112: Loader 776 ................................................................................................................................................. Section 112.1: Basic AsyncTaskLoader 776 ..................................................................................................................... Section 112.2: AsyncTaskLoader with cache 777 ........................................................................................................... Section 112.3: Reloading 778 ............................................................................................................................................ Section 112.4: Pass parameters using a Bundle 779 ..................................................................................................... Chapter 113: ProGuard - Obfuscating and Shrinking your code 780 ....................................................... Section 113.1: Rules for some of the widely used Libraries 780 .................................................................................... Section 113.2: Remove trace logging (and other) statements at build time 782 ....................................................... Section 113.3: Protecting your code from hackers 782 ................................................................................................. Section 113.4: Enable ProGuard for your build 783 ........................................................................................................ Section 113.5: Enabling ProGuard with a custom obfuscation configuration file 783 ................................................ Chapter 114: Detect Shake Event in Android 785 .............................................................................................. Section 114.1: Shake Detector in Android Example 785 ................................................................................................. Section 114.2: Using Seismic shake detection 786 ......................................................................................................... Chapter 115: Typedef Annotations: @IntDef, @StringDef 787 ................................................................... Section 115.1: IntDef Annotations 787 .............................................................................................................................. Section 115.2: Combining constants with flags 787 ....................................................................................................... Chapter 116: Capturing Screenshots 789 ............................................................................................................... Section 116.1: Taking a screenshot of a particular view 789 ......................................................................................... Section 116.2: Capturing Screenshot via Android Studio 789 .......................................................................................
Section 116.3: Capturing Screenshot via ADB and saving directly in your PC 790 ..................................................... Section 116.4: Capturing Screenshot via Android Device Monitor 790 ........................................................................ Section 116.5: Capturing Screenshot via ADB 791 ......................................................................................................... Chapter 117: MVP Architecture 792 ........................................................................................................................... Section 117.1: Login example in the Model View Presenter (MVP) pattern 792 .......................................................... Section 117.2: Simple Login Example in MVP 795 .......................................................................................................... Chapter 118: Orientation Changes 802 .................................................................................................................... Section 118.1: Saving and Restoring Activity State 802 ................................................................................................. Section 118.2: Retaining Fragments 802 ......................................................................................................................... Section 118.3: Manually Managing Configuration Changes 803 .................................................................................. Section 118.4: Handling AsyncTask 804 .......................................................................................................................... Section 118.5: Lock Screen's rotation programmatically 805 ....................................................................................... Section 118.6: Saving and Restoring Fragment State 806 ............................................................................................ Chapter 119: Xposed 808 ................................................................................................................................................ Section 119.1: Creating a Xposed Module 808 ................................................................................................................ Section 119.2: Hooking a method 808 ............................................................................................................................. Chapter 120: Security 810 .............................................................................................................................................. Section 120.1: Verifying App Signature - Tamper Detection 810 ................................................................................. Chapter 121: PackageManager 811 .......................................................................................................................... Section 121.1: Retrieve application version 811 .............................................................................................................. Section 121.2: Version name and version code 811 ...................................................................................................... Section 121.3: Install time and update time 811 ............................................................................................................. Section 121.4: Utility method using PackageManager 812 ........................................................................................... Chapter 122: ImageView 814 ........................................................................................................................................ Section 122.1: Set tint 814 ................................................................................................................................................. Section 122.2: Set alpha 815 ............................................................................................................................................ Section 122.3: Set Scale Type 815 ................................................................................................................................... Section 122.4: ImageView ScaleType - Center 820 ....................................................................................................... Section 122.5: ImageView ScaleType - CenterCrop 822 ............................................................................................... Section 122.6: ImageView ScaleType - CenterInside 824 ............................................................................................. Section 122.7: ImageView ScaleType - FitStart and FitEnd 826 .................................................................................. Section 122.8: ImageView ScaleType - FitCenter 830 ................................................................................................... Section 122.9: Set Image Resource 832 .......................................................................................................................... Section 122.10: ImageView ScaleType - FitXy 833 ........................................................................................................ Section 122.11: MLRoundedImageView.java 835 ............................................................................................................ Chapter 123: Gesture Detection 838 ......................................................................................................................... Section 123.1: Swipe Detection 838 .................................................................................................................................. Section 123.2: Basic Gesture Detection 839 ................................................................................................................... Chapter 124: Doze Mode 841 ....................................................................................................................................... Section 124.1: Whitelisting an Android application programmatically 841 ................................................................. Section 124.2: Exclude app from using doze mode 841 ............................................................................................... Chapter 125: Android Sound and Media 842 ........................................................................................................ Section 125.1: How to pick image and video for api >19 842 ........................................................................................ Section 125.2: Play sounds via SoundPool 843 .............................................................................................................. Chapter 126: SearchView 844 ...................................................................................................................................... Section 126.1: Setting Theme for SearchView 844 ......................................................................................................... Section 126.2: SearchView in Toolbar with Fragment 844 ........................................................................................... Section 126.3: Appcompat SearchView with RxBindings watcher 846 ........................................................................ Chapter 127: Camera and Gallery 849 ....................................................................................................................
Section 127.1: Take photo 849 .......................................................................................................................................... Section 127.2: Taking full-sized photo from camera 851 ............................................................................................. Section 127.3: Decode bitmap correctly rotated from the uri fetched with the intent 854 ....................................... Section 127.4: Set camera resolution 856 ....................................................................................................................... Section 127.5: How to start camera or gallery and save camera result to storage 856 .......................................... Chapter 128: Callback URL 860 ................................................................................................................................... Section 128.1: Callback URL example with Instagram OAuth 860 ............................................................................... Chapter 129: Twitter APIs 861 ...................................................................................................................................... Section 129.1: Creating login with twitter button and attach a callback to it 861 ...................................................... Chapter 130: Drawables 863 ........................................................................................................................................ Section 130.1: Custom Drawable 863 .............................................................................................................................. Section 130.2: Tint a drawable 864 ................................................................................................................................. Section 130.3: Circular View 865 ...................................................................................................................................... Section 130.4: Make View with rounded corners 865 .................................................................................................... Chapter 131: Colors 867 ................................................................................................................................................... Section 131.1: Color Manipulation 867 .............................................................................................................................. Chapter 132: ConstraintLayout 868 .......................................................................................................................... Section 132.1: Adding ConstraintLayout to your project 868 ....................................................................................... Section 132.2: Chains 869 ................................................................................................................................................. Chapter 133: RenderScript 870 ................................................................................................................................... Section 133.1: Getting Started 870 ................................................................................................................................... Section 133.2: Blur a View 876 ......................................................................................................................................... Section 133.3: Blur an image 878 .................................................................................................................................... Chapter 134: Fresco 881 ................................................................................................................................................. Section 134.1: Getting Started with Fresco 881 .............................................................................................................. Section 134.2: Using OkHttp 3 with Fresco 882 .............................................................................................................. Section 134.3: JPEG Streaming with Fresco using DraweeController 882 .................................................................. Chapter 135: Swipe to Refresh 883 ........................................................................................................................... Section 135.1: How to add Swipe-to-Refresh To your app 883 .................................................................................... Section 135.2: Swipe To Refresh with RecyclerView 883 .............................................................................................. Chapter 136: AutoCompleteTextView 885 ............................................................................................................. Section 136.1: AutoComplete with CustomAdapter, ClickListener and Filter 885 ....................................................... Section 136.2: Simple, hard-coded AutoCompleteTextView 888 ................................................................................. Chapter 137: Installing apps with ADB 889 ........................................................................................................... Section 137.1: Uninstall an app 889 ................................................................................................................................. Section 137.2: Install all apk file in directory 889 ........................................................................................................... Section 137.3: Install an app 889 ..................................................................................................................................... Chapter 138: IntentService 890 ................................................................................................................................... Section 138.1: Creating an IntentService 890 .................................................................................................................. Section 138.2: Basic IntentService Example 890 ............................................................................................................ Section 138.3: Sample Intent Service 891 ....................................................................................................................... Chapter 139: AdMob 893 ................................................................................................................................................. Section 139.1: Implementing 893 ...................................................................................................................................... Chapter 140: Implicit Intents 895 ............................................................................................................................... Section 140.1: Implicit and Explicit Intents 895 ............................................................................................................... Section 140.2: Implicit Intents 895 ................................................................................................................................... Chapter 141: Publish to Play Store 896 ................................................................................................................... Section 141.1: Minimal app submission guide 896 ..........................................................................................................
Chapter 142: Firebase Realtime DataBase 898 ................................................................................................. Section 142.1: Quick setup 898 ......................................................................................................................................... Section 142.2: Firebase Realtime DataBase event handler 898 .................................................................................. Section 142.3: Understanding firebase JSON database 899 ....................................................................................... Section 142.4: Retrieving data from firebase 900 ......................................................................................................... Section 142.5: Listening for child updates 901 ............................................................................................................... Section 142.6: Retrieving data with pagination 902 ...................................................................................................... Section 142.7: Denormalization: Flat Database Structure 903 ..................................................................................... Section 142.8: Designing and understanding how to retrieve realtime data from the Firebase Database 905 ............................................................................................................................................................................. Chapter 143: Image Compression 908 .................................................................................................................... Section 143.1: How to compress image without size change 908 ................................................................................ Chapter 144: Email Validation 911 ........................................................................................................................... Section 144.1: Email address validation 911 ................................................................................................................... Section 144.2: Email Address validation with using Patterns 911 ................................................................................ Chapter 145: Keyboard 912 ......................................................................................................................................... Section 145.1: Register a callback for keyboard open and close 912 ......................................................................... Section 145.2: Hide keyboard when user taps anywhere else on the screen 912 ..................................................... Chapter 146: Button 914 ................................................................................................................................................ Section 146.1: Using the same click event for one or more Views in the XML 914 ..................................................... Section 146.2: Defining external Listener 914 ................................................................................................................ Section 146.3: inline onClickListener 915 ......................................................................................................................... Section 146.4: Customizing Button style 915 .................................................................................................................. Section 146.5: Custom Click Listener to prevent multiple fast clicks 919 .................................................................... Section 146.6: Using the layout to define a click action 919 ........................................................................................ Section 146.7: Listening to the long click events 920 ..................................................................................................... Chapter 147: TextInputLayout 921 ........................................................................................................................... Section 147.1: Basic usage 921 ......................................................................................................................................... Section 147.2: Password Visibility Toggles 921 .............................................................................................................. Section 147.3: Adding Character Counting 921 ............................................................................................................. Section 147.4: Handling Errors 922 ................................................................................................................................. Section 147.5: Customizing the appearance of the TextInputLayout 922 .................................................................. Section 147.6: TextInputEditText 923 .............................................................................................................................. Chapter 148: Bottom Sheets 925 ............................................................................................................................... Section 148.1: Quick Setup 925 ........................................................................................................................................ Section 148.2: BottomSheetBehavior like Google maps 925 ....................................................................................... Section 148.3: Modal bottom sheets with BottomSheetDialog 932 ............................................................................. Section 148.4: Modal bottom sheets with BottomSheetDialogFragment 932 ............................................................ Section 148.5: Persistent Bottom Sheets 932 ................................................................................................................. Section 148.6: Open BottomSheet DialogFragment in Expanded mode by default 933 .......................................... Chapter 149: CoordinatorLayout and Behaviors 935 ..................................................................................... Section 149.1: Creating a simple Behavior 935 .............................................................................................................. Section 149.2: Using the SwipeDismissBehavior 936 ..................................................................................................... Section 149.3: Create dependencies between Views 936 ............................................................................................. Chapter 150: EditText 938 ............................................................................................................................................. Section 150.1: Working with EditTexts 938 ...................................................................................................................... Section 150.2: Customizing the InputType 940 .............................................................................................................. Section 150.3: Icon or button inside Custom Edit Text and its action and click listeners 940 ................................... Section 150.4: Hiding SoftKeyboard 942 ........................................................................................................................