Skip to content

Instantly share code, notes, and snippets.

View alexfu's full-sized avatar

Alex Fu alexfu

View GitHub Profile
@alexfu
alexfu / ImageLoader.java
Created April 5, 2013 14:17
Downloading an image from the network using HttpURLConnection
private void fetchImageFromNetwork() {
InputStream input = null;
OutputStream output = null;
HttpURLConnection urlConnection = null;
byte[] buffer = new byte[100*1024];
try {
URL url = new URL(imageUrl);
urlConnection = (HttpURLConnection) url.openConnection();
input = urlConnection.getInputStream();
output = new FileOutputStream(path);
@alexfu
alexfu / versioning.gradle
Last active November 9, 2015 19:54
An auto-versioning Gradle plugin for Android. http://alexfu.github.io/2015/11/09/Android-Auto-Versioning/
import groovy.json.JsonBuilder
import groovy.json.JsonSlurper
ext.VersioningPlugin = VersioningPlugin
class VersioningPlugin implements Plugin<Project> {
@Override
void apply(Project project) {
project.extensions.create("versioning", VersioningPluginExtension)
@alexfu
alexfu / ViewUtils.java
Last active September 26, 2015 13:53
View utility functions that I find myself using often.
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Alex Fu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@alexfu
alexfu / CursorExtenstions.kt
Created June 30, 2015 11:56
Android database cursor extensions that simplify retrieval of typed values
import android.database.Cursor
/**
* CursorExtentions
*
* Android database cursor extensions that simplify retrieval of typed values
* @author alexfu
*/
fun Cursor.getInt(colName: String): Int {
return this.getInt(this.getColumnIndex(colName))
@alexfu
alexfu / build-gradle-git-hockeyapp.gradle
Last active August 29, 2015 14:25
Examples of how to streamline releases with your Gradle based Android project
/**
* This example demonstrates how to create a build task
* for publishing a beta to HockeyApp in addition to
* tagging and pushing a beta release to your remote git
* repo using the gradle-git plugin.
*/
buildscript {
repositories {
jcenter()
@alexfu
alexfu / StringUtils.java
Created May 23, 2015 14:27
A utility function to convert an InputStream to String.
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class StringUtils {
private StringUtils() { /* No op */ }
public static String from(InputStream is) {
byte[] buffer = new byte[1024];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
@alexfu
alexfu / AndroidSchedulerTransformer.java
Last active August 29, 2015 14:19
Transforms the given Observable into an Observable that runs asynchronously and emits items on the main thread.
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Alex Fu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@alexfu
alexfu / MyFragment.java
Created March 3, 2015 12:12
Change View styles on the fly with ContextThemeWrapper
public class MyFragment {
private Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
// Where the magic happens
context = new ContextThemeWrapper(getActivity(), R.style.Theme_Main_Inverse);
}
@alexfu
alexfu / TextViewCounter.java
Last active August 29, 2015 14:12
A counting View that counts from zero to N while animating each increment.
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Alex Fu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
@alexfu
alexfu / FitFrameLayout.java
Last active August 29, 2015 14:00
A FrameLayout that fits it's content into the viewable area (like fitSystemWindow). Works great for when fitSystemWindow isn't enough.
import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.FrameLayout;
/**
* A FrameLayout that fits it's content into the viewable