Android Notes for Professionals

GoalKicker.com (CC BY-SA)
Page 1 sur 1329Lecteur de document UniversityLib

Android Notes for Professionals

Programming · notes

Browse all programmation documents

Android

Notes for Professionals

Android"

Notes for Professionals

1000+ pages

of professional hints and tricks

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

Contents

About

...................................................................................................................................................................................

1

Chapter 1: Getting started with Android

...........................................................................................................

2

Section 1.1: Creating a New Project

Section 1.2: Setting up Android Studio

Section 1.3: Android programming without an IDE

Section 1.4: Application Fundamentals

Section 1.5: Setting up an AVD (Android Virtual Device)

..............................................................................................................................

2

.......................................................................................................................

..................................................................................................

......................................................................................................................

.........................................................................................

19

Chapter 2: Android Studio

......................................................................................................................................

23

..............................................................................................................................

Section 2.1: Setup Android Studio

Section 2.2: View And Add Shortcuts in Android Studio

Section 2.3: Android Studio useful shortcuts

Section 2.4: Android Studio Improve performance tip

Section 2.5: Gradle build project takes forever

Section 2.6: Enable/Disable blank line copy

Section 2.7: Custom colors of logcat message based on message importance

Section 2.8: Filter logs from UI

Section 2.9: Create lters conguration

Section 2.10: Create assets folder

..........................................................................................

............................................................................................................

............................................................................................

........................................................................................................

............................................................................................................

...................................................................................................................................

..............................................................................................................................

...................................................................................................................

.................................................

27

Chapter 3: Instant Run in Android Studio

Section 3.1: Enabling or disabling Instant Run

Section 3.2: Types of code Swaps in Instant Run

Section 3.3: Unsupported code changes when using Instant Run

.......................................................................................................

..........................................................................................................

....................................................................................................

.........................................................................

33

Chapter 4: TextView

.................................................................................................................................................

34

Section 4.1: Spannable TextView

Section 4.2: Strikethrough TextView

Section 4.3: TextView with image

Section 4.4: Make RelativeSizeSpan align to top

Section 4.5: Pinchzoom on TextView

Section 4.6: Textview with di#erent Textsize

Section 4.7: Theme and Style customization

Section 4.8: TextView customization

Section 4.9: Single TextView with two di#erent colors

................................................................................................................................

..........................................................................................................................

..............................................................................................................................

.....................................................................................................

.........................................................................................................................

............................................................................................................

............................................................................................................

.........................................................................................................................

............................................................................................

44

Chapter 5: AutoCompleteTextView

...................................................................................................................

46

Section 5.1: AutoComplete with CustomAdapter, ClickListener and Filter

Section 5.2: Simple, hard-coded AutoCompleteTextView

.............................................................

46

.......................................................................................

49

Chapter 6: Autosizing TextViews

.........................................................................................................................

50

Section 6.1: Granularity

Section 6.2: Preset Sizes

...............................................................................................................................................

50

.............................................................................................................................................

50

Chapter 7: ListView

....................................................................................................................................................

52

Section 7.1: Custom ArrayAdapter

Section 7.2: A basic ListView with an ArrayAdapter

Section 7.3: Filtering with CursorAdapter

.............................................................................................................................

52

................................................................................................

53

..................................................................................................................

53

Chapter 8: Layouts

....................................................................................................................................................

55

Section 8.1: LayoutParams

Section 8.2: Gravity and layout gravity

Section 8.3: CoordinatorLayout Scrolling Behavior

....................................................................................................................

.................................................................................................

60

.........................................................................................................................................

13

14

18

23

23

24

Advertisement

25

26

26

28

29

30

32

32

32

34

35

36

36

38

39

39

41

55

58

Section 8.4: Percent Layouts

Section 8.5: View Weight

Section 8.6: Creating LinearLayout programmatically

Section 8.7: LinearLayout

Section 8.8: RelativeLayout

Section 8.9: FrameLayout

Section 8.10: GridLayout

Section 8.11: CoordinatorLayout

Chapter 9: ConstraintLayout

Section 9.1: Adding ConstraintLayout to your project

Section 9.2: Chains

......................................................................................................................................

............................................................................................................................................

...........................................................................................

64

...........................................................................................................................................

........................................................................................................................................

