custom Resource folder names for layout,drawable .. in android - 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)

Related

How to manually add a folder containing needed data in flutter

Iam working on a quiz project in flutter. I stored my quiz text files in folders according to the quiz category. And all these folders are stored in one root folder. So I should be able to list all the folders in this root folder ( which are categories ). And when category folder is opened the files in that corresponding folder should be listed.
The goal is to make buttons with each folder's name and the button will hold the path to open that folder.
ie, if the user clicks a button for folder, a list will be made containing the names of folders inside the clicked folder and according to this list a listview of buttons will be made.and finally when you open folder containing quiz files same will happen but button will contain the corresponding file path.
I have made the folder tree and functions to make buttons according to the names stored in list. But I have no idea where to put this folder inside the project.
I have tried puttin in the root of project, in Lib folder, in asset folder but flutter is failing to detect this directory.
I really need this to work for my project to go forward. Any help of pointers will be appreciated.
For this to work you need multiple things:
Viewing you folders and text files within flutter
Listing your folders and text files
Reading your text files
1 - Viewing you folders and text files within flutter
Since the assets notations doesn't support recursion, you have to either:
Write everything by hand
Use a plugin to generate the assets for you
Since you have many file I suppose the second option is the best for you. I did struggle to find one that worked but FlutterAssetSync is working like a charm according to my tests. The only constrain is that you have to put everything inside the "assets" folder (which is not really a constraint tbh).
Once you put you file in the assets folder, execute the plugin (under Tools > FlutterAssetSync). This will automatically add everything to your pubspec.yaml
2 - Listing your folders and text files
In order to get the name of your folders and your text files you can use the answers to this stackoverflow question. Basically here is the implementation:
Future<List> _getImages(String folderName) async {
final manifestContent = await rootBundle.loadString('AssetManifest.json');
final Map<String, dynamic> manifestMap = json.decode(manifestContent);
return manifestMap.keys
.where((String key) => key.contains(folderPath))
.toList();
}
3 - Reading your text files
To read the content of a text file asset you must use loadString (as we did in _getImages btw). Here is a quick implementation:
Future<String> _getQuizzText(String quizzTextPath) async {
return await rootBundle.loadString(quizzTextPath);
}
Putting everything together you should be able to make it work!

Can there be a content directory, where I can put files (compile time) and load files from (run time), inside an app?

I have some image files which will be dynamically loaded in my app. I know I can put images in the "/res/drawable" directory, but can there be somewhere else I can put files and loaded them by file name at runtime?
The image files contain characters that are now allowed in resource names. I did not name them, so ideally, I need to keep the names. For example, suppose I have "Cat #1.png" and "Cat #2.png", and I would like to load the image into an ImageView by that name at runtime, like so:
MyImageView1.setImageBitmap(createBitmapFromContentDirectory("Cat Pictures/Cat #2.png"))
MyImageView2.setImageBitmap(createBitmapFromContentDirectory("Dog Pictures/Dog #1.png"))
Is that possible, or should I change all image names into conforming names (e.g., cat_number1.png and cat_number2.png) and put them into the drawable directory?
You can put them in assets & load them from there,
Otherwise you can also put them in raw folder.
you can refer to Where do I I Place assets folder for assets.assets doesn't create Resource Id, so you can access them directly. Have a look at this
difference between assets & raw.

keeping images in a categorized way in android drawable folder and how to access them

