It's right there in values/strings.xml in Android's source - why isn't it in android.R?
edit: I am referring to Android's built in string resources (android.R.string.…) and not my own project's resources - so, for example, while I can use android.R.string.cancel, I can't use android.R.string.share despite the fact that the two of them are declared in the same file in Android's source code
They are just resources under the resource folder.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="share">hello</string>
</resources>
The above resource has a entry in R.java file which in under your package name. You can open R.java and have a look at it.
public static final int share=0x7f040000;
You can refer to the string in Strings.xml as
R.String.share
The R.strin.share is an int.
You can also get the string as
String s= getResources().getString(R.string.share);
Edit:
http://developer.android.com/guide/topics/resources/accessing-resources.html#PlatformResources.
Check the topic in the above link under the heading Accessing Platform Resources
Android contains a number of standard resources, such as styles, themes, and layouts. To access these resource, qualify your resource reference with the android package name.
For list of all resources check the link
http://developer.android.com/reference/android/R.html
The list from http://developer.android.com/reference/android/R.string.html
You are compiling for which SDK version??
Please check strings.xml of same SDK version..I think there is some mismatch in target SDK version and strings.xml(you are referring to, where "share" is present)
However i will recommend to add your own "share" string in application strings.xml, to avoid unwanted issues due to Android Fragmented nature.
android.R is the system's R
Your R is just R within your own package name. So just call it R.string.share instead of android.R.string.share when referring to it.
It's because it is not defined in android.R.string, but in com.android.internal.R$string. you cannot access it without some 'hack'.
public static int getReflactField(String className,String fieldName){
int result = 0;
try {
Class<?> clz = Class.forName(className);
Field field = clz.getField(fieldName);
field.setAccessible(true);
result = field.getInt(null);
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
...
int share_id=getReflactField("com.android.internal.R$string", "share");
if(share_id!=0)
Toast.makeText(getContext(), ""+getResources().getString(share_id),0).show();
this works both on my android 4.4 and 8.0 device
Or simply use
getResources().getString(Resources.getSystem().getIdentifier("share","string", "android"))
Related
I have huge number of sound bytes which I want to use in my project. Unfortunately all files are named numerically like "001.m3 , 002.mp3 ...."
When I added files in raw folder, Android R file gives error.
How can I solve this problem. Can any one provide me link where android has mentioned naming conventions for resource files.
Every resources having entry in R.java file, If you see R.java file is nothing but like our normal class
public final class R {
public static final class raw {
public static final int 001=0x7f090005; // this will not accept as a variable name
}
public static final class drawable {
}
public static final class id {
public static final int main=0x7f090001;
}
}
You should follow the same Naming Convention like we do have for Variables i.e.
1) Must not start with number
2) Must not contain Special character except(_)
3) Must not used Reserved Keyword mentioned here
Solution: You have to rename you files, thats it.
The problem that you are facing is because of your file names, since your file name is 001.mp3 or 002.mp3. Android creates R.java file automatically, and in that file(R.java) it will create a variable by that file name which is variable name "001". Having a numeric variable name is wrong . It will not allow such thing and instead throw an error.
If your file is 001.mp3 then R.java will have error in this line which is
Syntax error on token "001", invalid VariableDeclaratorId
public static final int 001=0x7f050000;
I request you to change your file names. May be follow the recommendations Are there conventions on how to name resources?
Hi i am working with Gestures and i need to import but i m getting error
com.android.internal.R;
The import com.android.internal.R cannot be resolved
kindly help me , please
You don't say why you need access to com.android.internal.R, but the sad fact is that you simply cannot import it (the "internal" is a clue that it's not part of the public API). Google doesn't expose this because it is subject to change.
It is possible to get to the internal resources by calling Resources.getSystem(). To get the value of a particular resource identifier, you have to know its name and then use code like the following to find the value:
Resources res = Resources.getSystem();
int id = res.getIdentifier("resource name", "resource type", "android");
Be aware that any name that you use could disappear in future versions of Android.
I have a couple suggestions:
1) Make sure you don't have any other errors other than the R-related errors. Right-click your project folder in Eclipse, Android Tools -> Fix Project Properties.
2) Check to make sure you have the correct R imported. Sometimes the default Android.R can be imported.
Yes you can use the internal R with some dirty trick (dirty trick = Java reflection).
Just an example:
Class clasz = Class.forName("com.android.internal.R$styleable")
Field field = clasz.getDeclaredField("TextAppearance");
field.setAccessible(true);
int[] textAppearanceStyleArr = (int[])field.get(null);
field = clasz.getDeclaredField("TextAppearance_textSize");
field.setAccessible(true);
int textSizeStyle = (Integer)field.get(null);
First of all, what is Gestures ?
Do you have a package named com.android.internal in your gen folder ? Doest it contain R.java ?
If not, try Project->Clean in Eclipse. If it still doesn't work, you may have an error in your XML layout files.
Over the course of programming I get errors that give a resource number like "0x7f040000" (or sometimes in decimal form) My question is simple: Is there an easy way to tell what resource that is in eclipse?
i know i could manually identify every resource and print them out, but then every time i make a change to the program i'd have to update this code. Is there some way to search by resource ID?
YOu look up the R class in the gen folder
for example
public final class R {
public static final class attr {
}
public static final class drawable {
public static final int block=0x7f020000;
block is part or the drawables in the R file.
if you want to do this at runtime (and not just in the IDE of eclispe you can try
getResources().getResourceName(id);
you can use getResources() for that:
getResources().getResourceName( theID )
In your project structure, you should have a directory called gen. Browse that directory until you find the class R. There you find every resource of your project defined.
Another possibility is to go directly to the R class: Press ctrl + shift + T and in the popup you just type R. But be careful: There are at least two R classes listed there. One is from Android itself (package android.R) and your with the package name you have chosen.
Look at your R class and see what variable corresponds to that resource id. Right click -> Go To Definition.
I am trying to separate an application into an app project and a library project (besides moving it from Netbeans to Eclipse). The app will contain resources that are used by the library - for this, I had read on Stackoverflow that we can bundle the resources in the library project and then override them in the app project.
But when I did this, I am getting the error:
...\res\values\attrs.xml:5: error: Attribute "pageBackground" has already been defined
Am I doing something wrong here? Any of my assumptions is faulty?
Thanks,
Rajath
I think I had similar problem when I tried to create a kind of 'configuration file' which was placed in application's resources and was meant to alter behavior of library it used. What I found working was using getIdentifier method from Resources instead of refering directly to R class:
final int resId = getResources().getIdentifier("my_resource", "raw", getPackageName());
You can then use the identifier as normal resource ID, e.g.:
if (resId > 0) {
final InputStream is = getResources().openRawResource(resId);
// ...
}
The idea was to handle both situations: when the file was present in app's resources or when it was not. But I think it should also work in your case of "overriding" the resources from library in application, thanks to getPackageName providing appropriate package name for resources' identifiers' resolving.
Is there a way that i can see the resources from my app within my library?
I tried
Class res = Class.forName( extras.getString( R.string.getClass() ) ); <- apps' R class
final Field[] fields = res.getFields(); <- returns nothing
for(final Field field:fields){
....
}
Thanks
Update
The app could see the whole list by just getting R of the library, but now i cant get string value from the app
Is it possible that you really want to be using a raw resource, rather than the strings resource? If you want random access to strings, reading a file into memory would make things much easier.
The nature of the strings resource is more of a compile-time linking, not a database to search at run-time.