Skip to content

Instantly share code, notes, and snippets.

View ElianFabian's full-sized avatar

Elián Fabián ElianFabian

View GitHub Profile
@DanielKnauf
DanielKnauf / medium_horizontal_recyclerview_in_viewpager.kt
Last active July 29, 2024 02:26
OnItemTouchListener blocking MotionEvent from RecyclerViews' parent if RecyclerView can scroll left or right
addOnItemTouchListener(
object : RecyclerView.OnItemTouchListener {
private var startX = 0f
override fun onInterceptTouchEvent(
recyclerView: RecyclerView,
event: MotionEvent
): Boolean =
when (event.action) {
MotionEvent.ACTION_DOWN -> { startX = event.x }
@fadhifatah
fadhifatah / AdaptiveSpacingItemDecoration.kt
Created March 8, 2022 07:59
New file: AdaptiveSpacingItemDecoration to help solve spacing problem inside RecyclerView
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView.ItemDecoration
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.StaggeredGridLayoutManager
/**
* Give equal margin around each [RecyclerView] items, adaptively. Supports known direct subclasses of
@mkovalyk
mkovalyk / PermissionManager.kt
Last active May 10, 2025 04:49
Make Android permission easier
package com.example.myapplication
import android.content.pm.PackageManager
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
@lunatic-fox
lunatic-fox / colors.kt
Created March 11, 2021 03:08
KOTLIN CONSOLE WITH COLORS
//Main constants and functions
const val RC = "\u001b[0m" // Reset foreground and background colors. --> $RC
const val R = "\u001b[7m" // Invert foreground to background. --> $R
const val U = "\u001b[4m" // Underline. --> $U
const val B = "\u001b[1m" // Bold. --> $B
const val I = "\u001b[3m" // Italic. --> $I
const val S = "\u001b[9m" // Strikethrough the text. --> $S
// 256 Colors
fun fg(n:Int) = "\u001b[38;5;${n}m" // Set a foreground color. --> ${fg(40)} //Sets a green color.
@gmk57
gmk57 / 1 ViewBindingDelegates.kt
Last active February 24, 2025 19:38
Kotlin delegates for Android View Binding with usage examples
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.DialogFragment
import androidx.fragment.app.Fragment
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleOwner
import androidx.viewbinding.ViewBinding
@aartikov
aartikov / DelegateAccess.kt
Created January 14, 2021 11:03
Wrap MutableStateFlow to property delegate
internal object DelegateAccess {
internal val delegate = ThreadLocal<Any?>()
internal val delegateRequested = ThreadLocal<Boolean>().apply { set(false) }
}
internal val <T> KProperty0<T>.delegate: Any?
get() {
try {
DelegateAccess.delegateRequested.set(true)
this.get()
class ForegroundServiceLauncher(private val serviceClass: Class<out Service>) {
private var isStarting = false
private var shouldStop = false
private var isCreated = false
@Synchronized
fun startService(context: Context, block: Intent.() -> Unit = {}) {
if (!isCreated) {
isStarting = true
@krishnabhargav
krishnabhargav / kotlin-sealedclass-serialization.kt
Last active October 28, 2024 20:10
Using GSON to support serialization and deserialization of Kotlin Sealed Classes.
import com.google.gson.Gson
import com.google.gson.GsonBuilder
import com.google.gson.TypeAdapter
import com.google.gson.TypeAdapterFactory
import com.google.gson.reflect.TypeToken
import com.google.gson.stream.JsonReader
import com.google.gson.stream.JsonWriter
import kotlin.jvm.internal.Reflection
import kotlin.reflect.KClass
@objcode
objcode / ConcurrencyHelpers.kt
Last active May 13, 2025 01:44
Helpers to control concurrency for one shot requests using Kotlin coroutines.
/* Copyright 2019 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
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
function Get-ArgumentCompleter {
<#
.SYNOPSIS
Get custom argument completers registered in the current session.
.DESCRIPTION
Get custom argument completers registered in the current session.
By default Get-ArgumentCompleter lists all of the completers registered in the session.
.EXAMPLE
Get-ArgumentCompleter