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 / FragmentObserver.java
Last active April 6, 2021 03:26
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.
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 July 11, 2022 10:41
EditText with support for prefixes.
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 December 31, 2015 02:09
An attempt at reversing the stack ordering of a simple FrameLayout.
/*
* 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 November 10, 2017 10:49
A ContextWrapper that provides a custom Drawable for overscroll egde and glow. http://alexfu.github.io/blog/2013/12/11/customizing-edgeeffect/
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 October 4, 2017 12:48
Example that shows how to determine if the ActionBar is currently in overlay mode.
public class MyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(windowActionBarOverlay()) {
// Do something
}
}
@alexfu
alexfu / DontScaleMe.java
Created February 4, 2014 03:41
Prevent your BitmapDrawables from scaling when using setBackground/setBackgroundDrawable. This is usually desired when you want to set an image as your background.
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 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
@alexfu
alexfu / ColorUtils.java
Last active January 30, 2023 00:04
Automatic text color selection using relative luminance.
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 February 9, 2023 05:09
An ItemDecoration that draws dividers between items. Pulled from Android support demos.
/*
* 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