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
Related
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).
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.
I want to make a small change to the Android standard TimePicker class. Specifically, I'm trying to change it so it works in 15 minute increments, rather than 1 minute increments.
This post helped me constrain the range of minute values to {0, 15, 30, 45}, as required in my app. But as I pointed out in a follow up comment, the minute spinner still shows previous minute as current value - 1, and the next minute as current value + 1, which creates a sloppy-feeling user interface.
I looked into the relevant Android source code, and it appears that the changes I would need to make are pretty simple. But when I tried copying the source code into my project I got about a zillion errors relating to the package declaration, where to find Widget, how to resolve R.id variables, etc.
So my question is:
What's the best way to make a small change to a given class from Android source code, and incorporate it into your own project?
In my case, I just need to make a few small changes to TimePicker and NumberPicker, but I'm not sure how to properly set this up in my project.
Thanks for any suggestions.
But when I tried copying the source code into my project I got about a zillion errors relating to the package declaration
Your source file's directory needs to match the package name. And since you cannot overwrite android.widget.TimePicker, you will either need to move that class to a new package or give it a new name.
where to find Widget
That implies that you copied TimePicker into one of your packages. That is fine, but then you need to add in the appropriate import statements for classes that TimePicker referred to from its original package. Or, you need to keep your (renamed) TimePicker in android.widget, adding this package to your project. This is rudimentary Java.
how to resolve R.id variables
If TimePicker relies upon resources that are not part of the Android SDK, you will need to copy those resources from the AOSP into your project as well.
What's the best way to make a small change to a given class from Android source code, and incorporate it into your own project?
IMHO, that cannot be answered readily in the abstract. Generally speaking, you do the sorts of things that I listed above.
You are best off subclassing the relevant classes and overriding the methods you would like to change.
In Java, you can do the following in a subclass:
The inherited fields can be used directly, just like any other
fields.
You can declare a field in the subclass with the same name as
the one in the superclass, thus hiding it (not recommended).
You can
declare new fields in the subclass that are not in the superclass.
The inherited methods can be used directly as they are.
You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
You can declare new methods in the subclass that are not in the superclass.
You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword super.
More info on subclassing in Java
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.
I'm working through a book samples programs and i get the same problem whether I create and type the code or whether i download the code already typed, so i'm thinking it's a setup problem.
I get this error in each each class:
setContentView(R.layout.menu) where the "R" is underlined in red and the code won't compile. If i add import android.r like "QuickFix" suggests, the red line goes away from the "R", but then the red line appears under "menu" (in this example). The same behavior occurs in each of the 5 classes that make up the project.
I do have matching layouts for "menu" etc. And (to the best of my knowledge) I registered them as Application Nodes in the manifest file.
It's driving me nuts. Thanks for any suggestions.
You are not supposed to import android.R as it won't resolve your resources. Just remove that import and try to compile again.
If you are working off a tutorial then also check to have the same resources as in the tutorial and also if you decide to name thing different then keep that in mind an reference them by your names.
The R class is generated by Android and contains IDs for all resources in your res folder. Go to your AndroidManifest and locate the package= attribute on manifest. R is in this namespace. So if the namespace is com.yourpackage.blah, you'll want to import com.yourpackage.blah.R;
The problem was that for some reason Eclipse was not autogenerating the gen/R.java file. I don't know why. One time it did generate the file, and a after Project..Clean and Build All everything resolved. Thanks for the input.
Also make sure your file names under res, eg drawables, are OK. I had a file name with capital letter and that prevented the creation of R with the same reference errors that you got. I did not check the error messages and it took me a good 10-20 mns to find this out.
Sometimes this happens to me.
It usually happens that I have been working with some xml element, and before clicking to a java document to hit "run" i just hit "run" while on the xml. I don't really know what happens when you "run" an xml, but it makes this horible file called some_xml_name.out.xml.
DELETE THIS FILE!
This file is the reason why you get so many R related errors, since the Resources library is never really created, or something like that.
That is my advice, without actually seeing the error you're getting in the console.