I know how to do this on Java.
I copied and paste the code from Java to Kotlin and Android Studio changed to this:
auth_password_text.setInputType(InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD)
But I'm receiving a message (grey not yellow like warning)
I search and found only java and react native answer but not was the best/right way to do this in Kotlin.
Thank you in advance!
In Kotlin you can set some properties accessing directly variable instead of calling setter method, that's the meaning of the warning you are getting.
You can remove warning like this:
auth_password_text.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
For any warning or suggestion reported by the IDE, you can press Alt-Enter, and in most cases (including this one) the IDE will suggest a quickfix that will automatically apply the suggested change.
In this case, it will change the call of a Java setter to a Kotlin property access:
auth_password_text.inputType = ...
Related
Kotlin autocomplete in android studio isn't showing all available suggestion.
The same code snippet in Java suggests setErrorEnabled()
Kotlin autocomplete snippet
I tried restarting IDE, invalidate cache but still don't work as it used to do in Java.
How do I fix this?
Use textPasswordInput.isErrorEnabled = true. Kotlin converts Java getters and setters into Kotlin properties. It still allows you to call setErrorEnabled but since the property syntax is preferred, it doesn’t include the setter method in the autocomplete. Notice your use of the setter is highlighted with a warning.
When I try to use the methods .startsWith and .contains with the variable value, I get the words in red, because apparently, they do not exist.
Can someone help with this error? I tried looking on the internet to see if this is an old deprecated function but I did not find anything apparently it is not deprecated...
Click to see the error here
I think you are leaving an space after calling the contains function. So is like you are not sending any arguments to the function.
It should be like this:
value.contains("/")
And same for startsWith
value.startsWith("-")
I just updated my old Android studio from 4.1.2 to the new ARTIC FOX 2020.3.1... and the error magically disappeared...Thanks.
After upgrading to IntelliJ IDEA 2017.1 variables now do not show their types. Instead, it shows var. Also, it abbreviates some method calls. For example instead me.getIdAsStr() it shows me.idAsStr. How to revert to the old behavior?
This behavior is caused by the Advanced Java Folding plug-in. Either disable the plug-in or change its setting to not fold certain code elements.
To disable showing val/var instead of variable type
go to Settings | Editor | General | Code Folding
and UnCheck Variable Declaration
And just next to it
Uncheck Getters and setters
to start showing getter Setter method calls with full method name
Android Studio shows the error
Unexpected implicit cast to CharSequence: layout tag was TextView
at this code
findViewById<TextView>(R.id.tv_name).text = "text"
If I write
(findViewById<TextView>(R.id.tv_name) as TextView).text = "text"
Everything is fine.
The question is why this happens? Doesn't findViewById<TextView> already have type TextView?
You can use Kotlin Android Extensions
Kotlin Android Extensions are Kotlin plugin that will allow to recover views from Activities, Fragments and Views in an amazing seamless way.
you can directly use
tv_name.text = "text"
no need of findViewById
Reference
https://antonioleiva.com/kotlin-android-extensions/
You can directly use all the views using DataBinding. Less code and very advance feature of android. There are many articles for Android DataBinding.
https://developer.android.com/topic/libraries/data-binding/index.html
This is only warning of lint. You can ignore it with #SuppressLint("WrongViewCast"). This happend because of usage of generic types from Java in Kotlin.
First of, Kotlin under the hood, just wraps up java findViewById method. Up to API 26 explicit cast was neccessary as method returned View, but now it returns . Check this answer No need to cast the result of findViewById?
If you dive into the source code, through invokations of findViewById, you'll get to Window.findViewById method, and if you look to description of it in documentation, you'll see one note there, which says that: "In most cases -- depending on compiler support -- the resulting view is automatically cast to the target class type. If the target class type is unconstrained, an explicit cast may be necessary." https://developer.android.com/reference/android/view/Window.html#findViewById(int)
I don't know what "unconstrained" actually means in this context, but as i understand, in some cases cast is required in others is not, so just deal with it. For example, i tried to add some param to ImageView and compiler didn't show any kind of warnings:
findViewById<ImageView>(R.id.iv).adjustViewBounds = true
I'm developing a Nativescript app, and in some devices the hint is bigger than the input, IOS add three dots at the end (...) like an ellipsis, but android only cut off the hint.
I tried the following code:
let shareInput: TextField = this.page.getViewById<TextField>('share-input');
shareInput.android.setEllipsize("end");
I'm getting an error like this:
java.lang.Exception: Failed resolving method setEllipsize on class
android.widget.EditText
I red some android issues and tried to use setSingleLine method but it does not work.
Any help is welcomed.
So this method is what you need (and are using :) - https://developer.android.com/reference/android/widget/EditText.html#setEllipsize(android.text.TextUtils.TruncateAt)
your argument is incorrect, it isn't expecting a string but an ENUM.
So try setEllipsize(android.text.TextUtils.TruncateAt.END)
And if you're going to be setting this on multiple fields I would add a var for the TruncateAt enum like const TruncateAt = android.test.TextUtils.TruncaeAT then just use that const/var in the argument like (TruncateAt.END) to save on repeating yourself.