Error while trying to use .startsWith() and .contains() methods in kotlin - android

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.

Related

Android Studio : Kotlin autocomplete not working properly

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.

Kotlin EditText show/hide password programmatically

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 = ...

Android Studio not recognizing Espresso/Hamcrest static method names without prefacing with the classname

I wasn't sure exactly what headline to give this question because I'm not sure what this is technically called. In Android Studio while typing out Espresso tests I noticed that it refuses to accept something like this:
onView(withId(R.id.someId)).perform(click());
and instead will only accept this:
Espresso.onView(ViewMatchers.withId(R.id.someId)).perform(ViewActions.click());
even though every example that I've seen online shows the first example as correct code. Why is Android Studio forcing me to preface every ViewMatcher/Espresso/ViewActions/etc. method with the classname even after the imports are included in my class?
To clarify - trying to use the first example shows "cannot resolve method" and using autocomplete on it (which I have to do several times before it will work) invariably autocompletes to the second example. In all the "regular" code for my project autocomplete works correctly and short method names are recognized. I've tried doing a "clean" and "invalidate cache and restart" but no change.
Example of Google doc that shows usage according to the shortened code:
https://developer.android.com/training/testing/ui-testing/espresso-testing.html
You can try to check out your Android Studio Preferences for imports. Just go to Preferences -> Search "imports"
Here are the settings that I use and I don't have that issue:
What fixed the issue for me: just removed red withId in one place. Then just re-entered "withId": after that all the other red withId were replaced with black text color withId and now it compiled successfull!

ContextCompat.GetColor not returning Color

I'm trying to replace Resources.GetColor with ContextCompat.GetColor but the last one does not return a color and I don't get what should i use instead of Resources.GetColor(which became deprecated from API 23). Can anyone help me (see below what I want to achieve)?
Button.SetBackgroundColor(ContextCompat.GetColor(this, Resource.Color.LightRed));
Note that I use Xamarin, but if you have answer in java I can easily adapt it.
Thank you!
ContextCompat just returns an integer representation of your color. You need to convert it to an Android color by splitting its RGB parts up. Use something like this
using Android.Graphics;
public static Color GetColorFromInteger(int color)
{
return Color.Rgb(Color.GetRedComponent(color), Color.GetGreenComponent(color), Color.GetBlueComponent(color));
}
and in your method
btn.SetBackgroundColor(GetColorFromInteger(ContextCompat.GetColor(this, Resource.Color.LightRed);
I had the same problem I think - and resolved it by adding the annotation #SuppressWarnings("ResourceAsColor") on top of the method.
The reason is that, in my opinion, Lint does not recognise the new API at this moment, even though it's valid. Both methods return an integer that represents a resolved color. In my tests Resources.GetColor() and ContextCompat.GetColor() return the same value. However, when using the latter I get an error in Android Studio saying:
Should pass resolved color instead of resource id here: `getResources().getColor(titleColor)`
...which does not make sense, because I AM passing a resolved color. It's just an int, so how can I be wrong... So in conclusion, I think suppressing the Lint Error is a valid way to handle the situation at the moment.
If you disagree please raise your voice, I'd be interested.

How to get clear from " method getTextContent() is undefined for the type Node" error?

I am tring to get an xml doc in my appBut I am getting an error in getTextContent() as "method getTextContent() is undefined for the type Node".I have also added the required packages at the top for it.I have also tried xml-apis-1.0.b2.jar & tried the quick fix of casting it also. I have add this part of this code,
fullReport.append(docs.getElementsByTagName("report").item(0).getTextContent());
Please tell me how can I correct this one.Thanks a million mate.
The one reason may be is that getTextContent () is not defined under type Element dut to the fact your Android project target version should be less than 2.2.It is only available in the higher version of Android to enhance the lower version.Just try updating your version 2.2 or more.Cheers!!!
Maybe because the order of the imports. Take a look at http://www.enterprisesearchblog.com/2009/09/fix-for-gettextcontent-is-undefined-for-the-type-node-for-solr-project-in-eclipse-ide.html

Categories

Resources