can't get R.anim in my project - android

I started some android programming and I am trying to do some animation in my activity. but all references are using interpolators and files from res/anim folder which I can't find in my project. Also, when I am using R.anim I am getting an error that it doesn't exist.
Is there any API to download?
Thank you

You need to import the R file. You should see something like below in the import list of your class file.
import <your.app.package>.R

I had the same problem, and none of the solutions above worked.
In the end, I got it to work by referencing it as android.R.anim as explained here.

Had similiar issue, but could access all other R.* locations but not anim.
Problem was with an anim xml file missing:
xmlns:android="http://schemas.android.com/apk/res/android"
Nothing in IDE detected any error. Hope this helps someone else.

No, R file is not some API. It is a file generated by the Android build system from existing resources. If you can't find some particular references to resources, this means these resources don't exist. You should put them where they belong. Read more about providing resources for your applications.

No api. Your config is wrong in some way. Attach some code, possibly a screenshot of your project browser so we can see the folder/file structure.

In my case there was yet another reason why my R.anim.shake hadn't been recognized. It was because my project is divided into 2 modules (app and library). I placed the animation file (shake.xml) in the app module while I was trying to reference it from the library module. Moving the shake.xml file to library/src/main/res/anim solved the problem.

Related

Accessing strings.xml values in android library project

I have MyAppProject as android application project and MyLibProject as android library project.
I have strings.xml(2 strings) and one xyz.java class in MyLibProject.
xyz.java class has 2 get methods to access the strings.xml values.
I have added reference library project "MyLibProject" in "MyAppProject" application project.
For some usecase, I will check whether Library project xyz.java class is in classpath then get the
values from xyz.class.
How to access the strings.xml texts in xyz.java class get method?
As far as I'm aware, you should be able to access any references via a fully qualified name for the R file you're attempting to access, via the Context. Something like (untested):
Context.getString(com.YOUR.LIB.R.string.YOURSTRING));
Failing that, you could possibly import the com.YOUR.LIB.R file directly into your activity.
Hope this helps! And I hope that I understand what you're trying to achieve. If not, please provide more information and possibly some code you've already tried, may help explain your problem :)
This question has been very old, but creating Android libraries will never get old. I have been working on creating Android libraries for a few days now, and myself encountered this issue of not been able to use res directory resources in library's java files due to some reason. After 2 days of searching, I found that I was missing package declaration in my library's manifest file.
So, to make it working, check if you have declared package in manifest.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.yourpackage">
</manifest>
It worked for me after this change, now I can use resources in library's java files. I hope it works for you too.

Import R with downloaded source code

I've downloaded the ContactsList example from google's developer site. When creating a new project with this code, I've got several errors, and I realized that all are because the same thing: It doesn't get the R resources, so it is importing the android.R.
I've tryed celaning the project, trying to solve layout's issues if the had, etc.. but still doesn't create the R.java file in "gen".
So, I don't know what I'm forgoting to do because I tought that code downloaded directly from google should work fine.
Check for the following:
Check whether there is error in your xml file.
Check whether you are missing any jar(additional) file.
Try to change your target sdk to some other and change to original.

Newly created layout files not added to R.java

I have tried literally everything to figure out why no new layout files are being added to my R.java file in eclipse. Nothing is working.
I have:
Restarted Eclipse
Used Project>Clean
Deleted R.java (had to restore because it would not autoregenerate no matter what I did)
Ensured all methods are defined in SDK version
Rebuilt the project
Unchecked auto-build
Every combination of above options
Been tempted to throw my computer across the room
Does anyone have any suggestions?
Check you don't have any errors inside one of your xml files. That is, check not only for xml file name errors but also for errors in the xml code, for example an illegal attribute, a forgotten tag etc. Check this in all your xml files, not just layout files but also anim, xml drawables etc. These errors don't show up in the error log but will prevent the R.java from being generated.
When I experienced the same issue as you this was often the reason. The error highlight was only visible inside the file, not the error log.
Net, go through every xml file with a fine comb.
I faced this kind of problem many times. From my thoughts it is caused due to the problem of images in the drawable or case sensitive of layout file (everything should be small). Check your case sensitive of layout file.
I will give all possible solutions that i had done when something like this happened with me
Project-->clean
Check for library if there was any for that
RightClick-->Properties-->Android-->add
Restart Eclipse
Check in
the manifest for if you have many packages make sure u are using the
activity declared package.
I would check the imported R. The problem could be that you have
import android.R;
instead of
import com.myproject.R;
Project -> and check Build Automatically
Is there any error in your resource files? If there is any error in your resource files the R file will not be generated.
When first of u go on Project->Clean the your application then refreshing and auto genrated R.java file for your apps.
Not to be repetitive, but did you check the Android manifest file? The error there doesn't usually pop up.
Did you check for other locale letters (maybe whitespaces too?) in the filenames? Letters that look exactly like Latin.
I think I had similar problem with the names. You can try rewriting your files' names.
Project Explorer (Right click on your project) -> Build Project
This worked for me.

