Skip to content

Instantly share code, notes, and snippets.

View patrickhammond's full-sized avatar

Patrick Hammond patrickhammond

View GitHub Profile
/**
* Created by Bill on 7/28/14.
*/
public class LearningApplication extends Application {
private static final String TAG = QuickeyLearningApplication.class.getSimpleName();
@Override
public void onCreate() {
super.onCreate();
@patrickhammond
patrickhammond / MyActivity.java
Last active August 29, 2015 14:05
Lambdas on Android...its a brave new world. See: https://github.com/evant/gradle-retrolambda
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
// Old school
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
@patrickhammond
patrickhammond / fml.java
Created November 11, 2014 21:29
Getting list value at position when you have added a header view. There really should be a Lint check around this type of subtle bug.
// If you do this...
list.addHeaderView(headerView);
// And then call this inside of onItemClick, the position value will not be what you expect (header is now index 0)
Fruit fruit = adapter.getItem(position);
// This is what you really want to call inside of onItemClick
Fruit fruit = (Fruit) parent.getItemAtPosition(position);
@patrickhammond
patrickhammond / CheckBoxHelper.java
Last active August 29, 2015 14:10
Creating checkboxes with bigger touch targets
import android.graphics.Rect;
import android.view.View;
import android.widget.CheckBox;
/**
* Enlarges the effective hit target of a checkbox to more closely match the dimensions of the
* provided root view. If the provided root isn't actually an ancestor view of the checkbox, bad
* things will happen.
* <p/>
* See: http://developer.android.com/training/gestures/viewgroup.html#delegate
@patrickhammond
patrickhammond / checkHasPermission.java
Created February 5, 2015 00:42
Utility method to perform a runtime check that an app has been granted a permission.
public static void checkHasPermission(Context contxt, String permissionName) {
String packageName = context.getPackageName();
PackageManager packageManager = context.getPackageManager();
int permissionStatus = packageManager.checkPermission(permissionName, packageName);
if (permissionStatus != PackageManager.PERMISSION_GRANTED) {
throw new SecurityException("Application requires <uses-permission android:name=\"" + permissionName + "\" />");
}
}
@patrickhammond
patrickhammond / PRESENTATION.md
Last active August 29, 2015 14:15
Getting started with RemarkJS. See instructions.md for details

class: center, middle, title-slide

Visit for remarkjs.com for syntax

@patrickhammond
patrickhammond / Optional.java
Created February 27, 2015 16:08
Optional implementation pillaged from Java 8 for when you don't have Java 8 APIs available. Optionally (ha!), you could take a look at Guava's Optional (http://docs.guava-libraries.googlecode.com/git/javadoc/com/google/common/base/Optional.html)
/**
* In the absence of Java 8, here is our Optional duplicated code. Other than the package name,
* this should be a subset of this API, http://docs.oracle.com/javase/8/docs/api/java/util/Optional.html
*/
public final class Optional<T> {
public static <T> Optional<T> of(T value) {
if (value == null) {
return Optional.empty();
}
@patrickhammond
patrickhammond / GsonParser.java
Last active August 29, 2015 14:16
Jackson parser behavior when encountering integer, nulls, and empty strings.
import com.google.gson.Gson;
import java.io.IOException;
public class GsonParser implements Parser {
@Override
public <T> T parse(String json, Class<T> clazz) throws IOException {
return new Gson().fromJson(json, clazz);
}
}
@patrickhammond
patrickhammond / recreate.sh
Last active August 29, 2015 14:16
Migrates a project from one package to another.
#!/bin/sh
rm -rf app
mkdir -p app/src/main/java/com/mycompany/myapp
echo "import com.mycompany.myapp.R;" > app/src/main/java/com/mycompany/myapp/Test.java
echo "com.mycompany.myapp.SomeActivity" > app/AndroidManifest.xml