What does R.id.myView refer to? - android

Could you please tell me what three single components of this name refer to?
R.id.myView

R - R.java is an auto-generated file by aapt (Android Asset Packaging Tool) which contains resource IDs for all the resources of res directory.
public final class R
extends Object
.id - Find view using its id "defined by you"
public static final class R.id
extends Object
myView - It is the view that you defined using the android:id="#+id/your_view" attribute in your XML file.
So, finally we can find or identify any view using R.id.your_view.

android.R -R is a final public class in android. It extends Object class and it has many nested classes like R.id, R.anim, etc.
R.java is Automatically System generated file and contains the id of each resources used in the Application which is used to make reference. R.class contains IDs for all your android resources.
android.R.id is a nested class of R class. It has many static final constants like text1, toggle, button, etc.
android.R.id.myView is an identifier of a View class. It represents an id for corresponding view defined in XML.

Android R.java is an auto-generated file by aapt (Android Asset Packaging Tool) that containsresource IDs for all the resources of res/ directory.
Whenever you use any recourse in you project then its one Unique Id will be generated automatically and you can identify that resource by using that id. You can not delete this file.
R :- Java class is collection of your all resources with its related id.
id :- Whenever you create any resource and assign the id by using #+id then R file create one unique id for that resource.
myView :- Its view id name that user can identify and By using that view id name we can identify that view in activity java file.
Below are the example of view id's in R.java file. If you want to show that where it is located then check this answer.
Example of R.Java file
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int ic_launcher=0x7f020000;
}
public static final class id {
public static final int menu_settings=0x7f070000;
}
public static final class layout {
public static final int activity_main=0x7f030000;
}
public static final class menu {
public static final int activity_main=0x7f060000;
}
public static final class string {
public static final int app_name=0x7f040000;
public static final int hello_world=0x7f040001;
public static final int menu_settings=0x7f040002;
}
public static final class style {
public static final int AppBaseTheme=0x7f050000;
public static final int AppTheme=0x7f050001;
}
}

Related

Cannot resolve symbol raw when the folder is empty

Why is it giving me "cannot resolve symbol raw" when i try to access the folder with the following code Field[]fields=R.raw.class.getFields();
The same code works when i add any file to the raw folder. The folder is in "res" directory.
Note: I have tried using the complete package name also.
When you create an Android resource folder such as layout, drawable, mipmap, etc.. The android will create a file named R.java. You can find it in
app\build\generated\source\r\degug\your\app\package\name\R.java.
It basically a normal java file which has been generated by android automatically for referring resources by ids in our code.
R.java
public final class R {
public static final class layout{
public static final int main_activity=0x7f0a0000;
public static final int login_activity=0x7f0a0001;
}
public static final class drawable{
public static final int ic_add=0x7f0a0000;
public static final int ic_edit=0x7f0a0001;
}
public static final class mimap{
public static final int ic_launcher=0x7f0b0000;
}
}
From Android official site:
Resources are the additional files and static content that your code
uses, such as bitmaps, layout definitions, user interface strings,
animation instructions, and more.
It means res folder is read-only and you cannot modify (add, update, delete) the content side.
Back to your problem, when you create raw folder but do not put any files inside and android know that in the runtime you cannot modify this folder as well, so why we need to create a refer to the raw resources anymore. It does not make sense. Because there is no raw class in R.java so your compiler display the error.
cannot resolve symbol raw
But if you create a file and put into raw folder, then after build process, a class raw will be created in R.java file.
public final class R {
public static final class layout{
public static final int main_activity=0x7f0a0000;
public static final int login_activity=0x7f0a0001;
}
public static final class drawable{
public static final int ic_add=0x7f0a0000;
public static final int ic_edit=0x7f0a0001;
}
public static final class mimap{
public static final int ic_launcher=0x7f0b0000;
}
public static final class raw {
public static final int sound=0x7f0b0000;
}
}
And now the error gone.
I think the problem is you are using wrong package of R.
Make sure R should be from your package name rather then some other android Native class.

Tons of items in R.java created by android studio

