wrong Chinese characters in android coding related - android

I have a chinese string:
String t = "中文..."
Now I want to display it in some text view:
TextView tv = findViewById(id)
tv.setText(t, null);
But this is showing wrong characters... any idea how could I show it
correctly?
Well, I am asked to post the real code, actually the above code is almost the
real code:
suggestions = new ArrayList<String>();
suggestions.add("今日");
Then I get suggestion first element and assign to t:
tv.setText(t, null);
BTW, when I log it out, I also see wrong characters...

I remember writing strings in Japanese and getting the same problem. I didn't solve it, but have you tried using String resources? If you put Chinese characters in res/values/strings.xml instead of in your source code, they should display as expected.
You can access the Strings in your resource file like so:
String suggestion1 = context.getString(R.string.suggestion1);
To get an array of strings, like in your example, it's convenient to use string-arrays.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="suggestions">
<item>示唆1</item>
<item>示唆2</item>
<item>示唆3</item>
</string-array>
</resources>
And then load them using:
String[] suggestions = context.getResources().getStringArray(R.array.suggestions);

Related

Android translation at runtime

Is there a way in Android to translate strings with an own translation file at runtime?
e.g.
english file:
hello world : Hello world.
german file:
hello world: Hallo Welt.
Like in Webframeworks (Symfony, Rails, ...)
I can think of two ways to do so. One is to specify the Android language/region code and use different res/values/strings.xml to translate the string values. But this will affect the entire app. Here is the reference on supporting different languages
Another way to do so is to create localized resource file like this for server response translation.
So the strings.xml would be like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="error_msg_ge">Fehlermeldung</string>
<string name="error_msg_en">Error Message</string>
</resources>
Below is just a pseudo code since you didn't post any code.
if(!isSuccess) {
TextView textView = new TextView(this);
textView.setText(R.string.error_msg_ge)
}
If we really want to be more precise, then we can specify more error messages in the resource file and swap to use based on the error message returned from the server.
So the XML could be like this
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="error_ge_timeout">auszeit</string>
<string name="error_ge_noresult">kein Ergebnis</string>
<string name="error_en_timeout">timeout</string>
<string name="error_en_noresult">no result</string>
</resources>
Then
if(!isSuccess) {
TextView textView = new TextView(this);
if(errMsg.equals(R.string.error_en_timeout)) {
textView.setText(R.string.error_ge_timeout);
} //...and so on
}
Lastly but not the least, you can also utilize third party API to do the translation. But since you are translating error message received from the server, I am not sure if that is necessary since it is not a conversational purpose so it could be an overkill. In any case, there are many ways to achieve it.

Best way to do TextView.setText() for lots of strings

I have an app that needs to check a random number, and then print out from the string.xml file. Here is the code I currently have (it does carry on but it's just declaring the randNum and rand):
randNum = rand.nextInt(59);
switch (randNum){
case 1:
random.setText(R.string.f_vocab1);
break;
This (needs to) goes on for another 59 cases and strings. I've just started Android development so I don't have a clue for a better way to do this. Can someone please tell me a better way to do this?
You could define a String Array in your strings.xml like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="string_array_name">
<item>first_string</item>
<item>second_string</item>
</string-array>
</resources>
In your code you can access this array with this line:
String[] stringArray = getResources().getStringArray(R.array.string_array_name);
Now generate your random number and get the string by index:
int randNum = rand.nextInt(59);
random.setText(stringArray[randNum];
Note: To get a random number isn't what you want I think, because you will get negative values. You have to deal with this properly in your switch-block or with the array approach I'm suggesting.

Android: text from strings.xml displayed as #numbers

I have updated some string values in strings.xml and my application now shows not the new text but something like #234545201. I have cleaned the projected and rebuilded, there are no import android.R anywhere, just R related to my package. What went wrong?
To obtain a string from your strings.xml file, you can do a few things.
If you need it as a String object, you can use getString(R.string.string_id) to fetch the string, given an ID.
If you're trying to set the text of, say, a TextView, you can actually simply use setText(R.string.string_id) and the OS will obtain the correct string for you.
In other words, the TextView class has a method called setText(int resid), and that's also the reason why you can't write something like the following:
TextView.setText(12345680);
Are you trying to read it directly as R.string.my_string_resource?
Try passing it to getString() as getString(R.string.my_string_resource).
You can put your stings.xml file in the follwing format.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Yamba</string>
<string name="titleYamba">Yamba</string>
<string name="titleStatus">Status Update</string>
<string name="hintText">Please enter your 140-character status</string>
<string name="buttonUpdate">Update</string>
</resources>
and use the name as reference of your textbox ids like.
android:text="#string/app_name"

Reference string in xml

I'm trying to reference a formatted String in my strings.xml.
I was googling around and found this good article:
http://developer.android.com/guide/topics/resources/string-resource.html
I followed it to every detail it would seem, but the text printed out isn't what it's supposed to be.
First, in my strings.xml under resources, i defined it as:
<string name="print_binary">This is the printout: %1$s</string>
And in the .java i put:
String fromString = "one";
Resources res = getResources();
onScreen = String.format(res.getString(R.string.print_binary), fromString);
This gives the following text on screen:
This is the printout: %1$s
Please advice
You can use:
onScreen = res.getString(R.string.print_binary, fromString);
Goodness, I'd totally missed inputting something like
TextView show = (TextView) findViewById(R.id.tvDisplay);
show.setText(onScreen);
And then adding an id for my TextView object.
Too late in the night. It seems like the String.format() statement isn't necessary, i'll read up on that.
Thanks for your support!

Array defining in Android Application

I want to use the concept of array in my Android Application, I don't know how to do that actually.
So could anybody please help me how to do that on demand.
I guess you are talking about arrays in Android through the res folder.
Create an array.xml inside the /res/values folder with something like this:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="names_list">
<item>John</item>
<item>Peter</item>
<item>Charles</item>
</string-array>
</resources>
You can get that array on your Activity by doing:
getResources().getStringArray(R.array.names_list);
There are alot of different "array" types in java... there are actual arrays like Thorsten showed you and then there are lists, collections and hashes. Take you pick. :) A great place to start learning more about Java is the docs.
http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/
http://download.oracle.com/docs/cd/E17409_01/javase/tutorial/
This defines an array of 5 strings:
String[] stringArray = new String[5];
However, I can not imagine that this is really what you're talking about...
CLARIFICATION
If you actually don't know what an array is, then my reply will give you a hint. In case you're talking about something else, this reply should indicate that you're not giving enough detail. You might as well be talking about this...

Categories

Resources