Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Fu alexfu

View GitHub Profile
@alexfu
alexfu / ImageLoader.java
Created Apr 5, 2013
Downloading an image from the network using HttpURLConnection
View ImageLoader.java
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 / FragmentObserver.java
Last active Apr 6, 2021
Observer pattern for notifying Fragments of a ViewPager to update their views. This will update the current Fragment, as well as the off screen Fragments that are retained.
View FragmentObserver.java
public class FragmentObserver extends Observable {
@Override
public void notifyObservers() {
setChanged(); // Set the changed flag to true, otherwise observers won't be notified.
super.notifyObservers();
}
}
@alexfu
alexfu / PrefixedEditText.java
Last active Oct 7, 2021
EditText with support for prefixes.
View PrefixedEditText.java
import android.content.Context;
import android.content.res.ColorStateList;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.widget.EditText;
/**
@alexfu
alexfu / ReversedFrameLayout.java
Last active Dec 31, 2015
An attempt at reversing the stack ordering of a simple FrameLayout.
View ReversedFrameLayout.java
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@alexfu
alexfu / MyContextWrapper.java
Last active Nov 10, 2017
A ContextWrapper that provides a custom Drawable for overscroll egde and glow. http://alexfu.github.io/blog/2013/12/11/customizing-edgeeffect/
View MyContextWrapper.java
import android.content.Context;
import android.content.ContextWrapper;
import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.util.Log;
/**
@alexfu
alexfu / IsMyActionBarInOverlay.java
Last active Oct 4, 2017
Example that shows how to determine if the ActionBar is currently in overlay mode.
View IsMyActionBarInOverlay.java
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(windowActionBarOverlay()) {
// Do something
}
}
@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 / ColorUtils.java
Last active Dec 6, 2018
Automatic text color selection using relative luminance.
View ColorUtils.java
public class ColorUtils {
private static final double LM_RED_COEFFICIENT = 0.2126;
private static final double LM_GREEN_COEFFICIENT = 0.7152;
private static final double LM_BLUE_COEFFICIENT = 0.0722;
public static int calculateRelativeLuminance(int color) {
int red = (int) (Color.red(color) * LM_RED_COEFFICIENT);
int green = (int) (Color.green(color) * LM_GREEN_COEFFICIENT);
int blue = (int) (Color.blue(color) * LM_BLUE_COEFFICIENT);
return red + green + blue;
@alexfu
alexfu / DividerItemDecoration.java
Last active Nov 26, 2020
An ItemDecoration that draws dividers between items. Pulled from Android support demos.
View DividerItemDecoration.java
/*
* Copyright (C) 2014 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software