Android Notes for Professionals Android ™ Notes for Professionals GoalKicker.com Free Programming Books Disclaimer This is an unocial free book created for educational purposes and is not aliated with ocial 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 12 ....................................................................................................................... Section 1.3: Android programming without an IDE 13 .................................................................................................. Section 1.4: Application Fundamentals 17 ...................................................................................................................... Section 1.5: Setting up an AVD (Android Virtual Device) 18 ......................................................................................... Chapter 2: Layouts 22 ..................................................................................................................................................... Section 2.1: LayoutParams 22 ......................................................................................................................................... Section 2.2: Gravity and layout gravity 25 .................................................................................................................... Section 2.3: CoordinatorLayout Scrolling Behavior 27 ................................................................................................. Section 2.4: Percent Layouts 29 ...................................................................................................................................... Section 2.5: View Weight 30 ............................................................................................................................................ Section 2.6: Creating LinearLayout programmatically 31 ........................................................................................... Section 2.7: LinearLayout 32 ........................................................................................................................................... Section 2.8: RelativeLayout 33 ........................................................................................................................................ Section 2.9: FrameLayout 35 .......................................................................................................................................... Section 2.10: GridLayout 36 ............................................................................................................................................. Section 2.11: CoordinatorLayout 38 ................................................................................................................................. Chapter 3: Gradle for Android 40 ............................................................................................................................. Section 3.1: A basic build.gradle file 40 ........................................................................................................................... Section 3.2: Define and use Build Configuration Fields 42 ........................................................................................... Section 3.3: Centralizing dependencies via "dependencies.gradle" file 44 ................................................................. Section 3.4: Sign APK without exposing keystore password 46 .................................................................................. Section 3.5: Adding product flavor-specific dependencies 47 ..................................................................................... Section 3.6: Specifying dierent application IDs for build types and product flavors 48 ......................................... Section 3.7: Versioning your builds via "version.properties" file 49 ............................................................................. Section 3.8: Defining product flavors 50 ........................................................................................................................ Section 3.9: Changing output apk name and add version name: 50 .......................................................................... Section 3.10: Adding product flavor-specific resources 51 .......................................................................................... Section 3.11: Why are there two build.gradle files in an Android Studio project? 51 ................................................. Section 3.12: Directory structure for flavor-specific resources 52 ............................................................................... Section 3.13: Enable Proguard using gradle 52 ............................................................................................................. Section 3.14: Ignoring build variant 52 ............................................................................................................................ Section 3.15: Enable experimental NDK plugin support for Gradle and AndroidStudio 53 ....................................... Section 3.16: Display signing information 55 ................................................................................................................. Section 3.17: Seeing dependency tree 56 ....................................................................................................................... Section 3.18: Disable image compression for a smaller APK file size 56 .................................................................... Section 3.19: Delete "unaligned" apk automatically 57 ................................................................................................ Section 3.20: Executing a shell script from gradle 57 ................................................................................................... Section 3.21: Show all gradle project tasks 58 ............................................................................................................... Section 3.22: Debugging your Gradle errors 59 ............................................................................................................ Section 3.23: Use gradle.properties for central versionnumber/buildconfigurations 60 ......................................... Section 3.24: Defining build types 61 .............................................................................................................................. Chapter 4: RecyclerView onClickListeners 62 .................................................................................................... Section 4.1: Kotlin and RxJava example 62 ................................................................................................................... Section 4.2: RecyclerView Click listener 63 .................................................................................................................... Section 4.3: Another way to implement Item Click Listener 64 ................................................................................... Section 4.4: New Example 66 .......................................................................................................................................... Section 4.5: Easy OnLongClick and OnClick Example 67 ............................................................................................. Section 4.6: Item Click Listeners 70 ................................................................................................................................. Chapter 5: NavigationView 72 .................................................................................................................................... Section 5.1: How to add the NavigationView 72 ............................................................................................................ Section 5.2: Add underline in menu elements 76 .......................................................................................................... Section 5.3: Add seperators to menu 77 ........................................................................................................................ Section 5.4: Add menu Divider using default DividerItemDecoration 78 ................................................................... Chapter 6: Intent 80 ......................................................................................................................................................... Section 6.1: Getting a result from another Activity 80 ................................................................................................... Section 6.2: Passing data between activities 82 ............................................................................................................ Section 6.3: Open a URL in a browser 83 ....................................................................................................................... Section 6.4: Starter Pattern 84 ........................................................................................................................................ Section 6.5: Clearing an activity stack 85 ...................................................................................................................... Section 6.6: Start an activity 85 ....................................................................................................................................... Section 6.7: Sending emails 86 ........................................................................................................................................ Section 6.8: CustomTabsIntent for Chrome Custom Tabs 86 ..................................................................................... Section 6.9: Intent URI 87 ................................................................................................................................................. Section 6.10: Start the dialer 88 ....................................................................................................................................... Section 6.11: Broadcasting Messages to Other Components 88 .................................................................................. Section 6.12: Passing custom object between activities 89 .......................................................................................... Section 6.13: Open Google map with specified latitude, longitude 91 ......................................................................... Section 6.14: Passing dierent data through Intent in Activity 91 ............................................................................... Section 6.15: Share intent 93 ............................................................................................................................................ Section 6.16: Showing a File Chooser and Reading the Result 93 ............................................................................... Section 6.17: Sharing Multiple Files through Intent 95 ................................................................................................... Section 6.18: Start Unbound Service using an Intent 95 ............................................................................................... Section 6.19: Getting a result from Activity to Fragment 96 ........................................................................................ Chapter 7: JSON in Android with org.json 98 ..................................................................................................... Section 7.1: Creating a simple JSON object 98 .............................................................................................................. Section 7.2: Create a JSON String with null value 98 ................................................................................................... Section 7.3: Add JSONArray to JSONObject 98 ............................................................................................................ Section 7.4: Parse simple JSON object 99 ..................................................................................................................... Section 7.5: Check for the existence of fields on JSON 100 ......................................................................................... Section 7.6: Create nested JSON object 100 ................................................................................................................. Section 7.7: Updating the elements in the JSON 101 ................................................................................................... Section 7.8: Using JsonReader to read JSON from a stream 101 ............................................................................. Section 7.9: Working with null-string when parsing json 103 ...................................................................................... Section 7.10: Handling dynamic key for JSON response 104 ...................................................................................... Chapter 8: Android Studio 106 ................................................................................................................................... Section 8.1: Setup Android Studio 106 ............................................................................................................................ Section 8.2: View And Add Shortcuts in Android Studio 106 ........................................................................................ Section 8.3: Android Studio useful shortcuts 107 .......................................................................................................... Section 8.4: Android Studio Improve performance tip 108 .......................................................................................... Section 8.5: Gradle build project takes forever 108 ...................................................................................................... Section 8.6: Enable/Disable blank line copy 109 .......................................................................................................... Section 8.7: Custom colors of logcat message based on message importance 110 ............................................... Section 8.8: Filter logs from UI 111 ................................................................................................................................. Section 8.9: Create filters configuration 112 ................................................................................................................. Section 8.10: Create assets folder 113 ........................................................................................................................... Chapter 9: Resources 115 ............................................................................................................................................. Section 9.1: Define colors 115 .......................................................................................................................................... Section 9.2: Color Transparency(Alpha) Level 116 ...................................................................................................... Section 9.3: Define String Plurals 116 ............................................................................................................................. Section 9.4: Define strings 117 ........................................................................................................................................ Section 9.5: Define dimensions 118 ................................................................................................................................ Section 9.6: String formatting in strings.xml 118 ........................................................................................................... Section 9.7: Define integer array 119 ............................................................................................................................. Section 9.8: Define a color state list 119 ........................................................................................................................ Section 9.9: 9 Patches 120 ............................................................................................................................................... Section 9.10: Getting resources without "deprecated" warnings 123 ......................................................................... Section 9.11: Working with strings.xml file 123 ............................................................................................................... Section 9.12: Define string array 124 .............................................................................................................................. Section 9.13: Define integers 125 .................................................................................................................................... Section 9.14: Define a menu resource and use it inside Activity/Fragment 125 ....................................................... Chapter 10: Data Binding Library 127 ..................................................................................................................... Section 10.1: Basic text field binding 127 ........................................................................................................................ Section 10.2: Built-in two-way Data Binding 128 ........................................................................................................... Section 10.3: Custom event using lambda expression 129 .......................................................................................... Section 10.4: Default value in Data Binding 131 ............................................................................................................ Section 10.5: Databinding in Dialog 131 ......................................................................................................................... Section 10.6: Binding with an accessor method 131 ..................................................................................................... Section 10.7: Pass widget as reference in BindingAdapter 132 ................................................................................... Section 10.8: Click listener with Binding 133 ................................................................................................................... Section 10.9: Data binding in RecyclerView Adapter 134 ............................................................................................. Section 10.10: Databinding in Fragment 135 ................................................................................................................. Section 10.11: DataBinding with custom variables(int,boolean) 136 ............................................................................ Section 10.12: Referencing classes 136 ........................................................................................................................... Chapter 11: Exceptions 138 ............................................................................................................................................ Section 11.1: ActivityNotFoundException 138 .................................................................................................................. Section 11.2: OutOfMemoryError 138 .............................................................................................................................. Section 11.3: Registering own Handler for unexpected exceptions 138 ...................................................................... Section 11.4: UncaughtException 140 .............................................................................................................................. Section 11.5: NetworkOnMainThreadException 140 ...................................................................................................... Section 11.6: DexException 142 ........................................................................................................................................ Chapter 12: Getting Calculated View Dimensions 143 .................................................................................... Section 12.1: Calculating initial View dimensions in an Activity 143 ............................................................................. Chapter 13: AsyncTask 144 ........................................................................................................................................... Section 13.1: Basic Usage 144 .......................................................................................................................................... Section 13.2: Pass Activity as WeakReference to avoid memory leaks 146 .............................................................. Section 13.3: Download Image using AsyncTask in Android 147 ................................................................................ Section 13.4: Canceling AsyncTask 150 .......................................................................................................................... Section 13.5: AsyncTask: Serial Execution and Parallel Execution of Task 150 .......................................................... Section 13.6: Order of execution 153 .............................................................................................................................. Section 13.7: Publishing progress 153 ............................................................................................................................. Chapter 14: SharedPreferences 155 ........................................................................................................................ Section 14.1: Implementing a Settings screen using SharedPreferences 155 ............................................................. Section 14.2: Commit vs. Apply 157 ................................................................................................................................ Section 14.3: Read and write values to SharedPreferences 157 .................................................................................. Section 14.4: Retrieve all stored entries from a particular SharedPreferences file 158 ............................................ Section 14.5: Reading and writing data to SharedPreferences with Singleton 159 ................................................... Section 14.6: getPreferences(int) VS getSharedPreferences(String, int) 163 ............................................................. Section 14.7: Listening for SharedPreferences changes 163 ....................................................................................... Section 14.8: Store, Retrieve, Remove and Clear Data from SharedPreferences 164 .............................................. Section 14.9: Add filter for EditTextPreference 164 ....................................................................................................... Section 14.10: Supported data types in SharedPreferences 165 ................................................................................. Section 14.11: Dierent ways of instantiating an object of SharedPreferences 165 .................................................. Section 14.12: Removing keys 166 ................................................................................................................................... Section 14.13: Support pre-Honeycomb with StringSet 166 ......................................................................................... Chapter 15: Emulator 168 .............................................................................................................................................. Section 15.1: Taking screenshots 168 .............................................................................................................................. Section 15.2: Simulate call 173 ......................................................................................................................................... Section 15.3: Open the AVD Manager 173 ..................................................................................................................... Section 15.4: Resolving Errors while starting emulator 173 ......................................................................................... Chapter 16: Material Design 175 ................................................................................................................................ Section 16.1: Adding a Toolbar 175 ................................................................................................................................. Section 16.2: Buttons styled with Material Design 176 .................................................................................................. Section 16.3: Adding a FloatingActionButton (FAB) 177 ............................................................................................... Section 16.4: RippleDrawable 178 ................................................................................................................................... Section 16.5: Adding a TabLayout 183 ........................................................................................................................... Section 16.6: Bottom Sheets in Design Support Library 185 ........................................................................................ Section 16.7: Apply an AppCompat theme 188 ............................................................................................................. Section 16.8: Add a Snackbar 189 ................................................................................................................................... Section 16.9: Add a Navigation Drawer 190 ................................................................................................................... Section 16.10: How to use TextInputLayout 193 ............................................................................................................. Chapter 17: Lint Warnings 194 .................................................................................................................................... Section 17.1: Using tools:ignore in xml files 194 ............................................................................................................. Section 17.2: Configure LintOptions with gradle 194 ..................................................................................................... Section 17.3: Configuring lint checking in Java and XML source files 195 .................................................................. Section 17.4: How to configure the lint.xml file 195 ....................................................................................................... Section 17.5: Mark Suppress Warnings 196 ................................................................................................................... Section 17.6: Importing resources without "Deprecated" error 196 ............................................................................ Chapter 18: Service 198 .................................................................................................................................................. Section 18.1: Lifecycle of a Service 198 ........................................................................................................................... Section 18.2: Defining the process of a service 199 ...................................................................................................... Section 18.3: Creating an unbound service 199 ............................................................................................................. Section 18.4: Starting a Service 202 ................................................................................................................................ Section 18.5: Creating Bound Service with help of Binder 202 ..................................................................................... Section 18.6: Creating Remote Service (via AIDL) 203 .................................................................................................. Chapter 19: Storing Files in Internal & External Storage 205 ...................................................................... Section 19.1: Android: Internal and External Storage - Terminology Clarification 205 .............................................. Section 19.2: Using External Storage 209 ....................................................................................................................... Section 19.3: Using Internal Storage 210 ........................................................................................................................ Section 19.4: Fetch Device Directory : 210 ..................................................................................................................... Section 19.5: Save Database on SD Card (Backup DB on SD) 212 ............................................................................. Chapter 20: WebView 214 ............................................................................................................................................. Section 20.1: Troubleshooting WebView by printing console messages or by remote debugging 214 ................. Section 20.2: Communication from Javascript to Java (Android) 215 ...................................................................... Section 20.3: Communication from Java to Javascript 216 ....................................................................................... Section 20.4: Open dialer example 216 ......................................................................................................................... Section 20.5: Open Local File / Create dynamic content in Webview 217 ................................................................ Section 20.6: JavaScript alert dialogs in WebView - How to make them work 217 ................................................. Chapter 21: Project SDK versions 219 ..................................................................................................................... Section 21.1: Defining project SDK versions 219 ............................................................................................................ Chapter 22: RecyclerView 220 .................................................................................................................................... Section 22.1: Adding a RecyclerView 220 ....................................................................................................................... Section 22.2: Smoother loading of items 221 ................................................................................................................ Section 22.3: RecyclerView with DataBinding 222 ........................................................................................................ Section 22.4: Animate data change 223 ........................................................................................................................ Section 22.5: Popup menu with recyclerView 227 ........................................................................................................ Section 22.6: Using several ViewHolders with ItemViewType 229 .............................................................................. Section 22.7: Filter items inside RecyclerView with a SearchView 230 ....................................................................... Section 22.8: Drag&Drop and Swipe with RecyclerView 231 ...................................................................................... Section 22.9: Show default view till items load or when data is not available 232 ................................................... Section 22.10: Add header/footer to a RecyclerView 234 ........................................................................................... Section 22.11: Endless Scrolling in Recycleview 237 ...................................................................................................... Section 22.12: Add divider lines to RecyclerView items 237 ......................................................................................... Chapter 23: Google Maps API v2 for Android 240 ............................................................................................. Section 23.1: Custom Google Map Styles 240 ................................................................................................................ Section 23.2: Default Google Map Activity 251 ............................................................................................................. Section 23.3: Show Current Location in a Google Map 252 ......................................................................................... Section 23.4: Change Oset 258 ..................................................................................................................................... Section 23.5: MapView: embedding a GoogleMap in an existing layout 258 ............................................................. Section 23.6: Get debug SHA1 fingerprint 260 ............................................................................................................... Section 23.7: Adding markers to a map 261 ................................................................................................................. Section 23.8: UISettings 261 ............................................................................................................................................ Section 23.9: InfoWindow Click Listener 262 ................................................................................................................. Section 23.10: Obtaining the SH1-Fingerprint of your certificate keystore file 263 ................................................... Section 23.11: Do not launch Google Maps when the map is clicked (lite mode) 264 ............................................... Chapter 24: PorterDu Mode 265 ............................................................................................................................ Section 24.1: Creating a PorterDu ColorFilter 265 ...................................................................................................... Section 24.2: Creating a PorterDu XferMode 265 ...................................................................................................... Section 24.3: Apply a radial mask (vignette) to a bitmap using PorterDuXfermode 265 ..................................... Chapter 25: 9-Patch Images 267 ............................................................................................................................... Section 25.1: Basic rounded corners 267 ....................................................................................................................... Section 25.2: Optional padding lines 267 ....................................................................................................................... Section 25.3: Basic spinner 268 ....................................................................................................................................... Chapter 26: Android NDK 269 ...................................................................................................................................... Section 26.1: How to log in ndk 269 ................................................................................................................................ Section 26.2: Building native executables for Android 269 .......................................................................................... Section 26.3: How to clean the build 270 ....................................................................................................................... Section 26.4: How to use a makefile other than Android.mk 270 ............................................................................... Chapter 27: RecyclerView Decorations 271 ......................................................................................................... Section 27.1: Add divider to RecyclerView 271 .............................................................................................................. Section 27.2: Drawing a Separator 273 ......................................................................................................................... Section 27.3: How to add dividers using and DividerItemDecoration 274 ................................................................. Section 27.4: Per-item margins with ItemDecoration 274 ............................................................................................ Section 27.5: ItemOsetDecoration for GridLayoutManager in RecycleView 275 ................................................... Chapter 28: Camera 2 API 277 .................................................................................................................................... Section 28.1: Preview the main camera in a TextureView 277 ..................................................................................... Chapter 29: ViewPager 286 .......................................................................................................................................... Section 29.1: ViewPager with a dots indicator 286 ........................................................................................................ Section 29.2: Basic ViewPager usage with fragments 288 .......................................................................................... Section 29.3: ViewPager with PreferenceFragment 289 .............................................................................................. Section 29.4: Adding a ViewPager 290 .......................................................................................................................... Section 29.5: Setup OnPageChangeListener 291 .......................................................................................................... Section 29.6: ViewPager with TabLayout 292 ............................................................................................................... Chapter 30: CardView 294 ............................................................................................................................................ Section 30.1: Getting Started with CardView 294 .......................................................................................................... Section 30.2: Adding Ripple animation 295 .........................................................................