Skip to content

Instantly share code, notes, and snippets.

@fleficher
fleficher / layout.xml
Created Mar 12, 2021
ViewBinding with include
View layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:id="@+id/mergeLayout"
layout="@layout/layout_merge" />
</LinearLayout>
View layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include layout="@layout/layout_merge" />
</LinearLayout>
@fleficher
fleficher / layout_merge.xml
Last active Mar 12, 2021
Simple merge layout
View layout_merge.xml
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/titleLbl"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</merge>
@fleficher
fleficher / FragmentAutoClearedValueBinding.kt
Last active Jan 25, 2022
Fragment View Binding Delegate
View FragmentAutoClearedValueBinding.kt
class FragmentAutoClearedValueBinding<T : ViewBinding>(
val binder: (View) -> T
) : ReadOnlyProperty<Fragment, T>,
DefaultLifecycleObserver {
private var value: T? = null
override fun onDestroy(owner: LifecycleOwner) {
value = null // Clear reference.
}
@fleficher
fleficher / MainActivity.kt
Created Mar 12, 2021
Activity ViewBinding advanced
View MainActivity.kt
class MainActivity : AppCompatActivity() {
private val binding by viewBinding(ActivityMainBinding::inflate)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
}
}
@fleficher
fleficher / ActivityViewBinding.kt
Last active Mar 12, 2021
Activity ViewBinding simple
View ActivityViewBinding.kt
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
}
@fleficher
fleficher / CustomTask.kt
Created Jan 26, 2020
custom task with KTS
View CustomTask.kt
// Our custom task inside buildSrc folder
open class PingUrlTask : DefaultTask() {
@Input
lateinit var url: String
@TaskAction
fun updateTranslations() {
println("Begin task")
View build.gradle.kts
// In your buildSrc folder
sealed class BuildType(val secret: String) {
object DebugBuildType : BuildType(secret = "sword")
object ReleaseBuildType : BuildType(secret = "shield")
}
@fleficher
fleficher / BuildConfigFields.kt
Created Dec 24, 2019
build config fields KTX
View BuildConfigFields.kt
// For flavors usage
fun BaseFlavor.buildConfigBoolean(name: String, value: Boolean) =
buildConfigField("Boolean", name, value.toString())
fun BaseFlavor.buildConfigString(name: String, value: String) =
buildConfigField("String", name, "\"$value\"")
// For build types usage
fun BuildType.buildConfigString(name: String, value: String) =
buildConfigField("String", name, "\"$value\"")
@fleficher
fleficher / Plugins.kt
Created Dec 24, 2019
Plugins declaration
View Plugins.kt
fun PluginDependenciesSpec.androidApp(): PluginDependencySpec =
id("com.android.application")
fun PluginDependenciesSpec.androidLibrary(): PluginDependencySpec =
id("com.android.library")
fun PluginDependenciesSpec.kotlinAndroid(): PluginDependencySpec =
kotlin("android")
fun PluginDependenciesSpec.kotlinAndroidExt(): PluginDependencySpec =