Skip to content

Instantly share code, notes, and snippets.

@vincetreur
vincetreur / sendToDevices.gradle
Created October 29, 2016 12:49
Sometimes you need to send adb commands to all connected devices/emulators from your gradle script. This function will do that for you. Create a closure that takes a serial id (string) as an argument and call sendToDevices().
/*
Example:
task tapPowerButton() {
group = 'device'
description = 'Tap the power button on all devices/emulators'
doLast {
def action = { serial ->
def adb = android.getAdbExe().toString()
exec {
commandLine "$adb -s $serial shell input keyevent 26".split(' ')
@vincetreur
vincetreur / FlashActions
Created May 26, 2016 13:32
An Android Espresso ViewAction that flashes the background color of a View.
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.test.espresso.UiController;
import android.support.test.espresso.ViewAction;
import android.view.View;
import org.hamcrest.Matcher;
import static android.support.test.espresso.matcher.ViewMatchers.isDisplayed;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
@vincetreur
vincetreur / Dimens.java
Last active January 15, 2017 09:42
A small utility to help converting between dips and pixels in your Android code. Most solutions are unreadable, this one should read a lot better.
import android.content.res.Resources;
import android.support.annotation.NonNull;
import android.util.DisplayMetrics;
import android.util.TypedValue;
/**
* Convert between DP and pixels, like {@link java.util.concurrent.TimeUnit} does.
* For example:
* Dimens.DP.toPX(context.getResources(), 16); // 16dp to pixels
* Dimens.PX.toDP(context.getResources(), 16); // 16px to dips
@vincetreur
vincetreur / ListPaddingDecoration.java
Created December 30, 2015 12:13
A RecyclerView.ItemDecoration that adds 8 dp padding to the top of the first and bottom of the last item in a list (or RecyclerView in this case).
import android.content.Context;
import android.graphics.Rect;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;
/**
* Adds 8dp padding to the top of the first and the bottom of the last item in the list,