- https://developer.android.com/kotlin/style-guide
- https://kotlinlang.org/docs/coding-conventions.html
./gradlew [module:]dependencies
./gradlew assemble[buildVariant]
./gradlew install[buildVariant]
./gradlew [module:]test[buildVariant]UnitTest
./gradlew [module:]connected[buildVariant]AndroidTest
The following custom task in the project-level build.gradle
is to delete the project-level build
directory when clicking the menu bar > Build
> Clean Project
.
task clean(type: Delete) {
delete rootProject.buildDir
}
val encoded: String = "<>&'\"".htmlEncode() // <>&'"
val decoded: String = String =
Html.fromHtml("<>&'"", Html.FROM_HTML_MODE_COMPACT).toString() // <>&'"
String.htmlEncode is a part of the Core KTX library and is syntactic sugar for TextUtils.htmlEncode .
val color: Color = Color.valueOf(0x11223344)
/** isOpen and close() require "androidx.drawerlayout:drawerlayout:1.1.0" or higher. */
override fun onBackPressed() {
if (drawerLayout.isOpen) {
drawerLayout.close()
return
}
super.onBackPressed()
}
OnBackPressedDispatcher.hasEnabledCallbacks()
returns true if both of the following are met.- There is at least one callback registered with this dispatcher.
- Your Activity is being between
onStart()
andonStop()
(both inclusive). If you overrideonStart()
and/oronStop()
, it is betweensuper.onStart()
andsuper.onStop()
. - Even if you pass a Fragment to
OnBackPressedDispatcher.addCallback(...)
, the Fragment's lifecycle does not affectOnBackPressedDispatcher.hasEnabledCallbacks()
.
- If your Activity overrides
onBackPressed()
but you forget to callsuper.onBackPressed()
in it, your callback will never be called.
class MyCustomView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyle: Int = 0
) : View(context, attrs, defStyle) {
init {
context.withStyledAttributes(
attrs,
R.styleable.MyCustomView,
defStyle,
0
) { // simpler than Theme.obtainStyledAttributes(...)
// ...
}
}
}
val cookieManager = CookieManager.getInstance()
val url1 = "example.com"
val url2 = "example.com/foo"
val url3 = "sub.example.com"
val url4 = "https://example.com"
val url5 = "http://example.com"
cookieManager.setCookie(url1, "a=1")
cookieManager.setCookie(url2, "b=2")
cookieManager.setCookie(url3, "c=3")
cookieManager.setCookie(url4, "d=4")
cookieManager.setCookie(url5, "e=5")
cookieManager.setCookie(url5, "e=5!")
val cookie1: String = cookieManager.getCookie(url1) // a = 1; b = 2; d = 4; e = 5!
val cookie2: String = cookieManager.getCookie(url2) // a = 1; b = 2; d = 4; e = 5!
val cookie3: String = cookieManager.getCookie(url3) // c = 3
val cookie4: String = cookieManager.getCookie(url4) // a = 1; b = 2; d = 4; e = 5!
val cookie5: String = cookieManager.getCookie(url5) // a = 1; b = 2; d = 4; e = 5!