How to reference R.java of other apps in Android, like com.android.music?

I am trying to develop an app to supplement the built-in music player. I've used git to download the com.android.music package and looked around at its code. I can launch the music player by copying some of its code and launching activities with intents.
Now what I need to do is get a handle to its current view. In the MusicUtils.java file, I see a line that says
View nowPlayingView = a.findViewById(R.id.nowplaying);
I'd like to do the same thing. Only I don't have access to the R.java file, so I can't write e.g. R.id.nowplaying. How do I do this? How do I reference the music players R.java? I do know the R.java stuff is declared public so that shouldn't be a problem. Right?
Is it even possible? I saw this related question and am now wondering: How to load com.android.music code into Eclipse and compile?
Btw, I'm working against the Android 2.2 SDK, but it'd be helpful to know if the answer is different for older versions such as 1.5. Thanks.
You cannot get a handle on a View that is not in your process, even if you have access to the R class.
The R class is code generated by the Android plateform from the XML's resources files found in the /res directory of the project. It means you want to use the git repo where you got the MusicUtils.java and go to the root dir of the related project to look out in /res/layout/. You then have to find an XML layout file declaring the View having the id "nowplaying".
Copy this file into your own project 'res/layout' directory, and Android will compile a R class for you to access the R.id.nowplaying you miss so much.
You have the source code, so take the resources and build your own R file. Also, you can access android system resources by android.R.<type>.<name> as well.

How to distribute the android reusable code in a package?

I have developed some reusable android component which is basically a class . This class has some resource dependencies e.g. some png drawables, some xml layouts etc. So this class referenced the auto-generated R file.I would like to distribute this code in a single package like jar file to other developers for use in their applications.
I have read that the only possible solution is to distribute code together with all my resources, which others have to copy to their "res" folder (source).
So I created a jar file having the class file (say MyClass which is in the package com.xyz.android.app) and resources and tried to use this in my new application.
So I added the jar file to my new applications build path using add external jars option in eclipse and copied all the resources to my new application's res folder. (The activity class say MainActivity of my new application is in com.abc.myapplication package, just for the case if it may helpful)
But when I run this new application there is java.lang.ClassCastException in the MyClass class. I tried to debug the application and then I found that in the MyClass class, there is "R cannot be resolved" problem.
Then I changed MainActivity's package to com.xyz.android.app (which is not the way, other developers will be happy to do), But again the same problem.
But When I just copy the source java file such that both MainActivity.java and MyClass.java are in com.xyz.android.app package then application runs fine.
So if I need to distribute such that other users need not to bother these package naming things,
how can I accomplish this? Please help !!
Edit
In android.jar, there are also some resources. How are they referenced in a project? How are they used in android classes? There is also android.R file?
Is it not possible to do the same thing i.e. to make jar file like android.jar for my reusable code?
As Nic Strong noted, there is nothing much built into the project to manage this, and current indications are that the upcoming changes to the SDK tools may only help a bit.
I am organizing some other tools to help deal with this problem. I hope to have developer documentation published in a few days.
This is not so easy to do at the moment. The problem is the project using your jar does not know to look in there for drawables etc.
The good news is it should soon be possible (hopefully with SDK 2.2 which is rumoured to be released at IO next week). See this blog post http://mobilebytes.wordpress.com/2010/04/27/android-tools-version6-coming/
I've been playing with Mark Murphy's ParcelHelper class at http://github.com/commonsguy/cwac-parcel. In a nutshell, it's a collection of methods that let you access the components of 'R' by name without needing to import 'R' into your code.
If I understand your question right, this is exactly what you want. You may still need to copy some resources, such as styleables, into your project.

Categories

Resources