id names are red in Android Studio (Kotlin) [duplicate] - android

This question already has answers here:
kotlinx.android.synthetic unused android studio issue
(10 answers)
The 'kotlin-android-extensions' Gradle plugin is deprecated
(8 answers)
Closed 2 years ago.
I made a very basic dice roll app (in Kotlin) using Android Studio with an imageView and a button. The imageView's id is "imageView" and the button's id is "button". But in the MainActivity.kt file the id names are red in colour and when I hover over them it says "unresolved reference: imageView (or button)"
Why is this happening and how do I solve this?
The id names are same on both the activity_main.xml and MainActivity.kt file:

Your activity needs to know what those names refer to, the platform doesn't automatically link views inflated from an XML layout file. Historically this is done in Android by calling the findViewById() method like this:
lateinit var myTextView
override fun onCreate(savedInstanceBundle: Bundle?) {
super.onCreate(savedInstanceBundle)
// The layout file is "inflated" automatically here
setContentView(R.layout.layout_name)
// Then we can assign a reference to views by ID:
myTextView = findViewById<TextView>(R.id.text_view_name)
}
Since then, shortcuts have come around: Butterknife, Kotlin Synthetics, or the current accepted standard, Jetpack View Binding. if you just need to grab a few views quick call findViewByid() but as you work with Android it's worth taking the time to enable View Binding to cut down on this boilerplate code.

just restart by using invalidate caches/Restart as you seeing this image and after this clean and then rebuild your project

You have to use kotlin extension.
In your app gradle.build add apply plugin: 'kotlin-android-extensions'
In your class add import for import kotlinx.android.synthetic.main.<layout>.* where is the filename of your layout.

Related

I need a way to change the image displayed by an ImageView in Android Studio (in Kotlin) [duplicate]

This question already has answers here:
Changing ImageView source
(8 answers)
Closed 1 year ago.
I need to change an ImageView in Android Studio to display a different image (while the program is running). I didn't find a way to accomplish this in Kotlin yet. Thanks for your time.
You need a reference to the ImageView (for example, using ViewBinding or findViewById), then you can use the method setImageResource, as stated in the documentation
Example:
// declare ImageView reference
var myImageView: ImageView
// in onCreate
myImageView = findViewById(R.id.myImageView)
// change image
myImageView.setImageResource(R.drawable.imageName)

Is there any way to add layout tag using android studio? [duplicate]

This question already has answers here:
DataBinding templates for Android Studio?
(4 answers)
Closed 2 years ago.
I'm migrating an old android project to use the Android DataBinding library. The first step is adding the layout tag as the root tag of all layout files in the project.
Doing this manually is ready frustrating so I want to ask Is there any way I can do it automatically in Android Studio?
Similar to converting a layout file to use motion layout. Just right click on the layout file and hit the convert to motion layout option.
Thanks
Click on the bulb icon on the top and convert to data binding layout as shown in picture that i added

Typo in android:onClick tag in layout XML file not shown as error. Build completes without warning

Developing an Android app with Kotlin and Android Studio 4.1. I mistyped the name of the onClick function of a button in the layout XML tag android:onClick, i.e. the function name does not match the name used in the code file (...kt). The build ran without error or warning, and the app was launched on the phone (emulator). However (no surprise once you know about the typo), the app crashed when the button was clicked, since Android cannot call the function.
Questions:
I find it irritating that the build completes despite the (obvious) problem. Is this expected bevahiour of Android Studio?
Android Studio does mark the code tab (red underline) if there is an error, but it doesn't do so with the XML file tab. Is this expected behaviour, again?
Note: There is said error in the XML file as well as an error in the .kt file added for illustration purpose.
From the picture, you can see that the xml CAN report errors, you just have to really break it. In this sense, it means that you have to type something which it cannot parse or make sense of, such as trying to define a property which doesn't exist.
Now, in terms of your button click listener, the xml doesn't really have any reference towards where it will be used, unless you tell it, by using context in your root layout :
tools:context="your_activity_here"
Complete example:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.overview.OverviewActivity"> //<-- this is key
But, this will also still compile and install and you won't have any compile time issues here, so this won't really help in your situation
Perhaps a better approach would be to ask why you're still defining these click listeners in the xml, when kotlin offers us synthetic imports. Here's an example:
Instead of defining the click listener in the xml file, like you're doing, the following is also valid:
Assuming you have a basic button
<Button
android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
...
now in the activity, you can just reference the button straight, by using:
myButton.setOnClickListener { //this is the ID of the component in xml you're trying to access
//your logic here
}
This is because of a feature kotlin has, called synthetic imports, which allows us to directly access xml components by importing the entire file, such as :
import kotlinx.android.synthetic.main.example.*
Why I think this is better ? (personal opinion)
This creates an additional separation between the xml and the implementation - by setting the click listener in the activity/fragment, this file is now entirely reusable somewhere else, so we can use it in another part of our app without having to define the same listeners everywhere and by only making use of synthetics to handle your event listeners, you actually will have compile time errors, preventing you from building if a component isn't found
In layout which you are importing in your Activity,
place onClick() in the element and refrence it to the Activity where you imported onClick listner android:onClick="onClick"

Unexpected XML View formatting Android Studio 3.5 [duplicate]

This question already has answers here:
Android studio 3.5 refactor issue
(9 answers)
Closed 3 years ago.
After updating Android Studio to 3.5 whenever I auto format code (cmd+opt+l) it changes the order of the view tags inside the xml.
So for example in a LinearLayout your views would move around
Found the solution:
Preferences -> Editor -> Code Style -> XML
Search for Set from on the top left.
Set from -> Predefined Style -> Android
Don't forget to apply your changes
Hope this helps someone

Kotlin layout id is showing two imports

I imported a module to my project. That module was working in my sample,
But in my app this is showing two imports, Can someone let me know, why did this happen. And what will be its solution. Because I never imported the ids in kotlin, so why I faced this behaviour now?
Please note:
It is not about one id drawer_layout, all my ids are showing two imports, Also please note that I never imported all these ids in my old sample project. But in my other app, all ids are showing two imports.
Edit
I am adding some additional screenshots for better understanding.
No duplicate id
But showing two imports
All ids are showing two imports in class (Almost 100 erros in class)
it's automatic in kotlin. You just have to choose kotlinx.android.synthetic
I've tried it in fragments and it can't. when in fragment you still have to use findViewById
So if you have a view in the xml layout with id = drawer_layout, there are two imports:
acr.browser.lightning.R.id.drawer_layout : This is the ID mapping that you will find in your R.java file. You will have an entry for every view there. This import exists even when you are not using kotlin. You will use this, for example, when you want to compare the View Id to determine which View is clicked, etc...
kotlinx.android.synthetic.main.activity_main.drawer_layout : Now this is the way by which you can access the VIEW without using findViewById and reference it directly.
Looking at your code, I see that the 2nd import is what you need to choose. It's normal for the IDE to show both the imports, because both are valid. Now, there is a bug in Android Studio (at least in the 3.1 version), where even when you choose the 2nd import, sometimes Android Studio will show ALL THE ID's as errors (since it won't recognize the synthetic import). To resolve this you need to Build the app again, and the errors will go away (if the synthetic import is still present)

Categories

Resources