Skip to content

Instantly share code, notes, and snippets.

@codeswimmer
codeswimmer / Android: Determine If You're Running on the Main UI Thread
Created February 17, 2011 21:45
Android: determine if you're running on the main UI thread
package com.codeswimmer.android.util;
import android.os.Looper;
public class ThreadUtil {
public static final boolean isRunningOnUiThread() {
return Looper.getMainLooper().getThread() == Thread.currentThread();
}
}
@codeswimmer
codeswimmer / Android.Bitmap.Rotate
Created March 7, 2011 17:25
Android: rotate a bitmap
public Bitmap rotateBitmap(Bitmap original, float degrees) {
int width = original.getWidth();
int height = original.getHeight();
Matrix matrix = new Matrix();
matrix.preRotate(degrees);
Bitmap rotatedBitmap = Bitmap.createBitmap(original, 0, 0, width, height, matrix, true);
Canvas canvas = new Canvas(rotatedBitmap);
canvas.drawBitmap(original, 5.0f, 0.0f, null);
@codeswimmer
codeswimmer / Android.Set.Orientation
Created March 10, 2011 01:19
Android: programmatically set screen orientation
Use this to make it landscape:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
...and this to make it portrait:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
@codeswimmer
codeswimmer / Android.getDeviceSerialNumber()
Created March 10, 2011 01:40
Android: get a device's serial number
public static String getDeviceSerialNumber(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
return telephonyManager.getDeviceId();
}
@codeswimmer
codeswimmer / Android determine screen orientation
Created March 11, 2011 21:09
Android: determine current screen orientation
public static final int determineCurrentScreenOrientation(Context context) {
Display display = ((WindowManager) context.getSystemService(WINDOW_SERVICE)).getDefaultDisplay();
int orientation = display.getOrientation();
}
@codeswimmer
codeswimmer / Android Horizontal Scrollable GridView
Created March 14, 2011 19:16
Android: GridView that scrolls horizontally and vertically
public class Test extends Activity {
GridView gv;
Gallery g[] = new Gallery[3];
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gv = (GridView)findViewById(R.id.gridview);
gv.setAdapter(new GAdapter());
for (int i = 0; i < g.length; i++) {
@codeswimmer
codeswimmer / Android: AsyncTaskLoader<D> Skeleton
Created March 24, 2011 04:42
Android: AsyncTaskLoader Fragment
public class RetrievedDataFragment extends Fragment implements LoaderManager.LoaderCallbacks<D> {
public RetrievedDataFragment() {
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getLoaderManager().initLoader(0, null, this);
@codeswimmer
codeswimmer / Android: Determine if the Device Currently Has Network Connectivity
Created March 30, 2011 16:46
Android: determine if the device currently has any network connectivity (mobile internet or wifi).
public static final boolean hasNetworkConnectivity(Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
NetworkInfo mobile = connectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
return (wifi.isAvailable() || mobile.isAvailable())
}
@codeswimmer
codeswimmer / Android: MotionEvent.ACTION converted to String
Created April 3, 2011 20:34
Android: covert the given MotionEvent.getAction() result into a String.
public static String eventActionToString(int eventAction) {
switch (eventAction) {
case MotionEvent.ACTION_CANCEL: return "Cancel";
case MotionEvent.ACTION_DOWN: return "Down";
case MotionEvent.ACTION_MOVE: return "Move";
case MotionEvent.ACTION_OUTSIDE: return "Outside";
case MotionEvent.ACTION_UP: return "Up";
case MotionEvent.ACTION_POINTER_DOWN: return "Pointer Down";
case MotionEvent.ACTION_POINTER_UP: return "Pointer Up";
}
@codeswimmer
codeswimmer / Android: How to Determine Current Speed
Created April 4, 2011 13:58
Android: determine the current speed
public static final float getCurrentSpeed(double location, int formatType) {
Location location = Location.convert(location, formatType)
return location.getSpeed();
}