..........................................................................................................................................

.............................................................................................................................................

.................................................................................................................................

................................................................................................................................

......................................................................................................................................................

74

.............................................................................................

73

Chapter 10: TextInputLayout

................................................................................................................................

75

............................................................................................................................................

Section 10.1: Basic usage

Section 10.2: Password Visibility Toggles

Section 10.3: Adding Character Counting

Section 10.4: Handling Errors

Section 10.5: Customizing the appearance of the TextInputLayout

Section 10.6: TextInputEditText

..................................................................................................................

.................................................................................................................

.....................................................................................................................................

..................................................................................................................................

77

......................................................................

76

Chapter 11: CoordinatorLayout and Behaviors

...........................................................................................

79

Section 11.1: Creating a simple Behavior

Section 11.2: Using the SwipeDismissBehavior

Section 11.3: Create dependencies between Views

...................................................................................................................

..........................................................................................................

..................................................................................................

80

Chapter 12: TabLayout

.............................................................................................................................................

82

Section 12.1: Using a TabLayout without a ViewPager

............................................................................................

82

Chapter 13: ViewPager

.............................................................................................................................................

83

Section 13.1: ViewPager with a dots indicator

Section 13.2: Basic ViewPager usage with fragments

Section 13.3: ViewPager with PreferenceFragment

Section 13.4: Adding a ViewPager

Section 13.5: Setup OnPageChangeListener

Section 13.6: ViewPager with TabLayout

...........................................................................................................

83

.............................................................................................

85

.................................................................................................

..............................................................................................................................

.............................................................................................................

..................................................................................................................

Chapter 14: CardView

...............................................................................................................................................

92

Section 14.1: Getting Started with CardView

Section 14.2: Adding Ripple animation

Section 14.3: Customizing the CardView

Section 14.4: Using Images as Background in CardView (Pre-Lollipop device issues)

Section 14.5: Animate CardView background color with TransitionDrawable

......................................................................................................................

...................................................................................................................

........................................

94

......................................................

96

.............................................................................................................

Chapter 15: NavigationView

..................................................................................................................................

97

Section 15.1: How to add the NavigationView

Section 15.2: Add underline in menu elements

Section 15.3: Add seperators to menu

Section 15.4: Add menu Divider using default DividerItemDecoration

...........................................................................................................

97

.......................................................................................................

.....................................................................................................................

................................................................

103

Chapter 16: RecyclerView

.....................................................................................................................................

105

Section 16.1: Adding a RecyclerView

Section 16.2: Smoother loading of items

Section 16.3: RecyclerView with DataBinding

Section 16.4: Animate data change

Section 16.5: Popup menu with recyclerView

Section 16.6: Using several ViewHolders with ItemViewType

........................................................................................................................

.................................................................................................................

Advertisement

.........................................................................................................

.........................................................................................................................

.........................................................................................................

...............................................................................

114

62

63

65

66

68

69

71

73

75

75

75

76

79

80

86

87

88

89

92

93

93

101

102

105

106

107

108

112

Section 16.7: Filter items inside RecyclerView with a SearchView

Section 16.8: Drag&Drop and Swipe with RecyclerView

Section 16.9: Show default view till items load or when data is not available

Section 16.10: Add header/footer to a RecyclerView

Section 16.11: Endless Scrolling in Recycleview

Section 16.12: Add divider lines to RecyclerView items

............................................................................................

.......................................................................................................

..........................................................................................

........................................................................

.......................................................................................

....................................................

117

Chapter 17: RecyclerView Decorations

Section 17.1: Add divider to RecyclerView

Section 17.2: Drawing a Separator

Section 17.3: How to add dividers using and DividerItemDecoration

Section 17.4: Per-item margins with ItemDecoration

Section 17.5: ItemO#setDecoration for GridLayoutManager in RecycleView

.........................................................................................................

...............................................................................................................

..........................................................................................................................

..................................................................

.............................................................................................

....................................................

129

Chapter 18: RecyclerView onClickListeners

.................................................................................................

131

Section 18.1: Kotlin and RxJava example

Section 18.2: RecyclerView Click listener

Section 18.3: Another way to implement Item Click Listener

Section 18.4: New Example

Section 18.5: Easy OnLongClick and OnClick Example

Section 18.6: Item Click Listeners