I have almost 1000 images with different categories like
Students_Images_Folder
Teachers_Images_Folder
Parents_Images_Folder
etc.
I want to keep import these images in my android project with categories not mdpi, xdpi,hdpi etc . Is there anyway to keep them categorized not merged in drawable.
Should I go for database ? I read that database is not suitable as it will lower efficiency. Any solution is welcomed. thanks for giving time to my silly question.
First of All you should add all images into drawable folder with unique name and of course some prefix like st for Student, tr for teacher and pr for parent.
Secondly you can create database with three tables to store the names of images into it individual category wise.
After that you can retrieve the images category wise from database and here is code to deal with resource id from file name.
OR
You can also create assets folder in android, here you can see how to create assets folder in android studio.
After creating assets folder you can create sub-directories according to your categories like student, teacher and parent.
And after than put images inside those directories.
Lastly load images from assets. To load images from sub-directories of assets implement following code
try
{
InputStream ims = getAssets().open("student/st_img.jpg");
Drawable d = Drawable.createFromStream(ims, null);
mImage.setImageDrawable(d);
ims .close();
}
catch(IOException ex)
{
return;
}
You can make subdirectories in your resource directory and make your build system use it with a small piece of Gradle script.
Read here for more information: Can the Android Layout folder contain subfolders?
Or this blog here: Android: Multiple Resource Folders
AFAIK, you can't add subdirectories to res folders, this will cause the resources compiler to fail!
But you can add a prefix at the beginning of imgaes's names for each category, for example: st_image_name for students images

How to use Android Assets with multiple languages?

I am using Assets files as help files in my app and have well over a dozen. I am porting the app to multiple languages. Where do the alternative language asset files go?
I am already using the "res/values" directories for language files (values, values-es, etc) for use within the app. I thought the "Assets" directory was for help files and items like that.
I am trying to NOT muddy my values folders with the many help files that I am including and was using "activity.getAssets().open( file )" to read the files.
Also, some of these "Asset" files are different language pictures.
Can you put the files in /res instead of /assets? This has built in support for multiple languages, there is an easy to follow guide here.
Basically, if your original text is in /res/values/strings.xml, for example, you would put your translations in /res/values-{ISO LANGUAGE CODE}/strings.xml
For example, your French translation would be in /res/values-fr/strings.xml.
Android will pick the appropriate translation file according to the locale of the user's phone.
There are some good explanations of the other differences between /res and /assets here.
For the voice recognition in my app (using Vosk) I have defined the specific asset folder as resource string.
I.e. values\strings.xml contains:
<resources>
<string name="language_directory">vosk-model-small-en-us-0.15</string>
...
and values-de-rDE\strings.xml contains:
<resources>
<string name="language_directory">vosk-model-small-de-0.15</string>
...
So I can access the asset directory via
val assets = Assets(activity)
val assetDir = assets.syncAssets()
val modelDir = activity.getString(R.string.language_directory)
recognitionListener.model = Model("$assetDir/$modelDir")
This way the correct directory is always chosen based in the active locale.
In my case, those are located at models\src\main\assets\sync\<language_directory>
Make folder in assets:
1. htmlpagesNL
2. htmlpagesUS
Copy file from htmlpagesNL and paste to htmlpagesUS and Translate
Use url inside Nl string file:
file:///android_asset/htmlpagesNL for NL translation
Use url inside Us string file:
file:///android_asset/htmlpagesUS for US translation
Support different languages
Support different languages and cultures

Where to store many subofolder locally images android

I have a series of images (200-300) of animals that will be loaded inside my application.There is a root folder and subfolders containing 3-4 images to any type of animal.
The application must work in local.Where I recommended to save these pictures? In assets or drawable directory ? Or something else ?
Thanks in advance
You should store your images in drawable folder.
You can create sub folders in assets dir. and call it with reference to the folder / dir structure.
I'm not sure though you can do it with drawables or not as never tried it but you can give it a try. Rest follow the below arrays approach to manage your code better.
Plus if you want to categorize them then care different arrays for each category and put the resource reference in each one of them accordingly. Then you can user these category arrays according to your needs.
e.g
String dogs[] = {R.drawable.dog1, R.drawable.dog2, R.drawable.dog3, etc};
String cats[] = {R.drawable.cat1, R.drawable.cat2, R.drawable.cat3, etc};
and you can use these arrays in their own category.

Categories

Resources