Android Studio Kotlin Class doesn't find resources - android

I have a problem where every single resource isn't found in my kotlin class, like strings, layouts, menus, drawables, and so on. They do exist.

To solve this, make sure Android Studio hasn't auto-imported android.R. This will change the meaning of R from my resources to the library.

Related

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)

When would I use android.R over my local R.?

If I want to set content view then I use:
setContentView(R.layout.test);
But when I set a theme, why do I have to add android to the beginning of R.style.x?
setTheme(android.R.style.Theme_Light);
setContentView(R.layout.test);
The android API define a huge amount of standard resources like theme, drawable, attributes, ...
When you need to refer to one of those resources : you must use android.R
android.R is a reference to the Android R.java file. It is not custom to your app. It contains a reference to every style, resource, anything that you would ever use in an Android app.
On the other hand, R.x (without the android prefix) refers to your local app. It contains specific styles, resources, etc. that have been defined for your app, and is much more specific.
So as to your question of which to use:
Use android.R if you want to reference a standard style. This would be something like android.R.style.holo. It could also be animations, but keep in mind these are not custom, these are the building blocks.
Use R.x when you are declaring something local. This will be something that you have definded, such as R.style.MyTheme or R.layout.MyLayout.
You are not forced to use android.R.style.Theme_Light you can create your theme and use R.style.myTheme.
You use android.R.* when you want to use a layout/image/attribute/etc. already built inside Android without the need to rewrite it again.
You can use android.R.* like R.x everywhere you want (they are the same thing, the difference that R.x is build with your application while android.R is already here). You can remake things, but why do more work when Google already did it for you?

Annoying default formatting

I am working on a simple android application in eclipse IDE and I got a little yellow icon on the left hand side of a line of xml code that looks like a light bulb with an exclamation mark beside it. When I hovered over, it says "[I18N] Hardcoded string "input..., should use #string resource input". The running and debug was successful but I just want to get rid of it as I find it annoying. What should I do?
If it's annoying, there is a reason. You totally should use #string resources instead of your hardcoded strings. All you have to do is to put your string in res/values/strings.xml and reference it in your layout via #string/my_string_id_here.
This is extremely useful for multi language support, or for plurals strings.
You can learn more here.
Hope this will help you.
The right way:
Move all your strings into resource files, as suggested, and reference them in your views like so: #string/mystringname
The "other" way:
Turn off Lint warnings in Eclipse in Window/Preferences/Android/Lint Error Checking
Both ways will remove that annoying triangle :)
This warning is there because hardcoding strings into the android app's Java source code is not recommended. It will compile fine - but Android Lint will complain about it, so that's why it's a "warning" and not an "error". Generally, it is preferable to define them in the separate "string.xml" file.
If you want to know why, check this answer.
For an example, check this answer.
You should also take a look at the official documentation for string resources.

Getting Resources from SherlockFragment

I am trying to get a color from colors.xml, using something along the lines of
view.setBackgroundColor(getResources().getColor(R.color.bgcolor_view));
Although there is a R.color.bgcolor_view in my colors.xml file, the R.color part only shows colors that are available in the built-in android settings. So I'm assuming that getResources() is not getting the resources from my project.
So, my question is, how do I obtain resources from within a SherlockFragment?
Look at your imports, you probably imported android.R, so remove this import and double check that you import your own R from your own project.

How to rename widget ID's in Eclipse?

I am working on an app built upon an example from a tutorial. Now the different widget IDs no longer reflect their purpose so I would like to rename them. However, this seems quite a task as the IDs are used in multiple files.
Is it possible somehow to rename the IDs so the changes are migrated into the other files in the project? That is pretty much similar to refactor source code names, but for widget IDs.
I don't think a tool like that exists in Eclipse. The easiest way to do it manually is to rename an item in the XML layout and then track down the errors in the Java classes. If you do it one-by-one then you should have it cleaned up in a minute or two.
You can try to use the Find/Replace function is Eclipse. I have found this useful several times when changing ID's or something to that effect. Let us know what you end up doing.
In eclipse:
Go to the xml layout -> Graphical Layout -> Properties then click the ... button near the desired field:
In case anyone stumbles across this problem now, you can rename the ID from the visual layout editor and it will do all the hard work automatically.

Categories

Resources