.......................................................................................................................................

.............................................................................................................................

..........................................................................................

................................................................................................................

.................................................................................................................

................................................................................

133

Chapter 19: RecyclerView and LayoutManagers

.....................................................................................

141

Section 19.1: Adding header view to recyclerview with gridlayout manager

Section 19.2: GridLayoutManager with dynamic span count

Section 19.3: Simple list with LinearLayoutManager

Section 19.4: StaggeredGridLayoutManager

Chapter 20: Pagination in RecyclerView

...............................................................................

..............................................................................................

.........................................................................................................

......................................................................................................

......................................................

141

Section 20.1: MainActivity.java

.................................................................................................................................

151

..........................................................................................................................................

...................................................................................................................................................

..............................................................................................................................................

.....................................................................................................................................

Chapter 21: ImageView

Section 21.1: Set tint

Section 21.2: Set alpha

Section 21.3: Set Scale Type

Section 21.4: ImageView ScaleType - Center

Section 21.5: ImageView ScaleType - CenterCrop

Section 21.6: ImageView ScaleType - CenterInside

Section 21.7: ImageView ScaleType - FitStart and FitEnd

Section 21.8: ImageView ScaleType - FitCenter

Section 21.9: Set Image Resource

Section 21.10: ImageView ScaleType - FitXy

Section 21.11: MLRoundedImageView.java

.........................................................................................................

.................................................................................................

...............................................................................................

.....................................................................................

168

.....................................................................................................

............................................................................................................................

...........................................................................................................

..............................................................................................................

Chapter 22: VideoView

...........................................................................................................................................

180

Section 22.1: Play video from URL with using VideoView

Section 22.2: VideoView Create

Chapter 23: Optimized VideoView

...............................................................................................................................

....................................................................................................................

.....................................................................................

180

Section 23.1: Optimized VideoView in ListView

.......................................................................................................

181

Chapter 24: WebView

.............................................................................................................................................

193

Advertisement

Section 24.1: Troubleshooting WebView by printing console messages or by remote debugging

Section 24.2: Communication from Javascript to Java (Android)

Section 24.3: Communication from Java to Javascript

Section 24.4: Open dialer example

Section 24.5: Open Local File / Create dynamic content in Webview

..........................................................................................................................

........................................................................................

......................................................................

................................................................

194

195

195

196

.................

193

115

116

119

122

122

125

125

127

128

128

131

132

135

136

139

142

144

148

151

156

156

157

157

162

164

166

172

174

175

177

180

181

Section 24.6: JavaScript alert dialogs in WebView - How to make them work

.................................................

196

Chapter 25: SearchView

........................................................................................................................................

198

Section 25.1: Setting Theme for SearchView

Section 25.2: SearchView in Toolbar with Fragment

Section 25.3: Appcompat SearchView with RxBindings watcher

..........................................................................................................

.............................................................................................

.........................................................................

200

Chapter 26: BottomNavigationView

...............................................................................................................

203

Section 26.1: Basic implemetation

Section 26.2: Customization of BottomNavigationView

Section 26.3: Handling Enabled / Disabled states

Section 26.4: Allowing more than 3 menus

.......................................................................................

204

.................................................................................................

............................................................................................................

...........................................................................................................................

203

Chapter 27: Canvas drawing using SurfaceView

.....................................................................................

207

Section 27.1: SurfaceView with drawing thread

.....................................................................................................

207

Chapter 28: Creating Custom Views

Section 28.1: Creating Custom Views

Section 28.2: Adding attributes to views

Section 28.3: CustomView performance tips

Section 28.4: Creating a compound view

Section 28.5: Compound view for SVG/VectorDrawable as drawableRight

Section 28.6: Responding to Touch Events

...............................................................................................................

......................................................................................................................

.................................................................................................................

.........................................................................................................

...............................................................................................................

............................................................................................................

223

......................................................

220

Chapter 29: Getting Calculated View Dimensions

....................................................................................

224

Section 29.1: Calculating initial View dimensions in an Activity

Chapter 30: Adding a FuseView to an Android Project

............................................................................

224

.........................................................................

225

Section 30.1: hikr app, just another android.view.View

..........................................................................................

225

Chapter 31: Supporting Screens With Di#erent Resolutions, Sizes

