HTML Formatting in TextView - android

All,
I have the following in my string resource xml file.
<?xml version="1.0" encoding="utf-8"?> <resources>
<string name="hello">Hello World, ProDroid_Android!</string>
<string name="app_name">ProDroid_Android-CH3-Resources</string>
<plurals name ="eggs_in_A_basket">
<item quantity ="one"> There is 1 egg</item>
<item quantity = "other"> There are %d eggs</item>
</plurals>
<string name="simple_string">simple string</string>
<string name="quoted_string">"Quoted String 'xyz' string"</string>
<string name="double_quoted_string">\"Double Quoted String\""\";</string>
<string name="java_format_string">Hello %2$s Java format String. %1$s again.</string>
<string name="tagged_string">Hello <b><i> Slanted Android.</i></b>, You are bold</string>
When I go to put the tagged_string into a TextView with the HTML formatting it does not display with it's formatting. However, if I cut and paste the Hello Slanted Android., You are bold and manually put it into .setText it works, or if I set it statically in my layout xml file. However using the Java code below it does not work (All you see is unformatted text):
//HTML
TextView t6 = (TextView) findViewById(R.id.textView6);
t6.setText(Html.fromHtml(ProDroid_Android.this.getString(R.string.tagged_string)));
Any help would be appreciated.
Thx

If you format your text in XML it isn't necessary to use Html.formatHtml(...)
Try this:
t6.setText(getString(R.string.tagged_string);
EDIT:
Hmm... This way worked for me fine and I tried exactly your String.
Maybe your Project needs a clean (Project > Clean ...)
If this doesn't help:
I found another question. Maybe the answer there can solve your problem

Related

Android studio random I'm getting an error

//ENG
I don't know English, I may have mistakes, sorry.
Hello there...
I want to random text, but somewhere there was a problem.
Where the problem is located
//TR
Merhaba...
Random text yapmak istiyorum ama bir yerde sorun çıktı.
Sorunun bulunduğu yer
Kodlarımın tamamı ise aşağıdadır.
There is a empty line in the start of the strings.xml file remove the empty line.
<?xml version="1.0" encoding="utf-8"?>
Remove this line from top
<resources>
<string name="app_name">JarTest</string>
<string-array name="status">
<item>Available</item>
<item>Busy</item>
</string-array>
</resources>

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"

string encoding, mobile encoding

I have a strings.xml file with all my app string, I have some simbols like €, and I having trouble with them.
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="currency"> € </string>
</resources></resources>
They usually shows fine, but not in all devices:
How can I auto-detect the configuration in the device?
You can create string files for different languages. Please read "Supporting Different Languages".
I found the problem, in the java class I had:
webCost.loadData(cost, "text/html", null);
And it should be:
webAbout.loadDataWithBaseURL(null, aboutStr, "text/html","utf-8", null);
You should use Unicode Character for Currency symbol.
so instead of
<string name="currency"> € </string>
use this one
<string name="pound">\u00a3</string>
<string name="euro">\u20ac</string>
This will uni formally look same in all the devices.
This will surely solve your issue.

Android String Resources Reference

i am using Eclipse 3.7.2.
I dont know why but the last line leads to an error highlightning
<string name="app_name">Test</string>
<string name="title">#string/app_name</string> <!-- works //-->
<string name="txt_text">Checkout #string/app_name this works</string> <!-- works //-->
<string name="txt_recommend">#string/app_name is not working</string> <!-- error //-->
is there a work around?
I believe you can't mix references and text in the XML. Use formatting placeholders instead.
http://developer.android.com/guide/topics/resources/string-resource.html#FormattingAndStyling
Instead of
<string name="app_name">Test</string>
<string name="txt_recommend">#string/app_name is not working</string>
this would look like
<string name="app_name">Test</string>
<string name="txt_recommend">%s is not working</string>
And in the code:
String text = String.format(res.getString(R.string.txt_recommend), res.getString(R.string.app_name));
The error is probably from the second last line rather than the last line.
<string name="txt_text">Checkout #string/app_name this works</string>
In this line you seem to be mixing text ("Checkout" and "this works" ) with a reference ("#string/app_name")
I dont think you can do that just in xml.

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.

Categories

Resources