I am learning to develop android apps in kotlin. I am using android studio 4.0.1. I dragged a switch onto a layout and set its state to true by default with
switch.setChecked(true)
But the IDE suggested I change it to
switch.isChecked()
, which wasn't what I wanted. Then I just happened to enter
switch.isChecked = true
and it worked.
My question is isChecked is a function and we don't invoke it that way. But somehow this worked. Why?
Thank you very much.
Here's a quote from Calling Java code from Kotlin: Getters and Setters:
Methods that follow the Java conventions for getters and setters
(no-argument methods with names starting with get and single-argument
methods with names starting with set) are represented as properties in
Kotlin. Boolean accessor methods (where the name of the getter starts
with is and the name of the setter starts with set) are represented as
properties which have the same name as the getter method.
Since Switch is a class created in Java and has setChecked and isChecked methods, Kotlin can synthesize an isChecked property for you so that accessing it from Kotlin can feel more idiomatic:
switch.isChecked = true
println(switch.isChecked) // prints "true"
Related
Hi I am making a app with Kotlin and I found that I can both use
textView.setText(str)
and
textView.text = $str
I wanna know what I should use and the differences between them.
Thank you.
They're the same in most cases, basically Kotlin generates a synthetic property for the class attributes based on their getter, which you can use to assign values to and get values from.
//So, for most cases
textView.setText("some value");
//Is the same as
textView.text = "some value"
//The second is simply shorter and is the 'kotlin way' of assigning values
Now, here's the catch -
In most cases, this works fine. But, as mentioned, the synthetic property is generated from the getter, if there is a setter as well, then issues arise. The reason is that the getter and the setter may have different types. For example, EditText has Editable getter, now, kotlin creates a synthetic property text of the type Editable.
editText.setText("some value"); //Works
editText.text = "some value" //Won't work, will show an error stating that expected type is Editable
textView.setText(str) and textView.text = $str, does the same job of setting the specified str to TextView. The only difference I can come up with is,
textView.setText(str) // old Java way of setting Text where method setText(str) was being called.
textView.text = $str //new Kotlin way of setting Text where instead of a method, a synthetic property is being called.
As in the Kotlin, you are not using findViewById
so to access your textView, import statement must be like this
import kotlinx.android.synthetic.main.<layout>.*
And textView.text = $str is the Synthetic Property access provided by Kotlin Plugin for android
You can use both, not much difference in the usability, but for the easier code writing
this would be better
For more information, read this https://kotlinlang.org/docs/tutorials/android-plugin.html
Both works the same way.
Java Convention
textView.setText(“…”)
Kotlin Convention
textView.text=”…”
“Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin.”- documentation
thus textView.text=”…” instead of textView.setText(“…”) if you are using Kotlin to follow Kotlin Conventions.
Ref - Begin Kotlin; from an Activity, a Button and a TextView
The method setText() and getText() are called setters and getters, they are automatically generated in kotlin.
class ClassName{
var name: String= "some_value"
}
You can use the name property directly with the object of the class or you can also use the auto-generated setter method.
class Another{
var c = ClassName()
c.name = "value"
c.setName("value")
}
But if a property starts with a val instead of var then it is immutable and does not allow a setter.
In case you want to read further:-
Setters and getters in kotlin
Hi I am making a app with Kotlin and I found that I can both use
textView.setText(str)
and
textView.text = $str
I wanna know what I should use and the differences between them.
Thank you.
They're the same in most cases, basically Kotlin generates a synthetic property for the class attributes based on their getter, which you can use to assign values to and get values from.
//So, for most cases
textView.setText("some value");
//Is the same as
textView.text = "some value"
//The second is simply shorter and is the 'kotlin way' of assigning values
Now, here's the catch -
In most cases, this works fine. But, as mentioned, the synthetic property is generated from the getter, if there is a setter as well, then issues arise. The reason is that the getter and the setter may have different types. For example, EditText has Editable getter, now, kotlin creates a synthetic property text of the type Editable.
editText.setText("some value"); //Works
editText.text = "some value" //Won't work, will show an error stating that expected type is Editable
textView.setText(str) and textView.text = $str, does the same job of setting the specified str to TextView. The only difference I can come up with is,
textView.setText(str) // old Java way of setting Text where method setText(str) was being called.
textView.text = $str //new Kotlin way of setting Text where instead of a method, a synthetic property is being called.
As in the Kotlin, you are not using findViewById
so to access your textView, import statement must be like this
import kotlinx.android.synthetic.main.<layout>.*
And textView.text = $str is the Synthetic Property access provided by Kotlin Plugin for android
You can use both, not much difference in the usability, but for the easier code writing
this would be better
For more information, read this https://kotlinlang.org/docs/tutorials/android-plugin.html
Both works the same way.
Java Convention
textView.setText(“…”)
Kotlin Convention
textView.text=”…”
“Methods that follow the Java conventions for getters and setters (no-argument methods with names starting with get and single-argument methods with names starting with set) are represented as properties in Kotlin.”- documentation
thus textView.text=”…” instead of textView.setText(“…”) if you are using Kotlin to follow Kotlin Conventions.
Ref - Begin Kotlin; from an Activity, a Button and a TextView
The method setText() and getText() are called setters and getters, they are automatically generated in kotlin.
class ClassName{
var name: String= "some_value"
}
You can use the name property directly with the object of the class or you can also use the auto-generated setter method.
class Another{
var c = ClassName()
c.name = "value"
c.setName("value")
}
But if a property starts with a val instead of var then it is immutable and does not allow a setter.
In case you want to read further:-
Setters and getters in kotlin
Well I have a WebView, and the following property setting works:
webview.settings.cacheMode = WebSettings.LOAD_NO_CACHE
but not this one:
webview.settings.appCacheEnabled = false
Instead, I have to use the old way:
webview.settings.setAppCacheEnabled(false)
Can you tell me why? Thanks.
According to the official documentation:
Note that, if the Java class only has a setter, it will not be visible as a property in Kotlin, because Kotlin does not support set-only properties at this time.
If you look at the WebSettings abstract class, you'll see it only has public abstract void setAppCacheEnabled(boolean flag); method and no getters for this property, hence Kotlin doesn't allow using a property access syntax here.
Actually, it's worth noting that while creating synthetic property, not only Kotlin looks for setter and getter methods that follow Java conventions, but it also infers property's type from the getter which comes in play in case of subclasses overriding getter methods that return more specific type than their superclasses.
e.g. this WebSettings Java class.
It has a Java method setJavaScriptEnabled(boolean) that turns into a Kotlin property javaScriptEnabled as below, but there is also setSupportZoom(boolean) that does not turn into a Kotlin property supportZoom.
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.setSupportZoom(false)
settings.builtInZoomControls = false
settings.setSupportMultipleWindows(true)
From the documentation:
Boolean accessor methods (where the name of the getter starts with is and the name of the setter starts with set) are represented as properties which have the same name as the getter method.
And still as of Kotlin 1.2.0:
Note that, if the Java class only has a setter, it will not be visible as a property in Kotlin, because Kotlin does not support set-only properties at this time.
There is no method in the Java class of signature boolean isSupportMultipleWindows() and boolean supportMultipleWindows() does not match the property representation in Kotlin.
In Kotlin, when using kotlinx.android.synthetic to access the View (e.g. Button), the setEnabled() function is missing? The isEnabled() function is still there.
How could I setEnabled()?
As said in the reference, Java getters and pairs of getter and setter are represented as properties in Kotlin, using the following logic:
T getSomething() (+ void setSomething(T)) → something: T
T isSomething() (+ void setSomething(T)) → isSomething: T
If there is a setter, a var-property is seen from Kotlin, otherwise it's an unmodifiable val.
Instead of setEnabled(value) just use isEnabled = value.
Apparently we now set it using
button.isEnabled = true