You can also combine when
and with
to get a nice syntax:
with(strAction) {
when {
contains("Grid") -> println("position is 1")
contains("bar") -> println("foo")
startsWith("foo") -> println("bar")
else -> println("foobar")
}
}
You can also save the result of when
into a property:
val result = with(strAction) {
when {
contains("bar") -> "foo"
startsWith("foo") -> "bar"
else -> "foobar"
}
}
println(result)
Related Contents:
- Default interface methods are only supported starting with Android 7.0 (Nougat)
- How to get the current index in for each Kotlin
- How to make primary key as autoincrement for Room Persistence lib
- How to set the part of the text view is clickable
- Android Room – simple select query – Cannot access database on the main thread
- Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.1, expected version is 1.1.15
- Kotlin addTextChangeListener lambda?
- Kotlin Error : Could not find org.jetbrains.kotlin:kotlin-stdlib-jre7:1.0.7
- “Not enough information to infer parameter T” with Kotlin and Android
- How to get current local date and time in Kotlin
- “Prefer to run the dagger processor over that class instead” in Kotlin
- What’s Kotlin Backing Field For?
- RequiresApi vs TargetApi android annotations
- Can’t create new Kotlin project after updating to Android Studio 4.2
- Kotlin: Whats does “return@” mean?
- Jetpack Compose – Column – Gravity center
- FragmentPagerAdapter deprecated
- RecyclerView itemClickListener in Kotlin
- Android Studio 3.1: Erroneous unresolved references in editor
- MutableLiveData with initial value
- How to close the virtual keyboard from a Jetpack Compose TextField?
- onBackPressed() deprecated, What is the alternative?
- How to create constructor of custom view with Kotlin
- import kotlinx.android.synthetic.main.activity_main is not working
- Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException
- ‘setHasOptionsMenu(Boolean): Unit’ is deprecated. Deprecated in Java
- Jetpack Compose on Kotlin 1.5.0
- How can I represent a “many to many” relation with Android Room when column names are same?
- kotlin and ArgumentCaptor – IllegalStateException
- Using rememberCoroutineScope() vs LaunchedEffect
- In which situation val/var is necessary in Kotlin constructor parameter?
- Weights in Jetpack compose
- Navigation component Kotlin – cannot be found from the current destination
- java.lang.IllegalStateException: Dex archives: setting .DEX extension only for .CLASS files
- How to make an API request in Kotlin?
- Kotlin: How to get and set a text to TextView in Android using Kotlin?
- Jetpack Compose – Centering Text
- The project is using an incompatible version (AGP 7.3.0-alpha07) of the Android Gradle plugin
- How to properly use setOnLongClickListener() with Kotlin
- Mockk Missing calls inside every { … } block
- Kotlin – idiomatic way to create a Fragment newInstance pattern
- Dagger 2 multibindings with Kotlin
- Kotlin – How to add OnPageChangeListener in ViewPager
- Overload Resolution Ambiguity error in kotlin
- Access application context in companion object in kotlin
- Create Vertical Divider Jetpack Compose
- For Loop in kotlin [closed]
- Unresolved reference: asLiveData while converting Flow to LiveData
- Pass Parcelable argument with compose navigation
- Kotlin Android- How implement CheckBox.OnCheckedChangeListener?