Skip to content

Instantly share code, notes, and snippets.

apply plugin: 'java'
sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7
def logger = new com.android.build.gradle.internal.LoggerWrapper(project.logger)
def sdkHandler = new com.android.build.gradle.internal.SdkHandler(project, logger)
for (File file : sdkHandler.sdkLoader.repositories) {
project.repositories.maven {
url = file.toURI()
@curioustechizen
curioustechizen / EnhancedCheckBox.java
Last active September 14, 2020 05:50
Android programmatically checkable widgets, differentiate between user clicks & programmatic setChecked calls. Based on this answer on StackOverflow: http://stackoverflow.com/a/14307643/570930. There are times when setting a switch to ON results in some action (Save to DB/ perform an HTTP POST etc). However, you want this action to happen only o…
/**
* An enhanced {@code CheckBox} that differentiates between user clicks and
* programmatic clicks. In particular, the {@code OnCheckedChangeListener} is
* <strong>not</strong> triggered when the state of the checkbox is changed
* programmatically.
*
*/
public class EnhancedCheckBox extends CheckBox implements ProgrammaticallyCheckable{
private CompoundButton.OnCheckedChangeListener mListener = null;
public EnhancedCheckBox(Context context) {
@curioustechizen
curioustechizen / StateSavingArrayAdapter.java
Last active June 17, 2018 09:56
StateSavingArrayAdapter: An ArrayAdapter that knows how to save/restore its own state.
/**
* <p>
* An {@code ArrayAdapter} that also knows how to save and restore its state.
* Basically all it does is save/restore the array of objects being managed by
* the Adapter.
* </p>
*
* <p>
* Note that this only saves the items and not things like checked item
* positions. Those belong to the {@link ListView} itself. Consider using
@curioustechizen
curioustechizen / ReceiverRegistrar.java
Last active June 11, 2020 18:15
Tiny utility class that helps avoid book-keeping code when dealing with registering and unregistering BroadcastReceivers. Hopefully, this will help you get rid of "Activity has leaked IntentReceiver that was originally registered here" messages without resorting to using boolean flags to keep track of your BroadcastReceivers.
package com.github.curioustechizen.safereg;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.util.ArrayMap;
/**
* Helper class to keep track of what {@code BroadcastReceiver}s have been
@curioustechizen
curioustechizen / NsdReflectionUtils.java
Created April 26, 2013 08:37
Using reflection to try and get the DnsSdTxtRecord object from NdsServiceInfo while using [android.net.nsd](http://developer.android.com/reference/android/net/nsd/NsdManager.html) for performing mDNS discovery. This sample uses [fest-reflect](https://github.com/alexruiz/fest-reflect) for reflection.
import static org.fest.reflect.core.Reflection.*;
import android.net.nsd.NsdServiceInfo;
public class ReflectionUtils {
public static int extractTxtRecordFromServiceInfo(NsdServiceInfo serviceInfo) {
Class<?> dnsSdTxtRecordType = type("android.net.nsd.DnsSdTxtRecord")
.load();
Object txtRecord =
method("getTxtRecord")
.withReturnType(dnsSdTxtRecordType)
@curioustechizen
curioustechizen / AsyncTaskUtils.java
Created March 11, 2013 06:57
Snippet demonstrating how to ensure AsyncTasks are always executed in parallel (the default pre-HC behavior)
import android.annotation.TargetApi;
import android.os.AsyncTask;
import android.os.Build;
public class AsyncTaskUtils {
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static <P> void executeAsyncTaskInParallel(
AsyncTask<P, ?, ?> asyncTask, P... params) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
@curioustechizen
curioustechizen / LocalBroadcastReceiver.java
Created May 24, 2012 10:54
Adding ordered broadcast capability to LocalBroadcastManager from Android support library.
package in.curtech.android.common;
import android.content.BroadcastReceiver;
/**
* Extension of {@code BroadcastReceiver} meant to be used in conjunction with
* {@link OrderEnabledLocalBroadcastManager}
*
* @author Kiran Rao
*/