Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
codeswimmer / Android: Calculate Distance From One to Another Location
Created April 4, 2011 14:01
Android: calculate the distance from one location to another.
public static final float calculateDistanceTo(Location fromLocation, Location toLocation) {
return fromLocation.distanceTo(toLocation);
}
@codeswimmer
codeswimmer / Android: Calculate Distance Between Two Locations
Created April 4, 2011 14:04
Android: calculate the distance between two locations
public static final float[] calculateDistanceTo(Location fromLocation, Location toLocation) {
float[] results = new float[];
double startLatitude = fromLocation.getLatitude();
double startLongitude = fromLocation.getLongitude();
double endLatitude = toLocation.getLatitude();
double endLongitude = toLocation.getLongitude();
fromLocation.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
@codeswimmer
codeswimmer / Android: Request That a View Rectangle be Visible, Scrolling if Necessary
Created April 4, 2011 14:24
Android: Request that a view's rectangle be visible on the display, scrolling if necessary
View.requestRectangleOnScreen(Rect rectangle, boolean immediate);
@codeswimmer
codeswimmer / Android: Quick Way to Resize a ViewGroup
Created April 4, 2011 14:26
Android: quick way to resize a ViewGroup
public static final void resizeViewGroup(ViewGroup viewGroup, int width, int height) {
viewGroup.setMinimumWidth(width);
viewGroup.setMinimumHeight(height);
}
@codeswimmer
codeswimmer / Android: assets folder access
Created April 10, 2011 01:19
Android: How to use access files stored in the assets folder
/* How to use access files stored in the assets folder:
*
* Files saved in the assets/ directory are not given a resource ID, so you can't reference them through the R class or from XML resources.
* Instead, you can query files in the assets/ directory like a normal file system and read raw data using AssetManager.
*
* However, if all you require is the ability to read raw data (such as a video or audio file), then save the file in the res/raw/ directory
* and read a stream of bytes using openRawResource().
*
* Notes:
* * AssetManager.list(String path)
@codeswimmer
codeswimmer / Singleton that is lazy loaded.
Created May 4, 2011 20:04
Java: Singleton - Lazy Loaded
public class Singleton {
// Private constructor prevents instantiation from other classes
private Singleton() {}
/**
* SingletonHolder is loaded on the first execution of Singleton.getInstance()
* or the first access to SingletonHolder.INSTANCE, not before.
*/
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
@codeswimmer
codeswimmer / Android: Programmatically Launch Home Screensd
Created May 5, 2011 16:37
Android: Programmatically Launch Home Screen
// Remove the Activity parameter if this is being placed into a class that derives from Activity
public void launchHomeScreen(Activity activity) {
Intent homeIntent = new Intent(Intent.ACTION_MAIN, null);
homeIntent.addCategory(Intent.CATEGORY_HOME);
homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
activity.startActivity(homeIntent);
}
@codeswimmer
codeswimmer / Android: determine the IP address of the device
Created May 18, 2011 02:52
Android: NetworkUtil.determineDeviceIpAddress()
public static final String determineDeviceIpAddress() {
try {
Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
if (en != null) {
for (; en.hasMoreElements() ;) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
@codeswimmer
codeswimmer / Android: RGB to YUV
Created June 17, 2011 03:25
Android: possibilities for converting an image from RGB to YUV (which comes in handy for QR code decoding)
Canvas c = new Canvas(bitmap);
ColorMatrix yuvMatrix = new ColorMatrix();
yuvMatrix.setRGB2YUV();
ColorFilter filter = new ColorMatrixColorFilter(yuvMatrix);
// TODO: the rest...
/*************************************************************************
static void setBackground(View v, Bitmap bm) {
@codeswimmer
codeswimmer / Android: PeriodicExecutor.java
Created June 18, 2011 01:44
Android: lightweight means of performing a task on a periodic basis (i.e. polling every 500 milliseconds).
package com.codeswimmer.android.executor;
import java.util.concurrent.Executor;
import android.os.Handler;
import android.os.SystemClock;
public class PeriodicExecutor implements Executor {
private static final Handler poller = new Handler();
private Runnable command;