Skip to content

Instantly share code, notes, and snippets.

View albodelu's full-sized avatar

Al B. albodelu

View GitHub Profile
@albodelu
albodelu / CustomTypefaceSpan.kt
Created June 13, 2018 05:03 — forked from florina-muntenescu/CustomTypefaceSpan.kt
Style internationalized text using Annotations
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@albodelu
albodelu / CreditCardNumberFormattingTextWatcher.kt
Created October 30, 2017 03:44 — forked from hleinone/CreditCardNumberFormattingTextWatcher.kt
Android EditText TextWatcher for formatting credit card number made with Kotlin
class CreditCardNumberFormattingTextWatcher : TextWatcher {
private var current = ""
override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
}
override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
}
override fun afterTextChanged(s: Editable) {
@albodelu
albodelu / Presenter.kt
Created October 8, 2017 08:33 — forked from ntoskrnl/Presenter.kt
Android. Example of how to store presenter in retained fragment and enable it to persist and restore view state.
interface Presenter<in V, in R> {
fun create()
fun attachRouter(router: R)
fun attachView(view: V)
fun detachView()
@albodelu
albodelu / ViewCompatExt.kt
Created October 5, 2017 17:10 — forked from rharter/ViewCompatExt.kt
Extension methods to easily provide all functionality in ViewCompat. This makes it super easy to see when there is a compat method available in code completion right alongside the framework methods.
import android.content.ClipData
import android.graphics.Paint
import android.os.Build
import android.os.Bundle
import android.support.v4.view.*
import android.support.v4.view.accessibility.AccessibilityNodeInfoCompat
import android.support.v4.view.accessibility.AccessibilityNodeProviderCompat
import android.view.*
/**
@albodelu
albodelu / FragmentArgumentDelegate.kt
Created September 30, 2017 09:09 — forked from yanngx/FragmentArgumentDelegate.kt
Fragment arguments without hassle !
package be.brol
import android.os.Binder
import android.os.Bundle
import android.support.v4.app.BundleCompat
import android.support.v4.app.Fragment
/**
* Eases the Fragment.newInstance ceremony by marking the fragment's args with this delegate
* Just write the property in newInstance and read it like any other property after the fragment has been created
import android.content.Context
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.support.annotation.AnyRes
import android.support.v4.app.Fragment
import android.support.v4.content.res.ResourcesCompat.*
import android.view.View
val Context.animations
get() = ResourceMapper { resources.getAnimation(it) }
@albodelu
albodelu / KIntent.kt
Created September 22, 2017 03:27 — forked from passsy/KIntent.kt
Kotlin extension functions to start a generic Activity
package com.pascalwelsch.extensions
import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Build
import android.os.Bundle
/**
* Extensions for simpler launching of Activities
@albodelu
albodelu / NearestNeighbors.kt
Created September 21, 2017 04:59 — forked from Atternatt/NearestNeighbors.kt
Recommendation algorithms
fun List<Number>.getNearestNeighbors(posibleNeighbors: List<List<Number>>, numberOfNeighbors: Int): List<List<Double>> {
return posibleNeighbors.mapIndexed { index, posibleNeighbor -> index to this.pearsonCorrelationWith(posibleNeighbor) }
.sortedBy { it.second }
.take(numberOfNeighbors)
.map { posibleNeighbors[it.first].map { it.toDouble() } }
}
@albodelu
albodelu / BackgroundColorAnimation.kt
Created September 21, 2017 04:58 — forked from Atternatt/BackgroundColorAnimation.kt
Property delegation Examples
fun backgroundColorAnimator(): ReadWriteProperty<View, Int> =
object : ReadWriteProperty<View, Int> {
override fun getValue(thisRef: View, property: KProperty<*>): Int {
return (thisRef.background as? ColorDrawable)?.color ?: 0
}
override fun setValue(thisRef: View, property: KProperty<*>, value: Int) {
ValueAnimator.ofObject(ArgbEvaluator(), getValue(thisRef, property), value).apply {
@albodelu
albodelu / DelegatesAndExtensions.kt
Created September 21, 2017 04:57 — forked from Atternatt/DelegatesAndExtensions.kt
DelegatesAndExtensions
fun alphaWithBounceAnimator(): ReadWriteProperty<ImageView, Int> =
object : ReadWriteProperty<ImageView, Int> {
override fun getValue(thisRef: ImageView, property: KProperty<*>): Int {
return thisRef.imageAlpha
}
override fun setValue(thisRef: ImageView, property: KProperty<*>, value: Int) {
val animX = ObjectAnimator.ofFloat(thisRef, "scaleX", 1f, 2.5f, 1f)