It's my first use of Android studio. But when I use it to create my first project, I found that in R.java(The location is app/build/source/r/debug/com.example.myapplication)
The beginning is like this:
public final class R {
public static final class anim {
public static final int abc_fade_in = 0x7f040000;
public static final int abc_fade_out = 0x7f040001;
public static final int abc_slide_in_bottom = 0x7f040002;
public static final int abc_slide_in_top = 0x7f040003;
public static final int abc_slide_out_bottom = 0x7f040004;
public static final int abc_slide_out_top = 0x7f040005;
}
public static final class attr {
public static final int actionBarDivider = 0x7f01005a;
public static final int actionBarItemBackground = 0x7f01005b;
public static final int actionBarPopupTheme = 0x7f010054;
public static final int actionBarSize = 0x7f010059;
public static final int actionBarSplitStyle = 0x7f010056;
public static final int actionBarStyle = 0x7f010055;
public static final int actionBarTabBarStyle = 0x7f010050;
public static final int actionBarTabStyle = 0x7f01004f;
public static final int actionBarTabTextStyle = 0x7f010051;
public static final int actionBarTheme = 0x7f010057;
public static final int actionBarWidgetTheme = 0x7f010058;
public static final int actionButtonStyle = 0x7f010072;
public static final int actionDropDownStyle = 0x7f01006d;
public static final int actionLayout = 0x7f01002c;
public static final int actionMenuTextAppearance = 0x7f01005c;
public static final int actionMenuTextColor = 0x7f01005d;
public static final int actionModeBackground = 0x7f010060;
public static final int actionModeCloseButtonStyle = 0x7f01005f;
public static final int actionModeCloseDrawable = 0x7f010062;
It list so many items that if I put them to Word, they will fill 38 pages in 11 font size. I tried deleted them, rebuilt them, restart, create another project, but they still exist. I had used Eclipse so I know how it should looks like. What's the problem? Is it caused by Gradle or Android Studio itself?
Deleting, rebuilding, restarting are not the correct approaches you should take.
These lines are generated by the Android Support Library.
Including the Support Libraries in your Android project is considered a best practice for application developers, depending on
the range of platform versions your app is targeting and the APIs that
it uses. Using the features the libraries provide can help you improve
the look of your application, increase performance and broaden the
reach of your application to more users. If you use the Android code
template tools, you will notice that all the Android application
templates include one or more of the Support Libraries by default.
[1] http://developer.android.com/tools/support-library/index.html
Yet, I don't understand why you want to take them off.
R.java is an auto-genrated file by android AAPT(Android Asset Packaging Tool). It has some significance, for every resource in the res folder a corresponding static integer field is created automatically.
So irrespective of the number of times you delete it, it will be generated again.
If you want to reduce the number of lines in the R.java file try deleting some values in the res folder.
The more layout files, widgets and images and other resources you have, the length of the R.java file is going to increase.
I hope this answers your doubts.

Changing id of string in Android / Eclipse

I start out with the example Android Drawer Fragment App.
I go to strings.xml and rename the id of the string "action_example" to "action_additem".
I go to the classes and change all mentions of this string to the new id.
I press Ctrl+Shift+S to save all files.
Still, I get this error in the fragment:
if (item.getItemId() == R.id.action_additem)...
action_additem cannot be resolved or is not a field
In R.java, I see the string ID was not updated.
What do I need to do to update it?
In the R file, I see two entries for this string:
public static final class id {
public static final int action_example=0x7f080005;
...
}
public static final class string {
public static final int action_additem=0x7f050006;
...
}
Why was only one of them updated and how do I update it to the correct id?
EDIT:
Okay - this is solved. One id was for the action, one for the string.
In Eclipse click Project > Clean...
This should rebuild the R.java file.

Error in R.java class

So I was happily coding away, and suddenly an error has appeared in my R.java class.
It's created a string, and says there is a "syntax error, and that I should delete the token 'sword'". I tried fixing it, but have instead created a duplicate string, "Long".
Here's the code:
public static final class string {
public static final int Long=0x7f050001;
public static final int Long Sword=0x7f050002;
public static final int app_name=0x7f050000;
public static final int desc=0x7f050003;
}
Does anyone know how to fix this error? Also, R.java doesn't like to be edited, so how to you save manual changes?
You named a field in your strings.xml called "Long Sword", change it to "Long_Sword", spaces aren't supported in the names.
Also, never ever touch the R.java file
public static final int Long Sword=0x7f050002;
Is not a valid field name.
Naming Conventions: http://docs.oracle.com/javase/tutorial/java/nutsandbolts/variables.html
You need to remove static from string class. A top level class cannot be static
public static final int Long Sword=0x7f050002; // not valid

Newbie confused by Android's findViewById() functions?

am very much a newbie when it comes to android development so bear with me. Im currently using Windows 8 / Eclipse.
My issue is, the findViewById function seems to have gone mad, although I could very well be using it wrongly.
The app was working fine, then I 'dropped' a few new TextViews on the page which seems to totally confuse it.
The findViewById function now either finds the wrong control completely, or doesnt find anything at all (and will return null). I've checked my activity_main.xml file and the id's are still correct.
Can anyone help?
This is a typical example of my usage:
public void toggleButtonNetwork_Click(View v) {
ToggleButton tb = (ToggleButton) this.findViewById(R.id.toggleButtonNetwork);//did work, now does not work!
}
The only insight I may add is that my R.java file looked like this when it was working:
...
public static final class id {
public static final int menu_settings=0x7f070004;
public static final int textViewGPS=0x7f070003;
public static final int textViewNetwork=0x7f070001;
public static final int toggleButtonGPS=0x7f070002;
public static final int toggleButtonNetwork=0x7f070000;
}
...
and now looks like this (broken):
public static final class id {
public static final int menu_settings=0x7f070006;
public static final int textView1=0x7f070004;
public static final int textView2=0x7f070005;
public static final int textViewGPS=0x7f070002;
public static final int textViewNetwork=0x7f070003;
public static final int toggleButtonGPS=0x7f070000;
public static final int toggleButtonNetwork=0x7f070001;
}
This is a quite common problem. Try and invoke the Project/Clean... on your project. It sometimes happens that the automatic generating of the R classes goes wrong and this will force them to be rebuilt.
Everthing is fine.
Always Just go to project -> clean
And than run .
Thats it.
This doesn't answer your question. I suspect that A--Cs comment is correct.
But, there is nothing to be confused about. findViewById is really simple.
When you compile your app, the compiler generates R.java and adds a line for each view in your layout XMLs (and strings, drawables, colours etc - anything that is a "resource") and gives it a unique ID.
public static final int toggleButtonNetwork=0x7f070001;
The ID will change as you change your resources, but it doesn't matter. When you use findViewById, you give it the "friendly name", in this case R.id.toggleButtonNetwork, which is compiled into 0x7f070001 since public static final int toggleButtonNetwork is a static constant.
When you inflate your view from XML, typically with setContentView, the view hierarchy of objects is built. The id of each object is the id found in R.java.
findViewById returns a reference to the object, of type View, which you then cast to whatever type of View it is which is why you use
= (Button)findViewById(R.id.toggleButtonNetwork);

Categories

Resources