I put an image in my res/drawable folder, but when I clean and build my project, I receive the error message R cannot be resolved. I don't understand why this happens, what should I do?
Check out the following link:
http://source.android.com/source/using-eclipse.html
Note: Eclipse sometimes likes to add an "import android.R" statement at the top of your files that use resources, especially when you ask eclipse to sort or otherwise manage imports. This will cause your make to break. Look out for these erroneous import statements and delete them.
After removing the erroneous imports, delete your R.java file and try to clean and build the project again. If the problem still persists, post here.
possible causes:
duplicate file names (across all drawables folders) . for example , is there a file named "h1.png" and also "h1.jpg" ?
bad files names . only letters allowed are lowercase english ones , and digits.
hidden files , like those of the OS . on windows , it's probably "Thumb.db" . either delete them , or delete & disable them. linux and mac also have such files , but with different names and usually start with "." .
Make sure your image name is in small latters and in res/drawable folder.for how we add drawables in our project and supported images formats see docs Drawable Resources
Might be you are using android.R.drawable.your_image
Remove android.R from imports. use R.drawable.your_image
For Android Studio:
Right click on res, new Image Asset
On Asset type choose Action Bar and Tab Icons
Choose the image path
Give your image a name in Resource name
Next->Finish
The image will be saved in the /res/drawable folder!
Related
In my project I want to add a menu bar. I have downloaded a menu icon now I want to add it under .Android -> Resources. Unfortunately I can't add anything (a prohibition sign appears as a mouse icon). I also tried to add only a png or another file which also does not work.
My project
The downloaded icons:
Does anyone know how I can add the files in the appropriate place?
Don't know if this is the case, but some times the projects lacks the actual 'Drawable' folder. Create the folder yourself from fileexlorer
I added a vector asset to a wear os project using and cannot access it using:
ContextCompat.getDrawable(mContext,R.drawable.ic_settings);
It does not seem to be finding the asset. I have the Drawable directory and see the assets in it. I opened up the xml file and there is an error:
"The drawable 'ic_settings' in drawable has no declaration in the base drawable folder or in a drawable-densitydpi folder; this can lead to crashes when the drawable is queried in a configuration that does not match this qualifier"
I have no idea what this means. I just added it using the New -> Vector Asset menu. Does anybody know how to solve this issue?
Here is the line in the code:
Here is where it is in android studio:
When I look at the file:
Furthermore I have a roundbutton.xml in the drawable directory which is referred by R.drawable in another pice of code, and which there are no errors.
UPDATE
After doing all the checks (is the asset in the drawable directory), I closed and restarted Android Studio and it recognized them. Ye ol' reboot. Thanks to all for the responses. I am going to select the one which helped the most as the answer.
The problem is that your asset is specific to a certain configuration, so if the configuration doesn't match with device settings then the drawable can't be found by resource and will lead to a crash.
Some variants are:
- drawable (base, if not found in any folder)
- drawable-ldpi (low-resolution devices will look into it)
- drawable-mdpi (medium-resolution devices will look into it)
- drawable-v24 (devices with API level v24 or v24+ will look into it)
For more info, you can see: https://developer.android.com/training/multiscreen/screendensities
To Fix it:
Goto Android window and expand app > res > drawable, find the drawable, and right-click on it. From the menu goto Refactor > Move File...
From the text edit box remove the substring after the word "drawable" (in the image you can see "-xxxhdpi" after the drawable word which we should remove) and press Refactor.
Now, you're good to go.
Which helped me in a similar case with another file.
I redid a bunch of work to fix this error. Deleted the entire contents of the file in the IDE editor. Retyped and rechecked the contents of the entire project probably 10 times. Nothing helped. It seemed mystical.
The problem was solved like this:
I removed the ic_launcher_background.xml file. Not safe.
And created a new one. Then I typed the whole text again.
And everything became good.
Perhaps some hidden control character in the file interfered. I can't repeat the situation again.
For me creating a new file in drawable directory (instead of copying it there) helped. I just copied and pasted the content of that file to the new which I created.
Saw the same message recently, but the cause was a hyphen in the file name. I had copied the file keyboard_alt-48px.xml directly into the drawable directory:
Renaming to remove the hyphen resolved the problem.
For reference, trying to rename with the hyphen in the file name again was then highlighted as an invalid resource name:
This is my project folder, when i adding first image then it not show any error but after adding the 2nd and 3rd image it show the red lines.
I don't know where i do mistake, if anyone knows then please help me.
In Android Studio, click on View > Tool Windows > Resource Manager in the menus or click on the Resource Manager tab to the left of the Project window.
Click the + below Resource Manager, and select Import Drawables. This opens a file browser.
Make sure the name of the image you are trying to add does not resemble some keyword. I was trying to add an image in drawable with name "try". When i made it "tried" error went away. So, name your image != any keyword.
FOR YOUR CASE IN PARTICULAR:
Try invalidating cache and restarting it. Moreover, also check if the problem persists only with these two images or others too.
Names should be in lowercase.
Therefore never use names with uppercase letters in it
try removing numerals and keep a meaningful name. Also ensure that the names are in lower case
drawable folder is wrong place for png files.
In drawable are stored xml files
All png files should be stored in mipmap folders
If you want have only one png for all resolutions, store it in mipmap-anydpi
I found the problem.
Add the same image to the default drawable folder. Android Studio automatically doesn't add it to the default drawable folder.
It usually happen because of the name assigned to image file. try to put the name of file in snake_case.[error][solution]
solutionC.pngerror
When I build my Android app, I get this error:
Error:Error: Duplicate resources: E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png:drawable-hdpi-v4/login_bg, E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.9.png:drawable-hdpi-v4/login_bg
Error:Execution failed for task ':app:mergeDebugResources'.
> E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png: Error: Duplicate resources: E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png:drawable-hdpi-v4/login_bg, E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.9.png:drawable-hdpi-v4/login_bg
I am not able to properly understand the error. What file is duplicated here? What am I supposed to do to rectify it?
The reason you are seeing this error is because Android considers the following images to be the same, based on how they are referenced in your layouts:
E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.png
E:\Android\LED\app\src\main\res\drawable-hdpi\login_bg.9.png
The first image, login_bg.png, is a normal image. The second image, login_bg.9.png, is named in such a way to tell Android that it is a 9-patch image. However, in terms of referencing the images, they are declared the same, as in the following examples.
Normal image:
<ImageView
android:id="#+id/normalImage"
android:background="#drawable/login_bg"/>
Nine-patch image:
<ImageView
android:id="#+id/ninePatchImage"
android:background="#drawable/login_bg"/>
Note: There is no difference in terms of referencing the images from the /res/drawables directory of your Android project.
See here for more info about nine-patch image, or the correct term for it is nine-patch drawable. For reference, nine-patch drawables must be declared as <name>.9.png, as in login_bg.9.png.
Therefore, simply renaming them will not solve the issue. You need to check with whoever developed the UI to see which one should be used: either the normal image (login_bg.png) or the 9-patch image (login_bg.9.png)—not both.
This error message will occur whenever the build tools detect multiple files with same name, regardless of their extension type.
For example, a file named mypicture.jpg cannot be in the same folder as mypicture.png.
In your case, login_bg.9.png and login_bg.png cannot be in the same directory.
Therefore, what you need to do is change the name of (or delete) one of the files.
Delete the image from drawable directory, because you are using same name within one folder, which is not allowed.
After I changed the initial/default ic_launcher via Resource Manager>>Image Asset>>Icon Type>> Launcher Icons
I got this error.
It happens because the initial icon still exists and with the same name inside the mipmap folder.
the solution for me was to delete the initial launcher icon.
Solution: Change your view to Project, find the initial icon, and delete it, then the error should be gone.
Solved this by going to
Build ➞ Clean Project
Using Ant instead of Gradle seemed to fix the issue for me. Many of my files have the same filename to start with a different extension (indexed resource files of various types). Ancient project, and the ancient solution worked for me.
I find a same string name in my two diff .xml file,
//a.xml
<string name="dialog_subscribe">关注</string>
//b.xml
<string name="dialog_subscribe">subscribe</string>
then i solved it by rename one of then
I am trying to create a new folder inside the /res directory
I added a new directory
The folder does not appear in the /res folder even though it exists in the finder
Case and point I tried adding the folder again and I got a message telling me it exists.
If you can suggest what I can do to fix this I'd love it!
(And yes I have restarted the program as well as my computer)
Turns out there is a selector at the top which, by default, is set to show Android. If you flick the toggle down to 'Packages' you can see your own packages.
In Android Studio you can add new directories by right clicking the resource folder in the Android view. (Don't get confused by the New Directory option.)
For example, if you want a new layout resource file and folder then right click layout and choose New > Layout resource file.
If you want a new layout-sw600dp for the activity_main.xml file then choose Smallest Screen Width from the list and click the >> button. Fill in the file name and width and click ok.
Android Studio will automatically create the layout-sw600dp directory and in the Android view you will have both layout files conveniently displayed next to each other.
It is a similar process for adding other resource directories and files (see my other example).
Clicking at TAB Android in top, select the perspective Project Files. Then u can see all folders of project.
Android 4.0
The Android view by default seems to conceal the duplicated resource type directories in the side panel.
If you want to view the duplicate resource type directories i.e. drawable for two different languages,
switch the view from "Android" to "Packages" or "Project" or "Project Files"
Browse the actual folder structure in windows and you'll see the folder. Put the same resource file found in the normal drawable folder into this new directory. Then when you go back into Android Studio you'll now see 2 files under the NORMAL \res\drawable structure. Make sure you don't look for a subfolder in Android Studio called "drawable-sw600dp" because you won't find it other than in windows explorer. What you will find in Android Studio (after you copy the resource file) is a new resource file along with your original called "jenny-web.png (sw600dp)" or something similar. This same principle resolves the same error when working with other resources like layouts.
For Android Studio 3.0, those who are following https://developer.android.com/training/basics/supporting-devices/languages.html and have encountered the same issue as the one stated in this question, you can also do the following aside from the above answers which are helpful.
Right click to the target folder (i.e. res folder) > New
Choose Android Resource File
Specify the file name, and in the Directory name field put the folder name you want to create or the existing folder you want to put the resource file.
After hitting OK, in the Project files dropdown, you will see the correct strucutre.
And in the Android dropdown, you will see it as part of the strings.xml under values folder.
This worked best for me. What I did was I right-clicked the 'app' directory and chose 'New' >> 'Android Resource File'. After that I filled in the File Name with 'strings'. Chose 'XML' as my resource type and my Directory Name was 'values-b+za+ZA'.
This created everything as I wanted it to, however, values-b+za+ZA, still didn't show in the Android view but it shows in the Package view. (The views you choose from the top dropdown.)
The difference this time is that in the Package view I can still see my new 'strings.xml' file and in the Android view I van also see the new 'strings.xml' file.
I guess android studio doesn't show custom directories in the Android view but creates like some kind of link to the true directory when working with it. I am not familiar with Android Studio at all but this did work for me. If there are more professional answers please feel free to correct me.
You most probably have the file named in Uppercase, Android Studio does not recognize file names in Uppercase, I had made this mistake a while back.
As the developer doc states:
be sure the Project window is open (select View > Tool Windows > Project)
and the Android view is selected from the drop-down list at the top of that window.
You will then be able to see all relevant project files (including new ones) in a more efficient way.
Hope it helps someone.
This is odd but in my case i had to many underscores delimiters.
When i renamed yt_search_view (2 delimiters) to yt_searchview (1 delimiter), it appear in project view where it should be. It is probably a bug. My version is Android Studio 3.2.
Also, i notice that if i add a value folder to the layout folder, it also fix the issue.
Example:
layout_control_x
--layout
----top_panel.xml
--values <--adding this empty folder fixed the problem
For complete information on how to add sub folder layouts check this.