How to dynamically load strings.xml easily? - java

I'm working on android i18n now. There are some strings.xml files in so many values-xxxx directories, but the product manager want to put these hole strings.xml files into the server, app get strings.xml File the rom network by a language-parameter dynamicly.But strings.xml is used in these ways: #string/xx, Widget.setText(R.string.xx), getString(R.string.xx), Toast.make(x, R.string.x, x) and so on... How to dynamic load android strings.xml easily? Or do you got a easier way to solve my problem? Thanks!

Frome one app I worked on:
After starting the app make an api call. The server responds with a JSON, containing all the strings. After parsing it, we can dynamically set all the strings in the right context or keep objects or even the json for getting the strings when required.
Hope this idea can help you.

i do understand that product manager wants to change this for a reason.
My Solution would be as follows:
Keep all string related to different language on server with different url pattern. In short url to get string should be like http://www.example.com/strings/{lang_param}/ example: http://www.example.com/strings/en/, http://www.example.com/strings/pt-br/ something like this.
Let server return JSON, where in key of strings would be same but the values changes based on url parameter i.e lang_param. Along with json server must return an identity has to indicate whether some strings items are changed or not. This identity hash must be kept in shared preference such that next time app boots-up you can query server check hash and if they are different mean some new changes are there get them in.
It is mandatory that you keep default strings.xml in internal memory of application using File file = new File(context.getFilesDir(), filename); given on https://developer.android.com/training/data-storage/files.html.
Once you get the new file update internal memory json file and create a utility method that parses this json. Please note that you need to maintain different hashes and json strings files based on different languages.

Related

identify similar location string

Given two strings representing locations, how can we identify if both are same or different locations.For eg. "Bangalore, Karnataka" and "Bangalore, India" are both same
location which is Bangalore.
I have a web-based application with a text-box for "location" and user can specify values as mentioned in the above example. Input values are stored into elasticsearch and later I need to find distinct locations. For that I am using Term Facet in elasticsearch but I getting two entries,"Bangalore, Karnataka" and "Bangalore, India". Somehow I need to identify that both are same locations.
I am looking forward for java based solution for this.
I would use a geo-coding Java API such GeoGoogle. It is open source. With that, you can call:
GeoUtils.distanceBetweenInKm(firstAddress.getCoordinate(), secondAddress.getCoordinate());

Android Intents and Lists

I plan on reading several files when my app/game is created and using the information from them for the entirety of the app. I also have to write to the file at one point.
I have two files. One is a 2-column text file that I'll turn into a dictionary for fast searching. The other is a text file that has 11 columns. I'll make a dictionary out of two of the columns, and the other data I need kept as is so I can write to the columns to count the amount of times something happens in different circumstances for datamining.
Currently, I've turned the second file into a list of a list of strings, or List>. I can't figure out how to pass that around in intents. ".putStringArrayListExtra" only works for a list of strings.
Am I going about this the wrong way entirely? This is my first real Android app.
In order to store a data structure into an Intent, it has to be either serializable or parcelable. If your data structure is neither of them, you might create a class that would implement Serializable and manage it. A good example might be found here.
Once done, you then might use Intent.putSerializable(...) to store your data structure. See this:
Using putSerializable in Android
Additionally to this, if you could convert your structure into a JSON structure, you'd already have it done since it would be treated as a String. If not, the above solution should be easy to do.

Java saving strings

I have a RuneScape Private Server project coded in Java, and am trying to code a personal "tag" that players can use. I have managed to do this, but everytime there is a restart on the server, their "tag" gets reset to "null".
Their "tag" is initalized by doing a command ";;settag [name]". Their tag is then set to whatever they want. I have done this through a string:
if (command[0].equals("settag")) {
newTag = getCompleteString(command, 1);
newTag = player.yellTag
player.sendMessage("Your tag is now:" +newTag);
}
I am unsure what the most efficient way to fix this would be, I am thinking of just loading and saving through .xml/.txt files. By the way, player.yellTag is where the next command (::mytag) searches it from, which works fine, until there is a restart of the server.
it all depends on the context of your application. If you are planning on having less than a few hundreds players, then a xml file may be ok. You should look at JAXB, which is, afaict, the standard way to store your objects in Java. You can also store them as JSON files, using gson which is way simpler to use and implement than XML stuff.
But if you get to have more than thousands of players, you may want to get some more efficient way to serialize your tags by putting them in a database, and thus an ORM library like hibernate could help you do that.
You may want to make your own stuff, like a tag directory full of files named after unique ids of your players containing the players' tag... It's a lot more "hackish" but still quite efficient.

