Android – get removable SD card location

This is a little tricky, and isn’t precisely a version limitation. Here’s why, and how I’ve found the most reliable way to get it. 1. Environment.isExternalStorageRemovable (without parameters) is API 9+, so has broad support. If your devices primary external storage location is removable, then this will return the location you want – done. 2. […]

Android – RecyclerView with smooth infinite / endless scroll

/EDIT: I’ve created a library (and demo): https://github.com/moagrius/EndlessRecyclerView Most “endless” RecyclerViews wait until the last item is visible, then load more data, which is often fine and probably very more efficient strategy. However, sometimes we need to be a little more responsive, and try not to let the scroll come to a complete stop. Here’s […]

Android – write to or copy file

There are several approaches to this in Java generally and Android specifically. At this point, I almost exclusively use InputStreams and OutputStreams, with chunks appropriate to the size of the data I’m dealing with (e.g., I’ll use 1mb chunks when dealing with large video downloads, or 1kb chunks for small images, or even no chunks […]

Android – play encrypted video in ExoPlayer

Ran into this requirement recently, and despite a couple open questions on Stackoverflow and ExoPlayer’s issues page, there wasn’t much guidance. Turns out this is relatively straightforward if you’re not streaming the video (e.g., you’re playing it from a local file). The DataSource interface has 3 methods: open, close, and read. When the renderer is […]

Simple Event Bus in Java

A lightweight, simple pub-sub implementation. In Android, this class could provide much of the same functionality as LocalBroadcastManager, without the need for a Context instance. Note that while getting the instance is thread-safe, its methods are not.

Android – Detect if on UI Thread (Main Thread)

Android – Find Views / Children By Class

Common Android Tasks

Start an Activity

Start an Activity and pass data

Reading data from a bundle when starting a new Activity

Save Instance State

Restore Instance State

Asynchronous Network Request with OKHttp

Load an Image into an ImageView with Picasso

Load in Image as a Bitmap with Picasso

[…]

Java (Android) – copy properties using public getters and setters

There are tons of existing classes that will do wonders with reflection, but some (many) aren’t available in Android… E.g., since java.beans.introspection requires AWT, it’s not supported. One common operation is copying properties from one object to another. Here’s an example using traditional Reflection. Copier.copy(objectToCopyFrom, objectToCopyTo); It’ll look at objectToCopyFrom, find all public getters (using […]

Android – ViewGroup.measureChildren versus View.measure

In creating a custom ViewGroup that expected all children to lay out to the entire available space, I encountered unexpected behavior when passing in RelativeLayout instances as children. Specifically, ALIGN_PARENT_BOTTOM worked, as did it if I also passed ALIGN_PARENT_RIGHT, but the latter without the former did not work (the child was not laid out whatever), […]