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 […]

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 […]

Android – get system defined ActionBar size (height)

In xml, you can use ?attr/actionBarSize, but if you need access to that value in Java… public int getActionBarSize() { TypedArray styledAttributes = getTheme().obtainStyledAttributes(new int[] { android.R.attr.actionBarSize }); int actionBarSize = (int) styledAttributes.getDimension(0, 0); styledAttributes.recycle(); return actionBarSize; }

Android – View traversal / walker / finder utilities

package com.qozix.view; import android.view.View; import android.view.ViewGroup; import android.view.ViewParent; import java.util.ArrayList; import java.util.Collection; /** * Collection of utility functions for finding Views in a hierarchy. */ public class Finder { /** * Interface with method to determine if a particular View meets arbitrary criteria for inclusion in a * ViewFinder method’s result set. */ public interface […]