Android – Application object as Server (instances instead of singletons)

Singletons have drawbacks. This is an old argument and I won’t get into it, but if you find static classes or Singletons are serving large portions of your app, here’s an alternative approach I’ve found success with. This is in directly violation of DI and I understand there are valid arguments against it, but at […]

Applying a Paint (including ColorFilters and ColorMatrix) to a View

Stackoverflow has a bunch of examples of how to apply a ColorFilter (and therefore a ColorMatrix) to a bitmap, or an ImageView (which has a built-in API), but applying color effects to an entire View are not nearly as well documented. The basic idea is to use your ColorMatrix instances in ColorFilters, then wrap that […]

Java – simple HashCode generator

/** * Created by Mike Dunn on 5/31/15. */ public class HashCode { public static int calculate(int initialNonZeroOddNumber, int multiplierNonZeroOddNumber, Object… members) { for (Object member : members) { initialNonZeroOddNumber = multiplierNonZeroOddNumber * initialNonZeroOddNumber + getHashCode(member); } return initialNonZeroOddNumber; } private static int getHashCode(Object object) { if (object == null) { return 0; } return […]

JavaScript – find next node in dom

function next(node, skip) { if (node == null) { return null; } if (!skip && node.firstChild) { return node.firstChild; } if (node.nextSibling) { return node.nextSibling; } return next(node.parentNode, true); }

Android – pass touch events to a child view

AOSP offers a delegate mechanism to modify the touchable area of Views, but this may not always behave as expected. A common use-case is having a View shown within a constrained area, but wanted to react to scrolling gestures from anywhere on the screen – e.g., imagine a RecyclerView on a landscape tablet that design […]

Android – (visually) clone a View

While there’s nothing in the Android framework can create a duplicate of an existing View, it’s trivial do duplicate one visually. Just call `someView.draw(canvas)` in the `onDraw` of the View you want to serve as the duplicate. E.g., package org.upshots.views; import android.content.Context; import; import; import android.util.AttributeSet; import android.view.View; /** * Created by michaeldunn […]

Copy files from Android device or emulator to computer

from here Copy it from internal to SD card, then ADB pull from SD card to host machine. $ adb shell # if you have multiple devices, adb -s shell $ run-as {app-package-name} $ cd /data/data/{app-package-name} $ chmod 777 {file} $ cp {file} /mnt/sdcard/ $ adb pull /mnt/sdcard/{file}

Android – confirm preference change

If you’re using the Preferences API in Android, and want to confirm (show a dialog, perform a test, etc) before a Preference is updated, you need to find that Preference item and call setOnPreferenceChangeListener to a custom listener. This can work in conjunction with PreferenceManager.registerOnSharedPreferenceChangeListener, you just need to return `false` to have the Preference […]

Android – isTaskRoot returning false incorrectly

Some Launchers will launch an app at it’s LAUNCH activity on top of the last point the user was at. Imagine: 1. User opens app. 2. The Launcher Activity is designated at Activity A, so that’s what opens. 3. User navigates several Activities deep and ends up at Activity D (A > B > C […]

Android – Restart App

The title isn’t exactly accurate – you can’t “restart” an app per se – but you can get pretty close: // presumably in an Activity; otherwise a Context instance will be needed for several methods Intent intent = getPackageManager().getLaunchIntentForPackage(getPackageName()); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); startActivity(intent); This starts the Activity specified in your manifest as the “LAUNCHER”, in […]