Automatic adding of elements to an array

Hi I'm not particular good at Java so please bear with me. I'm trying to write a very simple android app now and I need help with some coding.
Thing is, I have a server that automatically generates .png files and saves them to a public directory in a numerical order. The update occurs daily and is non-exhaustive.
Is there anyway in which I can assign the dynamic values to an array within my app?
private String[] myRemoteImages = {
"http://hypotheticalurl1.png",
"http://hypotheticalurl2.png",
"http://hypotheticalurl3.png",
"http://hypotheticalurl4.png",
"http://hypotheticalurl5.png",
"http://hypotheticalurl6.png",
"http://hypotheticalurl7.png",
"http://hypotheticalurl8.png",
"http://hypotheticalurl9.png",
"http://hypotheticalurl10.png",
"http://hypotheticalurl11.png",
"http://hypotheticalurl12.png",
//...blah blah blah
// these are all dynamically created so I won't know what is the last number on the list
};
This array will eventually be used to get the images from my server using the app. It works so far but that's only with hardcoded URLs. I would like the URLs to be dynamic, as the number of images will change from day to day.
I'm doubting that regex will work well in Java but then again I'm no expert. Was thinking of perhaps writing a script on the server end that generates a list of existing values and somehow parsing that with the android app.
Can anyone point me in the right direction? Thanks in advance.
Clarification:
The array doesn't have to be dynamically sized while the app is running.
I need a way to read the list of existing images in a remote directory and pass that information to populate the array automatically at runtime.
Resolved
Guys, thanks for the help. Sorry if I wasn't clear enough.
I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.
After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.
thanks for the help. Sorry if I wasn't clear enough.
I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.
After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.
With an array you can :
change the elements of the array
but you can't :
add or remove elements. The number of elements if fixed in an array. Some workaround can be found like putting null values and discarding theem when using the values in the array. But that's more troublesome than really useful.
On the other hand, if you want a full dynamic "array" : use a list (java.util.List). An ArrayList would be interesting here, or even a Vector as you will probably need some multihtreading around this array. With a list you can add and remove elements, size can vary and elements can be replaced.
I'd use an ArrayList in this case. You don't have to know the number of elements you want to add then and it's very simple to append elements at the end.
private List<String> list = new ArrayList<String>();
Then simply add elements by
list.add("http://hypotheticalurl1.png");
Regards,
Patrick
instead of using Array of String
use ArrayList<String> It will gives you more flexibility on adding and removing item on runtime refer this link...http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html
hear you can find example on arraylist...http://www.java2s.com/Tutorial/Java/0140_Collections/0160_ArrayList.htm
hope that helps
According to your scenario you need to have the followings:
1- a Web Service which has a method to get you the list of the available image names.
2- You need a web service client for your android application, I suggest you to use KSOAP 2 because it is widely known and easy to implement.
(If you can't figure out how to use the ksoap in your program, I can provide you some example codes)
3- You need to use ArrayList(java.util) to hold your dynamically sized array.
Hey ytou can do it via
ArrayList stringList = new ArrayList();
stringList.add("Item");

Best way to store text data in an android app?

I've made an Android application which contains most used German words and sentences. The application contains a CSV file which stores all the data.
Currently it is working as expected but I want to ask if there is a better way to store such data directly in the app?
I'm also thinking about the ability to update the data via internet like adding new words and sentences.
Thanks!
Miretz
If you want to modify the content (update, remove etc.) I would suggest using SQLite DB which has a pretty nice built-in integration with the Android platform.
There are 2 types SQLDatabaseLite and SharedPreference. Major difference between both is that one is organized and the other not so.
If you need a quick use of a storage facility within your app for example changing text sizes between activity SharedPrefference works best for you.
If you have a complex database system where you need more than one data to be saved for a particular event SQLDatabaseLite is for you example of this is spreadsheet of data for customers; Name, Phone Number, etc.

Categories

Resources