I want to use multi language in my app but cant solve problem with array; I couldn't define text from XML here. So what do you advise me to do here so that i can use multi language ? thanks a lot
public static final String[] columns = { "Bluetooth", "WiFi",
"Mobile Networks", "Auto Sync", "Gps", "Auto-rotate screen",
"Vibrate on touch", "Airplane mode", "Brightness", "Sleep",
"Volume Settings", "Phone Ringtone", "Uninstall",
"Backup & Restore", "Battery Usage", "Cache Clear", "System Clear",
"System Info" };
}
You can define string array in strings.xml, as the documentation says like:
<resources>
...
<string-array name="numbers">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
</string-array>
...
</resources>
Than you can get it from code like :
String [] fiilliste = getResources().getStringArray(R.array.numbers);
Don't forget to define your array in every strigns.xml in each of your values-XX folder which you want to support.
I think the best approach is to use R.string.whateveryouwant as Integer array, something like this:
Integer[] columns = { R.string.bluetooth, R.string.wifi, .... }
And when you display the text get the value from the language xml file, given the Integer key.
Related
I'm making a french Android app and I'm trying to support English.
I use "placeholders" to format my strings, so I can adapt them to male and female users. For example, this string in my strings.xml file:
<string name="encouraging_comment">
Les %1$s sont compliqué%2$ss...
</string>
will become "Les hommes sont compliqués" ("Men are complicated") or "Les femmes sont compliquées" ("Women are complicated").
And there lies my problem. The string translation, as follows...
<string name="encouraging_comment">
%1$s are complicated...
</string>
...needs only one placeholder, when the french equivalent needs two.
How can I manage this issue ?
Thanks in advance.
Just omit the second placeholder from the English template string and use an empty string for the second argument (or any other string, it doesn't matter, the value will be ignored) when you render the string:
XML:
<string name="encouraging_comment">
%1$s are complicated...
</string>
Java:
getString(R.string.encouraging_comment, "women", "");
getString(R.string.encouraging_comment, "men", "");
This works because it's not an error if there are more arguments than placeholders, only if there are fewer arguments than placeholders.
I'm assuming that you'll have some kind of table or mapping where you look up the placeholder values based on language and gender. In pseudo-code:
(French, Female) -> ("femmes", "e")
(French, Male) -> ("hommes", "" )
(English, Female) -> ("women", "" )
(English, Male) -> ("men", "" )
AFAIK, you can do this by adding check in your code like
String result;
if ( Locale.getDefault().getLanguage().equals("en")){ // examples "en", "fr", "sp"
result= getString(R.string.encouraging_comment, myString);
}
else{
result= getString(R.string.encouraging_comment, myString, myStringTwo);
}
textView.setText(result);
Hope this helps you :)
One way to do this is create a string locale in your strings.xml
for English Strings.xml
<string name="locale">En</string>
for French Strings.xml
<string name="locale">Fr</string>
Then while setting string
switch (getString(R.string.locale)) {
case "Fr":
//set your string for French
break;
case "En":
//set your string for English
break;
}
So I have many strings in strings.xml, they are recorded in a format of:
<string name="list_1">xxxxxxxxxxx</string>
<string name="list_2">xxxxxxxxxxx</string>
<string name="list_3">xxxxxxxxxxx</string>
......
Now I want to load them one by one without having to type all the string IDs. I want to load them in a fashion like:
for (int i = 1; i <= num; i++) {
// Just showing what I mean.
String xxx = getString(R.string.("list_" + i));
}
Is there a method to do so?
Try this:
int resourceID = getResources().getIdentifier("list_" + i, "string", getPackageName());
String xxx = getString(resourceID);
It would be worth to use resource arrays. For example
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="planets_array">
<item>Mercury</item>
<item>Venus</item>
<item>Earth</item>
<item>Mars</item>
</string-array>
</resources>
and you can access, iterate as follows:
Resources res = getResources();
String[] planets = res.getStringArray(R.array.planets_array);
In order to build vibrate pattern, I tried to define the array of data, in XML.
<string-array name="tab_bip_1">
<item>0</item>
<item>100</item>
<item>100</item>
</string-array>
Resources r = getResources();
long[]tab_vibrate = r.getIntArray(R.array.tab_bip_1); // Not correct...
Vib.vibrate(tab_vibrate, 0);
But to get array of values, it seems to be only possible to use getIntArray() which give an Int array, when vibrate needs a long array.
Is there a kind of "getLongArray()" method?
Or do I have to get data in string then loop to perform a long.parseLong() against each value?
Thanks
<string-array name="tab_bip_1">
<item>0</item>
<item>100</item>
<item>100</item>
</string-array>
You're trying to get IntArray, but this is a string array. Try this:
<integer-array name="tab_bip_1">
<item>0</item>
<item>100</item>
<item>100</item>
</integer-array>
And replace long with int, because it is just integer array.
Other than string resource we have in Android, we have the followings: https://developer.android.com/guide/topics/resources/more-resources.html
As you can see, there is not long type of xml resource. You need to use integer for number-type resources.
Thanks for the answers. Using an int array may be sufficient in this case but at the end I need to convert values as Vibrate don't want an Int Array but a Long one. :(
Also, using and Int array in XML is OK only if you have value lower than 32 bits. Not sure of what getIntArray() will say if we have an XML file with values greater than 32 bits.
I resolved my problem looping and casting string to long like this:
<string-array name="tab_bip_1">
<item>0</item>
<item>1000</item>
<item>2000</item>
</string-array>
// Get vibration array, which is in String
Resources r = getResources();
String[]str_tab_vibrate = r.getStringArray(R.array.tab_bip_1);
// Then loop around to transforme in long values
long[] long_tab_vibrate = new long[str_tab_vibrate.length];
for (int i = 0; i < str_tab_vibrate.length; i++) {
long_tab_vibrate[i] = Long.parseLong(str_tab_vibrate[i]);
}
// Then, vibrate
Vibrator Vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
Vib.vibrate(long_tab_vibrate, 0);
It works. Hope this will help.
I have a preference xml file and a listpreference.
The listpreference entryValues and entries are in a array.xml file.
Here's the problem, the entries/entryValues contain "10 mb/s" and i would like to get the int value from that entry/whatever is selected.
This gives me an error however, here is the code:
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "8");
int i = Integer.parseInt(result.substring(result.lastIndexOf(" mb/s")));
t.setText("Int value: " + i);
As i said this gives me an error and i cannot find the issue.
Thanks for any help!
FINAL FIXED CODE
TextView t = (TextView)findViewById(R.id.textView1);
String result = sp.getString("BITRATE", "3");
int intRes = Integer.parseInt(result);
t.setText("Int value: " + intRes);
Also i changed the preferences entryValues items to only be integers instead of ex 10mb/s to just 10.
Make your entryValues array containing only the integers. The entries are used when displaying to the user, the entryValues are what is stored in your preferences (and are not displayed). The two arrays do not have to have the same contents, but they should have the same number of items.
<string-array name="bitrate_entries">
<item>10 mb/s</item>
<item>25 mb/s</item>
<item>50 mb/s</item>
</string-array>
<string-array name="bitrate_entry_values">
<item>10</item>
<item>25</item>
<item>50</item>
</string-array>
Now you only need to use Integer.parseInt()
I have an ArrayAdapter<String> that I am using to show a single choice Dialog like this:
dialogBuilder.setAdapter(arrayAdapter ...
This is the ArrayAdapter:
arrayAdapter = new ArrayAdapter<String>(StartActivity.this, android.R.layout.select_dialog_singlechoice);
arrayAdapter.addAll("A Tropical Rainforest", "Backwater Chorus", "Big River", "Bird Song 1", "Bird Song 2", "Cave Ambience", "Cold Stormy Wind",
"Crickets", "Deep Woods", "Fireplace", "Jungle River", "Long Soothing Rain", "Ocean Waves 1", "Ocean Waves 2",
"Pacific And Songbirds", "Pig Frogs", "Small Green Froggies", "Small Rapid", "Sparkling Water", "Stormy Wind",
"Thunder and Rain", "Thunderstorm Inner Perspective", "Twilight", "Waterfall", "Wind");
Now the problem is that I want to make this a multi language app which means I need to get all of this strings from String Resources. But I can't do it since a String Resource returns int while I can only insert String in there.
Any idea how I can do this?
Create an array resource
<resources>
<string-array name="values">
<item>A Tropical Rainforest</item>
<item>Backwater Chorus</item>
<item>Big River</item>
<item>Bird Song 1</item>
<item>And so on</item>
</string-array>
</resources>
And now, you can retrieve the values from code like this:
String values[] = getResources().getStringArray(R.array.values);
arrayAdapter.addAll(values);
First, define a string array in resources:
<string-array name="nature_things_array">
<item>A Tropical Rainforest</item>
<item>Backwater Chorus</item>
<item>Big River</item>
<item>Bird Song 1</item>
</string-array>
Then set single choice items to AlertDialog.Builder:
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.pick_color)
.setMultiChoiceItems(R.array.nature_things_array, null,
new DialogInterface.OnSingleChoiceClickListener() {
#Override
public void onClick(DialogInterface dialog, int which,
boolean isChecked) {
// ...
}
})
});
return builder.create();
You can find a good android dialog manual here.