................................................

232

Section 31.1: Using conguration qualiers

Section 31.2: Converting dp and sp to pixels

Section 31.3: Text size and di#erent android screen sizes

.............................................................................................................

..........................................................................................................

....................................................................................

233

Chapter 32: ViewFlipper

........................................................................................................................................

234

Section 32.1: ViewFlipper with image sliding

..........................................................................................................

234

Chapter 33: Design Patterns

Section 33.1: Observer pattern

Section 33.2: Singleton Class Example

...............................................................................................................................

.................................................................................................................................

....................................................................................................................

235

Chapter 34: Activity

................................................................................................................................................

237

Advertisement

............................................................................................................................

Section 34.1: Activity launchMode

Section 34.2: Exclude an activity from back-stack history

Section 34.3: Android Activity LifeCycle Explained

Section 34.4: End Application with exclude from Recents

Section 34.5: Presenting UI with setContentView

Section 34.6: Up Navigation for Activities

Section 34.7: Clear your current Activity stack and launch a new Activity

...................................................................................

................................................................................................

....................................................................................

...................................................................................................

...............................................................................................................

.........................................................

244

Chapter 35: Activity Recognition

......................................................................................................................

246

Section 35.1: Google Play ActivityRecognitionAPI

Section 35.2: PathSense Activity Recognition

..................................................................................................

246

........................................................................................................

248

Chapter 36: Split Screen / Multi-Screen Activities

...................................................................................

250

Section 36.1: Split Screen introduced in Android Nougat implemented

..............................................................

250

Chapter 37: Material Design

Section 37.1: Adding a Toolbar

Section 37.2: Buttons styled with Material Design

...............................................................................................................................

.................................................................................................................................

.................................................................................................

252

198

198

204

205

212

212

214

216

217

232

232

235

235

237

238

238

241

242

243

251

251

Section 37.3: Adding a FloatingActionButton (FAB)

Section 37.4: RippleDrawable

Section 37.5: Adding a TabLayout

Section 37.6: Bottom Sheets in Design Support Library

Section 37.7: Apply an AppCompat theme

Section 37.8: Add a Snackbar

Section 37.9: Add a Navigation Drawer

Section 37.10: How to use TextInputLayout

..............................................................................................

..................................................................................................................................

...........................................................................................................................

........................................................................................

261

.............................................................................................................

..................................................................................................................................

..................................................................................................................

............................................................................................................

Chapter 38: Resources

...........................................................................................................................................

270

........................................................................................................................................

....................................................................................................

...........................................................................................................................

......................................................................................................................................

..............................................................................................................................

.........................................................................................................

...........................................................................................................................

......................................................................................................................

Section 38.1: Dene colors

Section 38.2: Color Transparency(Alpha) Level

Section 38.3: Dene String Plurals

Section 38.4: Dene strings

Section 38.5: Dene dimensions

Section 38.6: String formatting in strings.xml

Section 38.7: Dene integer array

Section 38.8: Dene a color state list

Section 38.9: 9 Patches

Section 38.10: Getting resources without "deprecated" warnings

Section 38.11: Working with strings.xml le

Section 38.12: Dene string array

Section 38.13: Dene integers

Section 38.14: Dene a menu resource and use it inside Activity/Fragment

.............................................................................................................

............................................................................................................................

..................................................................................................................................

.............................................................................................................................................

.......................................................................

.....................................................

280

Chapter 39: Data Binding Library

Section 39.1: Basic text eld binding

Section 39.2: Built-in two-way Data Binding

Section 39.3: Custom event using lambda expression

Section 39.4: Default value in Data Binding

Section 39.5: Databinding in Dialog

Section 39.6: Binding with an accessor method

Section 39.7: Pass widget as reference in BindingAdapter

Section 39.8: Click listener with Binding

Section 39.9: Data binding in RecyclerView Adapter

Section 39.10: Databinding in Fragment

Section 39.11: DataBinding with custom variables(int,boolean)

Section 39.12: Referencing classes

Chapter 40: SharedPreferences

....................................................................................................................

........................................................................................................................

..........................................................................................................

..........................................................................................

...........................................................................................................

........................................................................................................................

....................................................................................................

..................................................................................

..................................................................................................................

............................................................................................

.................................................................................................................

................................................................................