Skip to content

Instantly share code, notes, and snippets.

View alexfu's full-sized avatar

Alex Fu alexfu

View GitHub Profile
@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 / AndroidAsync.java
Last active February 7, 2021 15:27
Helpful RxJava functions for daily use
/**
* Transforms an observable to run on a new thread
* and to be observed on Androids main thread.
*/
public static <T> Observable.Transformer<T, T> androidAsync() {
return new Observable.Transformer<T, T>() {
@Override
public Observable<T> call(Observable<T> observable) {
return observable
.subscribeOn(Schedulers.newThread())
@alexfu
alexfu / avd_im_feeling_lucky.sh
Created August 16, 2019 14:03
Starts up a random AVD
#!/usr/bin/env bash
emulator=$ANDROID_HOME/tools/emulator
adb=$ANDROID_HOME/platform-tools/adb
EMULATOR_COUNT=$($emulator -list-avds | wc -l | cut -c 8)
RANDOM_INDEX=$(shuf -i 1-$EMULATOR_COUNT -n 1)
EMULATOR_NAME=$($emulator -list-avds | head -n $RANDOM_INDEX | tail -n 1)
printf "Starting $EMULATOR_NAME..."
#!/bin/bash
# Creates a new named bugfix branch in the following format: bugfix/xyz
# $1 - Name of the branch
# $2 - Base branch
BRANCH_NAME=bugfix/$1
START_POINT=$2
git checkout -b $BRANCH_NAME $START_POINT
@alexfu
alexfu / CheckableImageButton.java
Last active March 19, 2020 06:02
A simple checkable button that has all of the properties that comes with a Button but has checkable states. One thing all, if not most, Checkable views Android provides can't seem to do is center their own Drawables.
/*
* The MIT License (MIT)
*
* Copyright (c) 2015 Alex Fu
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
val units = listOf(
ChronoUnit.MILLENNIA,
ChronoUnit.CENTURIES,
ChronoUnit.DECADES,
ChronoUnit.YEARS,
ChronoUnit.MONTHS,
ChronoUnit.WEEKS,
ChronoUnit.DAYS,
ChronoUnit.HOURS,
ChronoUnit.MINUTES,
@alexfu
alexfu / Demo.kt
Last active March 21, 2019 14:50
Making (Android) Spannable great again with Kotlin
val world = "World"
val mySpannedText = SpannableString("Hello ${world}!")
mySpannedText.spanWith(world) {
what = BackgroundColorSpan(Color.RED)
flags = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
}
@alexfu
alexfu / multi-adb
Created January 4, 2019 18:09
ADB that prompts for which device when multiple devices are available
#!/usr/bin/env python
import sys
import subprocess
def get_devices():
pipe = subprocess.PIPE
process = subprocess.Popen(["adb", "devices"], stdin=pipe, stdout=pipe, stderr=pipe)
output, error = process.communicate()
output_lines = output.strip().split("\n")
@alexfu
alexfu / Instructions.md
Last active November 26, 2018 15:55
Creating GitHub PRs directly from JIRA tickets

Creating GitHub PRs directly from JIRA tickets

With a single command, you will be able to create a GitHub pull request from a JIRA ticket.

git jira-pr -t BUY-123 -b development

The above command will fetch the JIRA ticket and create a pull request using the JIRA ticket ID and summary as the pull request title.

@alexfu
alexfu / activity_main.xml
Last active July 10, 2018 15:13
Stretched LayerDrawable with centered icon
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:layout_width="200dp"
android:layout_height="100dp"