Localize strings but preserve span start/end info - android

My app shows a variety of messages and in these messages, some parts have colour spans (e.g. ForegroundColorSpan) added to them so they are a different colour from the rest of the message. Currently, they are all hardcoded and because they are hardcoded, I know exactly where to start and end the span for the block of text I want to colour differently.
The problem I'm having is once I localize the messages, I can't figure out how to detect where a span should start and end. For example:
Hardcoded:
'x changed the topic to oh hai' - I know from char 23 to the end should have the colour changed.
Localized to Dutch:
'x veranderde het onderwerp te oh hai' - char 23 is something else.
(This is a trival example, some of the messages have colours in multiple locations)
So my question is, how can I, after localizing, still know where to start and end a span?

I would say it's not possible. How do you want to know about the grammatic, vocabulary of a language, what the translator decided was suitable...
But you can insert markers in your translations - lile ##, ##, or whatever, to indicate these blocks. Then you parse, catch the positions of these markers and use that for the spans.

I decided to make the strings one single colour than do some complex parsing.

Related

Certain unicode characters unintentionally get converted to flag emoji in Android TextView

I am trying to show a text in my android app using special unicode characters (Look like letters).
One character always is correspondending to tow unicode characters:
The first one is always '\uD83C' (55356) which is follwed by '\uDDF9' (56825) for 'A' for example (56826 for 'B' etc...). Setting the text generally works fine but whenever the text contains a substring which correpondens to a country encoding (Like 'ES' for Spain) it does not show the two characters but the flag instead.
I already tried to understand this behaviour and searched for possibilites to turn i did not find any soloutions
Example:
I want to show these characters: 🇹🇪🇸🇹
String value as char array:
Result in my TextView:
Can you help me find a way to disable this behaviour. I already seen it working in other apps.
The characters you are using only exist to produce flag emoji; they serve no other purpose and are not intended to be used for “fancy” text. Displaying flags for valid region codes is their only correct behaviour.
If you absolutely have to use them without that happening, you need to insert invisible characters inbetween every letter to break up the ligatures, for example U+200C (Zero Width Non-Joiner) or U+2060 (Word Joiner).

Android Strings xml stores every value?

Do we have to place every text that we have to type in buttons or inputs or on plain surface of an activity.what happens if we don't provide values of text strings to strings xml??
what happens if we don't provide values of text strings to strings
xml??
you will get a lot of warnings from lint and no strings localization
Using strings.xml is mainly for localization, it's easy to translate a XML file and use it, not using strings.xml is a headache later on when you want to translate your app.
You may not use it, but maybe later you'll regret it.
Besides localization, it's very handy to have all strings located in the same place, when for instance, you have to change some label or correct some messages displayed to users.
Looking for strings in the code afterward could be a real pain when thousand of lines of code are produced.

Android TextView Display HTML With DL DT tags

Is there anyway to display DL DT and DD tags inside a TextView? I tried this
rawString is an array of "<dt>Some Name</dt><dd>Some text</dd>"
StringBuilder builder = new StringBuilder();
builder.append("<dl>");
for (String s : rawString) {
builder.append(s);
}
builder.append("</dl>");
But this doesn't seem to be interpreted even if I use Html.fromHtml() when i set the text
setText(Html.fromHtml(builder.toString()));
I guess if this will never work then is there a way to display information like this in a single textview where we have a 2 column or 3 column data and to line up the columns?
Name: The name
tim: value
yells: yes
If I use Webview then the data looks like this:
Name:
The name
tim:
value
yells:
yes
Seems like there is no way to display this without using a listview
Html.fromHtml() handles a limited set of HTML tags. This blog post of mine from 2010 lists the then-current set, and I am not aware that it has changed much. Quickly eyeballing the source code does not indicate any obvious changes.
So, you have a few choices, off the top of my ever-so-balding head:
Do something other than those tags.
Add a TagHandler to your fromHtml() call and figure out how to set up spans that will format things the way you want.
Use a WebView.
I guess another question would if this will never work then is there a way to display information like this in a single textview:
Getting them aligned like that will be difficult without a monospace font. You might be able to go through some text-measurement stuff to try to determine the right amount of whitespace to add to get things to align, or invent some spans to fill the space. It's one of those things that strikes me as being painful but eventually doable.

Android - Appending text lines. Help choose the right control

Folks,
In my social networking application, single-line messages come from various users. As the message comes in, I need to display them in our UI as a single line that shows the time, the user, and the message line. All 3 fields need to be colored differently.
I tried to use TextView but am running into a problem. As I need various colors, I thought of using SpannableString but the problem is that TextView.Append does not support SpannableString as a parameter.
The other thought I had was to build html style text as each line comes in.
I am wondering if I am overlooking something. Perhaps there is a better user control or a better way to achieve my objective.
Thank you in advance for your help.
Regards,
Peter
Use Html and with help of <font color><font/> tag, you can set single text with different color#
String result="<font color=color_code>First Textview</font> <font color=color_code>Second Textview</font><font color=color_code>Third TextView</font>";
textview.setText(Html.fromHtml(result));
You should use three different text fields, nest them in a table layout using columns, or a relative layout. That way they will automatically resize no matter what length.
You can also use the android:weight tag to control how much room each one takes up.
The reason is that usually its 1 label per field in data structure. As per MVC software design pattern.

Strange Characters Showing in Android Text View

I have a text view that is showing a couple of characters as rectangles and I can't figure out where they are coming from. Any ideas?
Here is the XML with the text:
<item>
Five cents per gallon discount. Go online to www.amerigas.com or yellow pages for the closest AmeriGas location.
\n\n
 
 Offers subject to change without notice, some restrictions may apply, contact store for details. Offer does not apply to fixed price customers. You must provide your Farm Bureau membership number to your AmeriGas dealer prior to delivery of your propane in order to receive the discount.\n
</item>
And here is a screenshot:
EDIT: In the XML, I notice when I paste it here the is a large space before Offers, which is where the characters are showing up. That space doesn't show up in my editor though.
My guess is that after the \n\n in the XML file, you have tab or some other odd white-space characters that the font does not handle correctly. Replace them with regular spaces and the little boxes should go away.
You need to replace the special char \n.
Just do this:
string = string.replace("\\\n", System.getProperty("line.separator"));

Categories

Resources