Skip to content

Instantly share code, notes, and snippets.

@mformetal
mformetal / PreDrawer.java
Last active April 7, 2017 10:04
A simple way to add a predraw listener to a view
public abstract class PreDrawer<T extends View> {
// Private constructor prevent instantiation by the caller. Use addPredrawer() method instead.
private PreDrawer(final T view) {
// Adds the observer to the given view, then calls notifyPredraw() when clean up has been done ready
final ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
public class CancelEditTextListener {
private EditText widget;
private Drawable[] hideDrawables;
private Drawable canceler;
public CancelListener(EditText editText) {
widget = editText;
widget.addTextChangedListener(textWatcher);
View.OnTouchListener touchListener = (v, event) -> {
@mformetal
mformetal / MotionEventHandler.java
Created March 8, 2016 05:19
My solution to the Pinch-To-Zoom problem
// Defines the actions of the Zoomer class. Feel free to implement as you see fit
public interface MotionEventHandler {
// Defines which "Mode" the user is in. NONE means they have not taken any action, DRAG means tehy're dragging,
// ZOOM means they're zooming
enum Mode {
NONE,
DRAG,
ZOOM
}
@mformetal
mformetal / BaseFragment.java
Created April 5, 2016 01:47
Base-Handling class for Fragments. Yes, I hate casting this much.
public abstract class BaseFragment extends Fragment {
@Override
public void onAttach(Context context) {
super.onAttach(context);
((MainApp) context.getApplicationContext()).getApplicationComponent().inject(this);
Map<Object, Class> map = getCastMap();
for (Object object: map.keySet()) {
Class clazz = map.get(object);
@mformetal
mformetal / build.gradle
Created June 7, 2016 15:18
Gradle command line builds
task appStart(type: Exec, dependsOn: 'installDebug') {
def adb = "$System.env.ANDROID_HOME/platform-tools/adb"
commandLine "$adb", 'shell', 'am', 'start', '-n', 'package.name/activityclass'
}