Skip to content

Instantly share code, notes, and snippets.

@rajohns08
rajohns08 / UIView.swift
Last active September 28, 2016 00:44
iOS / Swift - Autolayout wrapper to reduce boilerplate
extension UIView {
var differentSuperviewsWarningMessage: String {
return "Since you are adding a constraint to self.superview, self and the view passed in need to have the same superview. The views you are trying to align do not have the same superview."
}
func centerInSuperview() {
self.centerVerticallyInSuperview()
self.centerHorizontallyInSuperview()
}
@rajohns08
rajohns08 / Example.java
Last active June 9, 2016 00:30
Android - Use layout file for map marker
public static MarkerOptions createMarker(Context context, LatLng point, int bedroomCount) {
MarkerOptions marker = new MarkerOptions();
marker.position(point);
int px = context.getResources().getDimensionPixelSize(R.dimen.map_marker_diameter);
View markerView = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.map_circle_text, null);
markerView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
markerView.layout(0, 0, px, px);
markerView.buildDrawingCache();
TextView bedNumberTextView = (TextView)markerView.findViewById(R.id.bed_num_text_view);
Bitmap mDotMarkerBitmap = Bitmap.createBitmap(px, px, Bitmap.Config.ARGB_8888);
@rajohns08
rajohns08 / Person.java
Last active June 4, 2016 17:12
Android / Java - Gson model object without making container object to unwrap root element
public class Person {
public static class Container {
public Person the_person;
}
public static Person fromJson(String json) {
Gson gson = new Gson();
Person.Container container = gson.fromJson(json, Person.Container.class);
return container.the_person;
@rajohns08
rajohns08 / FocusChangeEditText.java
Last active December 4, 2017 06:34
Android - Hide keyboard when tapping outside of an EditText
public class FocusChangeEditText extends android.support.v7.widget.AppCompatEditText {
public FocusChangeEditText(Context context, AttributeSet attrs) {
super(context, attrs);
OnFocusChangeListener dismissKeyboardOnTapListener = new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus) {
Keyboard.hide(v);
}
}
@rajohns08
rajohns08 / image_view.xml
Created April 28, 2016 13:04
Android - Add border to ImageView that is center cropped
<ImageView
android:padding="2dp"
android:layout_width="110dp"
android:layout_height="110dp"
android:src="@drawable/dummy_pic"
android:scaleType="centerCrop"
android:cropToPadding="true"
android:background="@color/white" />
@rajohns08
rajohns08 / CustomFontTextView.java
Last active May 1, 2016 19:46
Android - Custom view attributes with a custom font
public class CustomFontTextView extends TextView {
public CustomFontTextView(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CustomFontTextView, 0, 0);
try {
String fontName = ta.getString(R.styleable.CustomFontTextView_text_font);
this.setTypeface(Font.fromString(context, fontName));
} finally {
ta.recycle();
@rajohns08
rajohns08 / BaseUrl.swift
Created April 19, 2016 01:13
iOS / Swift - Base url class for getting from environment
class BaseURL {
class func getFromEnvironment() -> String {
#if DEBUG
return "http://192.168.1.126"
#else
return "https://judgecardx.com"
#endif
}
@rajohns08
rajohns08 / RestApi.swift
Created April 19, 2016 01:12
iOS / Swift - Simple RestApi util class
class RestApi {
class func post(request: NSMutableURLRequest, completion: (response: String?, json: AnyObject?) -> ()) {
dataTask(request, method: "POST", completion: completion)
}
class func put(request: NSMutableURLRequest, completion: (response: String?, json: AnyObject?) -> ()) {
dataTask(request, method: "PUT", completion: completion)
}
@rajohns08
rajohns08 / Permission.java
Last active March 15, 2016 01:30
Android - Permission Utility Helper
import android.content.Context;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import static android.Manifest.permission.*;
public class Permission {
public static boolean granted(Context context, String permission) {
int permissionCheck = ContextCompat.checkSelfPermission(context, permission);
@rajohns08
rajohns08 / MyProgressDialog.java
Last active March 18, 2016 02:13
Android - Custom progress dialog loading indicator utility class
public class MyProgressDialog {
private static ProgressDialog progressDialog;
public static void show(Context context, int messageResourceId) {
if (progressDialog != null) {
progressDialog.dismiss();
}
int style;