Add text file resources to R.java - android

I have text files named hierarchy1.txt, hierarchy1.1.txt and heirarchy1.1.1.txt in my android project->res->raw folder. These files have json objects. I m trying to read from these files but these are not getting added in my R.file. How do I resolve this?

You should use underscores instead of dots in your filenames.
If you rename your file to hierarchy1_1.txt you should be able to access it with:
context.getResources().openRawResource(R.raw.hierarchy1_1);

Related

Android Studio Group File Template, Custom Path For Child File

Am trying to create a Group File Template which has to child files
FragmentKotlinClass.kt
ViewModel.kt
fragmetn_layout.mxl
But the problem is that all three files created in the same directory
The question is How to specify the directory name in the File Name Template
for the .xml file to put it in res/layout/
You can create file in upper directory by using upper directory prefix ../fragment.xml or ../../fragment.xml
So the challange is finding how many times we should go upper. I used $PACKAGE_NAME variable and some regex to solve this problem. First I go up until project root, then I go down to /res/layout/ and finally added my fragment file in snake case format.
So you can try this one-liner solution in your template's File Name:
#set( $LAYOUT_FILE = $PACKAGE_NAME.replaceAll("\.", "/").replaceAll("\w+","\.\.").concat("/../res/layout/").concat($NAME.replaceAll("([a-z])([A-Z]+)", "$1_$2").toLowerCase()) )${LAYOUT_FILE}

How to read raw folder file name which has space between words

I have a few mp3 files in raw folder in which some of the file name have two words.I mean space between the words.Please help me how to read the file name.
That's not a valid resource naming. Replace all spaces with underline character (_):
Original - original file name.mp3
Changed - original_file_name.mp3
This will allow you to access that file through Resources.openRawResource() API.

How to add two files with the same file name into a single zip archive file?

As from the zip format specification http://en.wikipedia.org/wiki/Zip, it is possible to add two files with the same file name into a single zip archive file.
For example, I'd make a zip looks like
foo.zip
--bar.txt
--bar.txt
--3rd.txt
Does anyone know to accomplish this?
I tried linux utility zip and unzip, it always overwrites the previous added zip entry.
The Java class java.util.zip.ZipFile does not work either.
One way to do this is to create the zip initially using unique names - e.g. bar.txt, car.txt and 3rd.txt. Then open up the resulting zip file in a binary editor and search for car.txt and replace it with bar.txt.
Note that there should be two occurrences of the filename that you need to replace - one in the local file header for the file (somewhere in the middle of the zip), and one in the central directory (somewhere near the end of the zip).
If you need to do this programmatically, I would suggest you actually parse the central directory to find the exact positions of the filenames in the various headers rather than a simple search and replace to avoid the chance of false positive. It's not a very complicated format.
Note that when you try to uncompress a zip like this, you may get a warning about the file already existing when the second copy of the filename is uncompressed, depending on what program you are using for unzipping and what options you have set.

custom Resource folder names for layout,drawable .. in android

I have 2 different functionality to be implemented as part of a single application.
They have different screen flows and BL to be implemented but a single apk,so it is transparent to the end user.
I have 100 screens for each of the flow,so can I have custom folder names in the resources directory for easy maintenance and loose coupling.
eg: res
-layout
-layout_savings
-layout_checking
-drawable
-drawable_savings
-drawable_cheking
currently we have only the following structure
res
-layout
-layout-land
-drawable
-values
Will there be any problem while generating the R.java file.
Any help would be appreciated.
for what you need , i suggest you to use the assets directory instead of res. in assets dir you can sort your files anyway you like (dir name/filename) . You access them with AssetManager.
this require a little change in code but i think this is the only way if you want to sort your file like that , res folder doesn't allow custom directory names.
(Or of course you can store files in the sdcard)

Change string.xml at runtime

I have a special request to implement dynamic language packs and themes in Android. This basically means:
to download a zip file containing a file named strings.xml (containing the translation)
replace in the application the file /res/values/strings.xml with the one downloaded
Is this possible? Thank you for your help.
No. Things inside the Res folder are static and you can't recreate the R class in runtime.
Why don't you try using sqlite for your situation?

Categories

Resources