Sub-Folder I didn't mean the drawable-hdpi, drawable-mdpi, drawable-ldpi, etc
but #drawable\myfolder\img.png
I'm a bit concerned on customizing the folder structure in drawable similar the way we have package to structure the java files.I'm using too many images for my project and when I try to copy activity layout xml; searching for the images in drawable, I felt it boring! Does android have such feature or some wayout?
I think, currently this way exists. I also have many drawables and layouts, so tried to start from layouts and then added drawables and menus. I recommend to see Can the Android Layout folder contain subfolders? and http://alexzh.com/tutorials/how-to-store-layouts-in-different-folders-in-android-project/.
Yes, I have now new resources in new subfolders. It requires time to manage (create res folders, edit build.gradle), but a folder tree becomes more neat. Sometimes AS cannot find resources during compilation. In this case I have to create new folders and edit build.gradle. Probably after several weeks everything will be done.
UPDATE
It has worked until Android Studio updated to 3.2 (and 3.2.1). Currently if you move any drawable, layout, menu resource to another folder (and add this folder to build.gradle as written in the articles above) you cannot normally use it. Before 3.2 we could simply press Build > Rebuild Project and reference to that resource. Now they have broken this behaviour and you should press File > Invalidate caches / Restart... > Just Restart (or close and open AS) to access this drawable as usual. If you don't want to restart AS, you can use the resource, but write a path to it manually like #drawable/reset_password, AS won't hint as you type and won't draw it in Design tab.
If you use Kotlin Android extensions and reference to ids like send_button (without findViewById()) you will get so many bugs that can't imagine. If you change resources, often nothing changes in layouts until you rebuild the project. This is because Kotlin caches resources. I often forget about it and waste hours.
As far as i know, NO.
There was an interesting post on G+ with a workaround for the layout dir, that works also for the drawables dir. I guess.
All the infos are here
Android doesn't support subfolders within its predefined directories, the only thing possible is for you to create directories within the res directory
Related
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:
I inherited the values folder structure shown below.
For over an hour I've Googled and looked and pulled down menus in Android Studio 1.2.2 and tried creating files and folders but nothing I have done lets me add, for instance, dimens.xml (sw720dp).
I deleted dimens.xml (sw720dp - landscape) because landscape didn't suit my needs.
What steps did whoever created the folder structure take in order to get dimens.xml (sw600dp) to appear in the structure?
What steps do I need to take to get a dimens.xml file with the sw<N>dp file naming convention? How do I get dimens.xml (sw720dp) where it belongs?
You can ignore the rest of this. I'm just showing that I've TRIED stuff, none of which works right.
Am I supposed to right-click values or dimens.xml (2)? What then?
Here's what I did and it didn't do as I need:
I also tried:
EDIT
I also tried copying the sw600dp file. No luck:
IT CAN'T BE THIS HARD.
What steps do I need to take to get a dimens.xml file with the swdp file naming convention?
First, you will probably be happier with life if you change the drop-down above your project tree from "Android" to "Project", which shows you an actual filesystem view of your project.
To add a resource directory from within Android Studio:
Right-click over res/
Choose New > "Android resource directory" from the context menu to bring up the New Resource Directory dialog:
Choose your resource type in the "Resource type" drop-down (in this case, values)
Click on your desired qualifier (in this case, "Smallest screen width") in the "Available qualifiers" list.
Click the >> button
In the field that appears, fill in your numeric value (in this case, 600)
Click OK to close the dialog and create the directory
To add dimens.xml to that directory, right-click over the newly-created directory and choose New > "Values resource file" from the context menu.
I have had MANY failures trying to do stuff OUTSIDE Android Studio, but this time I got away with it:
I will now see how #Commonsware says I could have done it.
EDIT
And as always, #Commonsware bailed me out with good advice (I have no idea when or why or how I lost the File structure "tab" along the left margin).
Here's what I now see, and (I was right:) it's NOT hard to see what to do ONCE YOU SEE THIS:
And, following his advice, I get to here and am ready to create the dimens.xml file that goes inside:
I want to support separate UIs for both portrait and landscape mode in my android studio project, for which I need to have a folder with name - 'layout-land', to create it I followed below steps:
Right Click on res folder and create a new directory
Give name - 'layout-land'
Problem is- the created folder is not appearing within res folder :(
I am clueless, please suggest.
That's the slightly confusing 'android' view of Android Studio. It shows only one layout folder (also shows only one drawable folder) which really contains all folders. Switch the view to 'project' and you'll see all folders.
I'm posting this just in case anyone really likes Android hirearchy of the project (note: other answers using classic hirearchy are correct as well).
All layout dirs with different qualifiers are categorized under name "layout". Just right click on the "layout" create your xml with any qualifier you need and you'll get this hirearchy (image below).
Notice activity_main.xml is not a directorty but a category collecting all activity_main.xml files with different qualifiers.
This categorizing pattern appears quite often in Android hirearchy.
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.
I have an application and putting all of the layouts inside of the res/layout folder is getting pretty large and hard to manage. I would like to have folders for all the layouts. I have read that there cannot be subdirectories inside the layout folder but that there could be in the assets folder. So my question is, how do I access a file and set it as my view from a file in the assets directory? Something like the following
int assetId = getAssets().open("main.xml");
setContentView(assetId)
Would the above code work? How would I set that xml file for my view?
Thanks.
You can achieve this by using a custom script and having it run before the build executes. Android seems to ignore anything in layout subdirectories, so you can safely put your files into them. The following ruby script (written for Linux, but easily convertible to other platforms) will then delete everything that's not a directory in res/layout/ and copy every file from the subdirs into res/layout/:
#!/usr/bin/ruby
require "fileutils"
def collect_files(directory)
FileUtils.cd(directory)
FileUtils.rm(Dir.entries(directory).reject{|x| File.directory?(x)}) #Remove all layout files in base dir
files_to_copy=Dir.glob("**/*").reject{|x| File.directory?(x)}
files_to_copy.each{|x| print "Copying #{x} to #{directory}\n"}
FileUtils.cp(files_to_copy, directory) #Copy all files in subdir into base dir
end
if ARGV[0]!=nil && File.directory?(ARGV[0])
xml_dir=ARGV[0]
layout_dir="#{xml_dir}/layout"
collect_files(layout_dir)
else
puts("Must specify a valid directory!")
end
Be warned that the above script is not robust, and will actually delete any layout files not in a subdirectory. You can always remove the deletion step if you like, but then any files you remove from the subdirectories will remain in the main directory for subsequent builds.
If you're running Eclipse, you can then configure an external tool, which you can add to your builders later. Just open up Run -> External Tools -> External Tools Configurations, and create a new tool under 'Programs'. Here a screenie of my settings:
Note: The working directory is a red herring, and won't be used. You'll need to specify the location where you drop the script, not the one shown here
Now you can add the tool to the builders for your project. Select your project and open up Project -> Properties. Now Select the 'Builders' item and click 'Import'. You should see your tool there if you defined it successfully. It needs to run before the rest of the build process, so make sure to move it up to the top of the list. Here's what it should look like when you're done:
Now you just move layout files into subdirectories (but watch out for name collisions, remember the files will all end up in the same directory for the build!) and build your project. You'll see them magically appear in the root of /res/layout/ when you do this and your app should then build normally.
Caveat Scriptor: If you're specifying multiple layouts, or anything else which uses more than just the /res/layout/ directory, you'll need to extend this script or add the tool multiple times for the different directories to handle it. I don't personally use this technique, and so haven't seen where it falls down, but have performed a test with a basic android Hello World app with a couple of layouts in some subdirectories.
Also, my script will break if used with paths containing spaces!
The short answer: it can't be done in that way.
The main reasons:
In the res/layout folder all .xml files are precompiled, so Android can use them as resources. In assets folder all files remain intact, so the app can read them as regular files. (In your code example you get InputStream, not resource ID).
Android automatically manages layouts located in the res/layout folder, searching the best matched to current screen resolution, orientation, locale, etc.
layoutinflator might help you
look here
What does LayoutInflater in Android do?
I don't think this is a good idea, and probably just doesn't work. What would the framework do if it thought it needed an hdpi version of your layout, for example?
You might try to go after the root cause of your layout proliferation:
Could you develop better naming conventions for your layouts?
Could you refactor certain layouts so that more components can reuse the same layouts?
Are you manually handling orientations instead of relying on -portrait and -landscape?