Skip to content

Instantly share code, notes, and snippets.

fun TextView.afterTextChangedDelayed(afterTextChanged: (String) -> Unit) {
this.addTextChangedListener(object : TextWatcher {
var timer: CountDownTimer? = null
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
/** USAGE -> ('a'..'z').randomString(6) */
fun ClosedRange<Char>.randomString(lenght: Int) =
(1..lenght)
.map { (Random().nextInt(endInclusive.toInt() - start.toInt()) + start.toInt()).toChar() }
.joinToString("")
editTextRegisterStepThreeUserName.setText(('a'..'z').randomString(6))
@EmmanuelGuther
EmmanuelGuther / getFirstElementIfCondition.kt
Created October 16, 2018 12:09
Get first element in collection if the condition is true
listFoo?.singleOrNull { it.id == "oooo" }.let {it...}
@EmmanuelGuther
EmmanuelGuther / DateUtilsFormatDateRange.java
Created October 8, 2018 11:48
Date and time formatting
long lastYear = 1407869895000L; // August 12, 2014, 8:58PM
long before = 1439405895000L; // August 12, 2015, 8:58PM
long now = 1442343495000L; // September 15, 2015, 8:58PM
// August 12 – September 15 (default)
DateUtils.formatDateRange(this, before, now, 0);
// August 12, 8:58PM – September 15, 8:58PM (with time)
DateUtils.formatDateRange(this, before, now, DateUtils.FORMAT_SHOW_TIME);
@EmmanuelGuther
EmmanuelGuther / glidePlaceholderExample.kt
Created October 2, 2018 12:04
Glide placeholder example kotlin
fun example(){
Glide.with(context)
.load(items[position])
.apply(RequestOptions()
.placeholder(R.drawable.animated_loading_icon)
)
val selectedSubcategories = selectedCategories.asSequence().map { it?.subCategories }.fold(mutableSetOf<SubCategoryModel>()) { acc, list ->
list?.asSequence()?.filter { it.checked }?.map { acc.add(it) }?.toList()
acc
}
val subcategoriesJoined = selectedSubcategories.asSequence().map { it.name }.joinToString(separator = " • ")
editTextProfessionalRegisterCategories.setText(subcategoriesJoined)
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/layoutHeaderNavView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
private fun printKeyHash() {
try {
val info = activity?.packageManager?.getPackageInfo("com.s", PackageManager.GET_SIGNATURES)
for (signature in info!!.signatures!!) {
val md = MessageDigest.getInstance("SHA")
md.update(signature.toByteArray())
Log.d("KeyHash:", Base64.encodeToString(md.digest(), Base64.DEFAULT))
}
} catch (e: PackageManager.NameNotFoundException) {
@EmmanuelGuther
EmmanuelGuther / android_screen_size_dimens_folder.txt
Created September 4, 2018 13:04
android screen size dimens folder
values-sw720dp 10.1” tablet 1280x800 mdpi
values-sw600dp 7.0” tablet 1024x600 mdpi
values-sw480dp 5.4” 480x854 mdpi
values-sw480dp 5.1” 480x800 mdpi
values-xxhdpi 5.5" 1080x1920 xxhdpi
values-xxxhdpi 5.5" 1440x2560 xxxhdpi
@EmmanuelGuther
EmmanuelGuther / View.kt
Last active June 20, 2018 14:34
Kotlin function extension to change color animatedly
fun View.changeColorAnimatedly(duration: Long, repeatable: Boolean, startColor: Int, endColor: Int) {
val anim = ObjectAnimator.ofInt(this, "backgroundColor", startColor, endColor)
anim.duration = duration
anim.setEvaluator(ArgbEvaluator())
if (repeatable) {
anim.repeatCount = ValueAnimator.INFINITE
anim.repeatMode = ValueAnimator.REVERSE
}
anim.start()
}