How to set values of views from external file in android - android

Hi
My question is "Is it possible that I pick up all values from external file that is present in phone memory and put these values in my textviews,Buttons etc. ?"
if yes then how?

Step #1: Read in the file using standard Java file I/O.
Step #2: Parse the data.
Step #3: Call appropriate setters on your widgets, such as setText() to change the caption of a TextView, Button, etc.

Related

Reading and writing specific line from csv file in Kotlin

I wanted to read and write specific lines from a CSV file.
Here is an example of my CSV
ID; Code; Name
1; ABHD; Paul
2; HYDR; Arthur
3; POAJ; Jake
4; PLMH; Georges
Actually I know how to read all the file and put it into a list of lines by myFile.readLines().
But I want to read a specific line like the line with the Code ABHD, and later on modify that specific Line without reading and writing the whole file. Any idea ?
Thanks by advance.
A CSV file is a not a database. You can't write individual elements inside the file. You need to read the entire file and write the entire file. If the file is a reasonable size, you can read the entire file into memory, alter the data you want to change (in memory) and then write the entire file again. If the file is huge, you probably need to make your modifications "on the fly" (ie: read a line, (optionally) change it, write the line to the output file).
There are libraries that you can use to parse the data, as CSV can be complicated to parse (especially if it contains text strings).
See https://www.baeldung.com/kotlin/csv-files for some help or search for "kotlin csv parse"

Can localization resources be downloaded in runtime?

I have application with internet access and don't want to store many string.xml files for different languages.
What I want:
Application contains only one string.xml with english strings.
When user launches it - I see user's phone locale, and ask my own server for necessary language file. (resource keys will be the same)
When new resource file comes, all interface should work with new file.
The question is how to change existing or add new string.xml file in runtime?
You obviously cannot change, download or remove strings.xml at runtime
If you want to store locations, you will have to use SQLite storage to store translations.
similar:
How to modify strings.xml file at runtime
run time modification of strings.xml
Now I can see only solution:
Create some "localization proxy" that will return me necessary resource (from string.xml or downloaded resource)
Replace all getString() and getText() to your own method getStringFromLocalization
Override TextView, Button and some other views with custom one and change there init and setText methods.
Overriding the standered resource/language using resource files which are complied time then your scarifying performance over customization. Do it only if u need this.

Android Strings

I wrote a big app with thousands of string in the code.... very bad idea, because now I want to translate each string.... big problem.
Copying all strings to the strings.xml takes a long time.
Eclipse has an option to take all selected strings and put them into messages.properties.
Does this work similiar like strings.xml? When, why all people use strings.xml.
Or should is use eclipse to seperate each string and than I should copy them to string.xml?
All people are using strings.xml because this is the normal way to do it on Android. You don't have to manage the load of the strings, to call any locale function in your script.
You can see the documentation here : http://developer.android.com/guide/topics/resources/index.html
BTW, you can easily transform your eclipse generated file to an strings.xml file after the extraction.
In Eclipse you can use the shortcut keys Alt + Shift A, S to extract an inline string in to the strings.xml file via a popup dialog - might be a bit easier than doing it by hand. And as the others say, yes you should ALWAYS use the strings.xml file so that you only have to look in one place when you want to change a string, instead of having to search through all your code.

Keep Strings into .txt file or put them into a Database?

Hey, I have a lot of Strings that I use into my app, the .txt file that I use has ~14000 lines.. and each 3-10 lines are divided into sections like <String="Chapter I"> ... </String> ..
Speaking of performance/speed, should I put the sections into a Database, Or read line by line through the .txt file and check if the section number is the current one? Will this affect speed/performance?
I could also divide each ~2000 lines into a different .txt file so there would be less lines to go through. Is this a bad way of storing data? Thanks
I think sqlite would do the trick. It will probably be way faster than parsing a text file, plus you wont have to maintain the headache of your own ad hoc text database, or build a parser in the first place. Basically, use it, its way easier.
The standard way to deal with Strings in Android is to put them into res/values/strings.xml (I'm pretty sure you can have multiple String files in that directory if you like). If you are developing in Eclipse it will automatically populate the R class (the resource class) with constants that you can use to reference these Strings in your code:
R.string.mystring
Or in XML layouts:
#string/mystring
Or if you're doing something more custom you can use:
String string = getString(R.string.hello);
I would definitely choose this over a .txt file. It's much easier. All the work is done for you! Have a read of this Android article about it.
This is what a database is for. Use it.

Creating java.util.Map in Android

I want to create a java.util.map in android from a resource. I want to do this because I have a lot of entries to populate into the java.util.map and I want to store the values in the res folder of the project in xml format.
Is there an effecient way to do this in android? My map will have around 2500 entries so I want to do this as effeciently as possible and I don't want to hard code them...
Thanks,
Gaz
I think the Xml format is "too much" to simply store key-value pairs. A text file where a line is a key-value pair is more adequat (e.g. with comma separator), also the parsing will be easier.
foo1,bar1
foo2,bar2
...
Save your text file in res/raw directory
Open it via context.getResources.openRawResource(R.raw.fileName)
Loop on each lines and split the line to retrieve the key and the value.
Put them in your map
That's all

Categories

Resources