Android – Serve Local Video to Chromecast
There are multiple use-cases for an embedded HTTP Server in an Android app, not the least of which is a hi-fidelity testing fake. Here's an example of how we adopted ServerSocket for use in sending downloaded, encrypted video streams to Chromecast by simply passing an HTTP Url stream. First, we needed some simple HTTP classes […]
Android – CloneView

To visually clone another view. This does not make a functional copy of the view – no hit areas or other behavior or state is cloned. This is a simple View instance that draws the pixels of the source `View` and positions them appropriately. package com.moagrius.widgets; import android.content.Context; import; import android.util.AttributeSet; import android.view.View; /** […]

Android – Use callbacks to handle Exceptions

Just a simple alternative to specific try/catch blocks. public class Safely { public static void handle(Attempter attempter, Handler handler) { try { attempter.attempt(); } catch (Exception e) { if (handler != null) { handler.onException(e); } } } public static void handle(Attempter attempter) { handle(attempter, null); } public interface Attempter { void attempt(); } public interface […]

Android – Use lambda or function reference for BroadcastReceiver

public class ReceiverFactory { public static BroadcastReceiver create(BroadcastHandler handler) { return new Receiver(handler); } public interface BroadcastHandler { void onReceive(Context context, Intent intent); } private static class Receiver extends BroadcastReceiver { private BroadcastHandler mHandler; private Receiver(@NonNull BroadcastHandler handler) { mHandler = handler; } @Override public void onReceive(Context context, Intent intent) { mHandler.onReceive(context, intent); } } […]

Android – Simulate application resource reclamation

In android, when resources are low, the device may kill an application (usually in the background) to re-allocate those resources to the app the user is interacting with at the moment. This is a different process than force-stopping or killing the application, both of which are easy to simulate with ADB commands: adb shell am […]

Android – Replacement for Gradle “The TaskInternal.execute() method has been deprecated”

So lot’s of people use “dynamic” tasks in gradle, like so: task minifyCssAssets {   doLast {     def files = fileTree(“path/to/css”)     files.include “**.css”     files.eachWithIndex { File entry, int i ->       def dynamicTask = “minifyCss${i}”       task “${dynamicTask}”(type: com.eriwen.gradle.css.tasks.MinifyCssTask) {       source = […]

Android – match_parent won’t always match parent

Here’s a simple example: <FrameLayoutandroid:layout_width=”match_parent”android:layout_height=”wrap_content”android:background=”#FF0000″><FrameLayoutandroid:layout_width=”10000dp”android:layout_height=”10000dp” /><FrameLayoutandroid:layout_width=”match_parent”android:layout_height=”match_parent”android:background=”#FF9900″><TextViewandroid:layout_width=”wrap_content”android:layout_height=”wrap_content”android:text=”XXX” /></FrameLayout></FrameLayout> (You may want to throw that whole block in a ScrollView for more obvious results) So what you have above is pretty straightforward. You have a container with wrap_content for height. It has 2 children, one is at a fixed height of 10000dp, and the other is set […]

Android – Throttle and Debounce

Throttle: one submission every {interval}, won’t start a new task until elapsed time has passed. Debounce: one submission every {interval}, will cancel all pending tasks to schedule a new one. Both are instantiated with and the only public API is attempt(Runnable), where Runnable is the task to be queued. In practical terms: int value = […]

JavaScript – get absolute position from top or left of document (x, y)

This should probably not be as complicated as it is, but obviously: JavaScript. Anyway, there are 2 basic approaches. You may see offsetTop and offsetLeft used in examples in blogs and on stack. This is great in a garden path setup, but only gives the distance between the element and it’s nearest “offsetParent” (which is […]

Android – layout available space

This is one of the most common questions around Android layout, and one that’s not at all intuitive or well documented. Let’s take the most simple use case: you have a thumbnail that’s 75dp on the right, and you want a TextView to take up the rest of the space. Of course, you could satisfy […]