This snippet of a vector drawable will turn a bitmap drawable into a tile or repeatable pattern.
Read more…All snippets tagged with android
Page 5 of 7Repeating a bitmap pattern
Connectivity live data
Monitor network connectivity, and show a SnackBar when the connection is lost.
Read more…Check if current connection is WiFi
Note: requires android.Manifest.permission.ACCESS_NETWORK_STATE
in the manifest.
Turn timestamps into "time ago" strings
This recipe creates a “time ago” string from any timestamp, often used to display how long ago a comment was posted or a notification was received.
We’re creating an extension method, so any Long
timestamp can be converted like so: 12313131.timeAgo(arr)
where arr
is a list of labels.
This list can be retrieved from a string-array
in the strings.xml
to make it localizable.
Take a picture using the default camera
Add a file file_paths.xml
to the resources directory res/xml/
in order to grant access to the correct folders:
Soft keyboard event listener
Detect when the keyboard is visible and when not.
Read more…Room type converters
Common type converters for the Room library.
Read more…Open settings activity
Start the App Settings Activity so that the user can change settings related to the application, such as permissions.
Read more…Monitor permission status
Use LiveData to determine the status of any permission (in the sample code below: the location permission). If the user grants the permission at any point, then we will get notified.
Read more…Lock an app using the phone's default lock screen
The snippets below can lock access to the app whenever the user leaves the app and comes back, with an optional timeout.
Read more…Getting the user's location
All the methods below need the location permission:
Read more…Pagination
A very complete recipe that implements a RecyclerView that pulls in paginated data, using Retrofit, LiveData. It includes example models, adapter, viewholders, and a viewholder for displaying the network state in case of an error.
Read more…HtmlCompat for Android Nougat and up
Tiny snippet for reducing compatibility clutter.
Read more…Encrypted shared prefs
A class that is using the “normal” shared preferences for any device with an sdk < M and the EncryptedSharedPreferences
for M and above.
Double trigger livedata transformation
A replacement for the mediator LiveData for triggering LiveData transformations.
Read more…