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

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.

Related

How Do I call String-array value in xml layout

I have a string-array in my string.xml file in Android res/value folder like this
my string.xml
<string-array name="books_titles_en">
<item>Smoking Effeccts on Your Body</item>
<item>Smoking - effects on your body</item>
<item>How Tobacco Smoke Causes Disease</item>
<item>patterns of use, health effects, use in smoking cessation and regulatory issues</item>
</string-array>
I need to retrieve the first element of array like this
my home.xml
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string-array/book_title_en[0]"/>
I know it seems like it will cause an error. How do I retrive these things on the string-array in string.xml in Android
Just to giva a full answer to your problem:
There is no way of accessing an array directly, but you could do something like this:
<string name="books_titles_en_1">Smoking Effeccts on Your Body</string>
<string name="books_titles_en_2">Smoking - effects on your body</string>
<string name="books_titles_en_3">How Tobacco Smoke Causes Disease</string>
<string name="books_titles_en_4">patterns of use, health effects, use in smoking cessation and regulatory issues</string>
<string-array name="books_titles_en">
<item>#string/books_titles_en_1</item>
<item>#string/books_titles_en_2</item>
<item>#string/books_titles_en_3</item>
<item>#string/books_titles_en_4</item>
</string-array>
and then:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="#dimen/frag_1_text_pad"
android:textSize="#dimen/frag_1_text_size"
android:paddingRight="#dimen/frg_1_text_right_pad"
android:id="#+id/book_link0"
android:text="#string/book_title_en_1"/>
So you basically reference the neccessary strings directly, that are referenced in your string array. ;)
Refer to this link Android - retrieve string array from resources
And this link Help in getting String Array from arrays.xml file
Basically you will have to code for the array retrieval xml in the Java Class by making an adapter.
The first link I posted used
for(int i = 0; i < menuArray.length; i++)
{
Button b = new Button(this);
b.setText(menuArray[i]);
ll.addView(b);
}
which is a sample of how to obtain the whole array from the xml.
You can modify it for single array value retrieval, or whichever array value you wish to retrieve.
Happy coding :D

wrong Chinese characters in android coding related

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);

Where to declare lots of constant objects?

This might look like a stupid question but I actually have objects - let's call the object "Cocktail" - that contains a certain amount of fields such as cocktail name, list of ingredients, recipe, etc...
So basically what I would like to do is have my cocktails list available when my application needs it, but I do not really know how to store them. I thought of declaring all cocktails in Android string arrays such as following :
<
?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array
name="first_cocktail_name">
<item>ingredient1</item>
<item>ingredient2</item>
<item>ingredient3</item>
<item>recipe</item>
</string-array>
<string-array
name="second_cocktail_name">
<item>ingredient1</item>
<item>ingredient2</item>
<item>ingredient3</item>
<item>recipe</item>
</string-array>
</resources>
And so on, but problem is I don't have the possibility of declaring item key so retrieve all the info will be a bit tedious, for example if the number of ingredients vary between different cocktails index retrieving will not work...
I'm not a big fan of hard-coding all my cocktail objects directly in code when app starts either so I don't really know what to do with that...
Any idea ?
Thanks !

Android dynamically create String Array from XML string resources, without XML string array

Ok, so this question is a bit weird, and kinda rubs me the wrong to even ask, but since I probably don't have a choice I want to see what you guys think. I'm still a novice at Java and Android.
The case is as follows: We are trying to automate the building of our strings.xml for localisation. A parser has been made to convert a csv-file to xml. For regular strings it's not a real problem, that works fine. But the parser that was built, doesn't take string arrays into account and there is little chance that someone will modify it.
Is there an "easy" way to work with the strings and create an string array programmatically based on parts the name attributes?
If not, then I would have to hard code the arrays and that leaves the creator (client) of the language files unable to add items to something that should be a dynamic list.
I know modifying the strings.xml manually might be an option, but because our management wants to automate stuff like that, it's not much of a choice I have.
Probably I will hard code the stuff and say they can't dynamically fill the lists, but still (also for my personal education) I wanna know what you guys think.
Thanks for your opinions or solutions. :)
Cheers!
You can use (& really really really should anyways) references in string arrays. Assuming this is your generated res/values/strings.xml in Swedish:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1">Hej, detta är på svenska</string>
<string name="string2">Denna strängen också</string>
</resources>
You can put your string-array in, for instance, res/values/arrays.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="somearrayname">
<item>#string/string1</item>
<item>#string/string1</item>
</string-array>
</resources>
So you will get strings.xml as below
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="string1">1st string</string>
<string name="string2">2nd string</string>
........
<string name="stringN">Nth string</string>
</resources>
I suggest a simple modification to strings.xml, which is add a string with name count at top of all strings with value equal to total number of strings and add another string with name prefix below the count, then it looks like as below.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="count">4</string>
<string name="prefix">string</string>
<string name="string1">1st string</string>
<string name="string2">2nd string</string>
<string name="string3">3rd string</string>
<string name="string4">4th string</string>
</resources>
So now, in your java file you can access them like below
int count = Integer.parseInt(getString(R.string.count));
String prefix = getString(R.string.prefix);
String[] strings = new String[count];
for (int i = 0; i < count; i++) {
strings[i] = getString(getResources().getIdentifier(prefix+(i+1), "string", getPackageName()));
}
I hope it may help you.

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