Android Tips Round-Up, Part 2

Here's the second round-up of Android tips I've been posting.

More: Part 1 | Part 2 | Part 3 | Part 4 | Part 5

DateUtils.formatDateTime() - One-stop shop for localized date/time strings.

[AlarmManager.setInexactRepeating](http://developer.android.com/reference/android/app/AlarmManager.html#setInexactRepeating(int, long, long, android.app.PendingIntent)) - Saves on battery life by grouping multiple alarms together. Even if you're only calling a single alarm this is better (just make sure to call AlarmManager.cancel() when done).

[Formatter.formatFileSize()](http://developer.android.com/reference/android/text/format/Formatter.html#formatFileSize(android.content.Context, long)) - A localized file size formatter.

ActionBar.hide()/.show() - Animates the action bar hiding/showing. Lets you switch to full-screen gracefully.

[Linkify.addLinks()](http://developer.android.com/reference/android/text/util/Linkify.html#addLinks(android.text.Spannable, int)) - If you need to control how links are added to text.

StaticLayout - Useful for measuring text that you're about to render into a custom View.

Activity.onBackPressed() - Easy way to manage the back button. While I wouldn't normally hijack back, sometimes it's necessary to make a flow work.

GestureDetector - Listens to motion events and fires listener events for common actions (like clicks, scrolls and flings). So much easier than implementing your own motion event system.

DrawFilter - Lets you manipulate a Canvas even if you're not calling the draw commands. For example, you could create a custom View which sets a DrawFilter which anti-aliases the draws of the parent View.

ActivityManager.getMemoryClass() - Gives you an idea of how much memory the device has. Great for figuring out how large to make your caches.

SystemClock.sleep() - Convenience method which guarantees sleeping the amount of time entered. I use it for debugging and simulating network delays.

ViewStub - A View that initially does nothing, but can later inflate a layout. This is a great placeholder for lazy-loading Views. Its only drawback is that it doesn't support <merge> tags, so it can create unnecessary nesting in the hierarchy if you're not careful.

DisplayMetrics.density - You can get the density of the screen this way. Most of the time you'll be better off letting the system scale dimensions automatically, but occasionally it's useful to have more control (especially with custom Views).

[Pair.create()](http://developer.android.com/reference/android/util/Pair.html#create(A, B)) - Handy class, handy creator method.