How do activities know what R is? - android

In an android Activity based class, you do not have to import the R class explicitly when accessing your resources. However, in non-activity classes you do.
Can anyone explain why?

Yes you do...look at your import list. If you're using Eclipse, you may have to expand it to see it. R is a generated class file and has to be imported like any other class you wish to use.

Related

Android - Why do I need the full activity name to access my drawables and XML files?

I created a 2nd activity in my application. Now I need to add com.example.avenger every time I need to do something like val dialogImageView = view.findViewById<View>(R.id.dialog_imageview) as ImageView
If R itself is being recognized, but not specific resources of yours, usually that means that you have an import for somebody else's R class. For example, you might have an import of R from a library, because you are also referring to some resources from that library.
In the end, only one can be used via the R shorthand and the import. If you are using Java, pick whichever R you are more likely to use and import that one, then fully-qualify references to the other. If you are using Kotlin, you can rename imports, so you could have one as R and one as RMaterial (if, for example, the R you are colliding with is from the Material Components for Android library).

I use to be able to type r.id.etc now i have to type my package blah.blah.blah.r.id.etc

Before I could type R.id.exit now I have to type com.ramos.science.R.id.exit why?
Check your imports, be sure that com.ramos.science.R is listed there.
R will conflict with the R class from any other package. For example, if you needed to reference some android.R.id... then Eclipse may have automatically imported android.R so you could just reference it as R, but that would mean that any other R would need to be fully qualified as com.ramos.science.R.id....
My recommendation, and a good convention to adopt, is to use the fully qualified R for other packages and simply R for yours. So if you ever need an Android one, always do android.R..., and then just import your package so you can use simply R.
TLDR: check your imports for other R classes. If there's any that aren't your package, delete them and add yours. For other packages use the fully qualified name, e.g. android.R.id...
EDIT
The Android Lint tool even has a check for this:
Checks for 'import android.R' statements, which are usually accidental
Importing android.R is usually not intentional; it sometimes happens
when you use an IDE and ask it to automatically add imports at a time
when your project's R class it not present. Once the import is there
you might get a lot of "confusing" error messages because of course
the fields available on android.R are not the ones you'd expect from
just looking at your own R class.

Android multiple packages in one project, what is R and how to import it?

I have a launcher for people with bad eyesight and a simple music player, this is my second APP so bear in mind I'm a complete noob in android and eclipse.
I tried to merge both my launcher and music player. I added the activity to the manifiest with a different intent, copied layouts and drawables to the launcher project and I added the player's package inside my /src folder.
Afterwards on the first lines of com.easyplayer.java I got this error:
import com.easyplayer.R; // the import com.easyplayer.R cannot be resolved
This is the only bug I'm getting so I suppose I did everything else fine. I imagine R must reference the player's layout, but I'm not sure how to fix it (cleaning/rebuilding doesn't work). What is the R class? And what can I do to fix this?
The R class you see is auto-generated by Android. It is a utility class that contains references to all the resources in your project. There is a few answers detailing its contents here.
You mentioned you performed a clean of your project, but you need to do a full build as well to regenerate this file.
edit: The import of the new code may have somehow invalidated your xml files. Check to see if there are any errors there, which could be preventing the R file from being re-created during a build.
if you have several packages like, com.example.app.package1 and com.example.app.package2, then import the R.java file like,
import com.example.app.R;
And if you already have import android.R; then please delete that and save the file and clean the project, problem will be solve...:)

Calling my R, instead of android.R

How can I access the R class from classes that were not inherited from Activity. So when I write R.something, I implicitly call android.R.something.
What is the explicit way of call R.something?
Just import the version of R that you want. If you need to access values in both classes, at least one of them will have to be fully qualified.
Note that inheriting from Activity has nothing to do with using you project's R values without an import statement. It's whether the code is in the application package as declared in the manifest.
If there are errors in your resources, you wont get a R in your gen directory. Check the console log output for extra information. Anything colored red tends to be really important :) other than that, qualify your package directly as already mentioned

Android: GridView ImageAdapter can't refer to R.drawable icons - They are missing

I've run the HelloWorld without problem, but the GridView will not work.
The Sample_0, Sample_1, etc icons are not visible in the R class within the ImageAdapter file.
They ARE visible within the main class.
Going back to the HelloWorld example, I noticed there is an 'icon' in each drawable for three resolutions.
So as a test, I created a new class to access it, ( and yes I did import android.R)
Sadly, the R class does not refer to icon.png.
The only place where R.drawable.icon exists is in the main class.
Why does my R class (which is correctly generated with a reference to icon.png) not seem to be the same in a new java class file?
Help!
Jurimo
You're importing the wrong R. You're almost certainly importing android.R. You can't do that. You don't need to import R since it's within your package.

Categories

Resources