Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Fu alexfu

View GitHub Profile
@alexfu
alexfu / DontScaleMe.java
Created Feb 4, 2014
Prevent your BitmapDrawables from scaling when using setBackground/setBackgroundDrawable. This is usually desired when you want to set an image as your background.
View DontScaleMe.java
public class DontScaleMe extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.alayout);
ViewGroup parent = findViewById(R.id.root);
Bitmap bitmap = obtainBitmapFromSomewhere();
// This will scale your Bitmap to fit it's bounds.
parent.setBackground(new BitmapDrawable(getResources(), bitmap));
@alexfu
alexfu / FitFrameLayout.java
Last active Aug 29, 2015
A FrameLayout that fits it's content into the viewable area (like fitSystemWindow). Works great for when fitSystemWindow isn't enough.
View FitFrameLayout.java
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
@alexfu
alexfu / TextViewCounter.java
Last active Aug 29, 2015
A counting View that counts from zero to N while animating each increment.
View TextViewCounter.java
/*
* 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 Mar 3, 2015
Change View styles on the fly with ContextThemeWrapper
View MyFragment.java
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 / AndroidSchedulerTransformer.java
Last active Aug 29, 2015
Transforms the given Observable into an Observable that runs asynchronously and emits items on the main thread.
View AndroidSchedulerTransformer.java
/*
* 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 / StringUtils.java
Created May 23, 2015
A utility function to convert an InputStream to String.
View StringUtils.java
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 / build-gradle-git-hockeyapp.gradle
Last active Aug 29, 2015
Examples of how to streamline releases with your Gradle based Android project
View build-gradle-git-hockeyapp.gradle
/**
* 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 / CursorExtenstions.kt
Created Jun 30, 2015
Android database cursor extensions that simplify retrieval of typed values
View CursorExtenstions.kt
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 / ViewUtils.java
Last active Sep 26, 2015
View utility functions that I find myself using often.
View ViewUtils.java
/*
* 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
View versioning.gradle
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)