Update resource from external API android - android

In my app I want to update the strings.xml file at runtime. So the idea is to download the content from an external API and then update the strings.xml file. Does anybody had any past working experience on this or any idea how to achieve this goal will be a great help!!

You cannot modify resources at runtime. You are welcome to ship a new APK that contains the new resources, though.
You are also welcome to download files from the Internet, parse them, and use their contents. However, you have no means of using them literally as string resources. They will just be strings (or whatever else you parse from the file).

You can't do that. If you ever happen to add values to the strings.xml at runtime, their ids won't be generated in the R.class, you can alternatively save those strings to another file on the phone (database file, json file, xml, etc.).

Related

How can I load the strings.xml dynamically? Can I put the file in a server?

Why i ask this is because the size of my APK is huge. I wanna make it smaller.
There're lots of strings in strings.xml. Our product manager force us to support all languages on the earth.
I'd like to know, can I just put some languages of strings.xml locally, put others in a server, then when user launches the app, downloads the strings.xml from it dynamically according to the language of the user.
I am not quite clear about the process of how android load the strings.xml file. Any idea about it?
Thanks~~
can I just put some languages of strings.xml locally, put others in a server, then when user launches the app, downloads the strings.xml from it dynamically according to the language of the user.
You cannot modify resources at runtime. You are welcome to download and process XML files at runtime, and those XML files might contain strings that you want to display to the user. However, you cannot use the Android resource system to pull in those strings. You would need to write your own Java code that uses those values, including determining which translation to use for a given device (taking into account the multiple-locale support offered in Android 7.0).
You can easily call some web-service on your application start and fetch the strings of desired/selected language in form of say JsonArray. Then parse that data to some data-model like ArrayList of string to manage in your app

Android String Resources from API

It's possible to add dynamic strings resources in my application from my API. Stored with json or XML and with the same format like it is right now?
I have already a multilingual website, so i want in my application to have the same strings from an API! How can i store them in my application in strings resources files?
sorry for my english.
Strings resources files cannot be updated in Application lifecycle because there are compiled when you generate your APK. Maybe you need to look after storing your strings in a database or something else . See https://developer.android.com/guide/topics/data/data-storage.html
As said before you can't modify the resources inside the app. But you can send the message/text in the required language from the server, I would implement a mechanism through which the app tells the server which language must use for the returned texts, and the app simply draws them when received. It is easy, simply change the focus from the app to the server.

Can I change a string in the strings resource file to one from array in an activity?

Been having a little bit of trouble with this. Googled about and I can't really find anything. Saw a few things about setting text but I don't think that affects the string resource file or at least I can't make it affect the resource file. Help please!
No, you can't modify apk at the runtime. If you don't own source code, you can pull apk file to the computer and decompile it by using APKTool, for example. If you are working with data you get from internet/user, it should be kept inside DBs, files, whatever. In other words in internal or external storage. Please take a look on storage options you can use http://developer.android.com/guide/topics/data/data-storage.html

Replacing XML file at runtime on android

I'm new to android development and I was wondering if it is possible to change an XML file during runtime of the application.
I'm having an XML file with data that I put into a database, but I would like to have the possibility to download another XML-file (with newer data) and replace the old one.
(I'm not talking about the manifest.xml file or layout.xml files, it's a file I created myself in a self-made subfolder)
Any suggestions how I can manage this?
If you download XML, parse it in the database. Why do you need to 'replace' it? (You can do this but it's not the best solution).
What you should do is download the XML into memory (for example HttpClient or any other), parse the XML and directly insert into the database.
This means you don't need to save anything.

Is it possible to make use of the Android resource resolver on a self-created folder in the file system?

I'd like the ability to "overwrite" the Android resources packaged within my apk by having the app periodically download a zipped file containing overrides that follow the same naming convention as the source does. For example, the zip might consist of the following paths:
res/values/strings.json
res/values-land/strings.json
My code would parse those files and produce a Map> that would map the string resource id to a folder->value pair (or something along these lines). At this point I'm really only concerned with strings and images (maybe arrays), but not layouts, etc.
To the point: Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state? I'm assuming this is difficult because the compiler breaks everything down to ids, but figured it was worth a shot. Any ideas?
Is there any method available, that, given a list of folder names, would tell me which one the Android resolver would choose based on current state?
No. You are welcome to roll this yourself based on Configuration, DeviceMetrics, and kin. You will also need to create your own parsers for your own files, as Android's resource system only works with resources, not arbitrary files retrieved from arbitrary locations.
The expectation in Android is that if you want to update resources, you update the app, probably because there were also code changes as well. Admittedly, this approach has its limitations.

Categories

Resources