I have a string resource called "foo". It may be a simple string... or it may contain HTML. This may change over time: I should be able to box it up as at least a SpannableString immediately upon reading whether it's HTML or not (but how??)
I want to get that raw CharSequence and first be able to display it as-is (the exact characters, not Android's "interpretation" of it). Right now I can't do that... toString() decides to rip out the parts it doesn't think I want to see.
I'd then like to be able to create a SpannableString from this and other Strings or SpannableStrings via concatenation using some method (none of the normal ones work). I'd like to then use that SpannableString to display the HTML-formatted text in a TextView.
This shouldn't be difficult, but clearly I'm not doing it right (there's very little info out there about this that I've found so far). Surely there is a way to accurately interconvert between between Strings, SpannedStrings and even Spannablestrings, without losing the markups along the way?
Note that I've already played with the somewhat broken Linkify, but I want better control over the process (no dangling unformatted "/"s, proper hrefs, etc.) I can get this all to work IF I stay in HTML at all steps, though I can't concatenate anything.
Edit 1: I've learned I can use the following to always ensure I get my raw string (instead of whatever Android decides it thinks the CharSequence really is). Nice... now, how to coax this into a SpannableString?
<string name="foo"><![CDATA[
<b>Some bold</b>
]]>
</string>
Edit 2: Not sure why this didn't work earlier, but... if foo1 and foo2 are strings marked up as above (as CDATA), then one can apparently do this:
String foo1 = (String)getResources().getText(R.string.foo1);
String foo2 = (String)getResources().getText(R.string.foo2);
SpannedString bar = new SpannedString(Html.fromHtml(foo1+foo2));
Curious: is there a more straightforward solution than this? Is this CDATA business actually necessary? It seems convoluted (but not as convoluted as never quite knowing what the resource type will be... String, Spannable, etc.)
I had the same problem. There are two solutions according to Google API Guides.
First is to escape < mark with < in the string resource. Unfortunately, String conversion removes the tag in the background.
Second is to use Format Strings instead of XML/HTML tags. It seems simpler, faster, and evades hidden conversion problems. getString(resource, ...) works like a printf(string, ...) here.
Both work and require some code to replace given part of the string anyway (handle tags or format strings). Enjoy! =)
It appears there isn't a more straightforward way to accomplish this.
Related
i have a simple Memory Game as Project. For the Memory Tiles I wanted to use Emojis. I tried to use it that way:
emojiCard.setText(new String(Character.toChars(Integer.parseInt(1F60D, 16))));
now I just have to save 1F60D to a variable and can show the emoji.
that works for simple emojis but I cannot use the "new" ones because then i have to use surrogate pairs and I don't know how to do this.
Is there a better way ? like saving the unicode ?
sorry i'am really new to android development and tried already a lot of things.
Thanks.
Integer.parseInt() takes a String as input, so presumably you meant to say Integer.parseInt("1F60D", 16) instead. Which would be wasted overhead when you can simply pass a numeric 0x1F60D literal to Character.toChars() instead.
Java strings use UTF-16 encoding. When encoded to UTF-16, codepoint U+1F60D uses surrogate pairs, so surrogates is not your issue.
Assuming you are referring to how newer emojis support modifiers (to change their genders, colors, etc), then that has nothing to do with surrogates. You simply append the modifier codepoint(s) you want after the base emoji codepoint. For example:
emojiCard.setText(new String(Character.toChars(0x1F466)) + new String(Character.toChars(0x1F3FE)));
(👦 + 🏾 = 👦🏾)
It is known that Androd string resources support xliff namespace to annotate non-translatable string formatting placeholders, like this
<string name="max_file_size_exceeded_template">File
<xliff:g example="some_image.jpg" id="file_name">%1$s</xliff:g>
is too big and could not be uploaded.</string>
It helps translators to undestand what parts of string should not be modified. But sometimes I need to annotate some strings max length it they are used in UI control with limited size. What I want is add text length limit to warn translators about this. Something like max-length="24" max-lines="2" length-unit="char" Maybe, xliff supports such thing or it can be achieved in other way.
I use Weblate for translations if it matters.
You can set this in Weblate, the Andoid format does not support this directly.
Click on edit (pencil) icon next to flags on the string.
Enter max-length:LENGTH as check flag.
See also https://docs.weblate.org/en/latest/admin/translating.html#additional-information-on-source-strings
I have the following idea:
In German we have four extra letters (ä, ö, ü, ß) and I don't know any other language which has these vocals but I think French people with their accents also know this problem. We have a lot of apps in the Google Play store for cities, bus stations, trains and other stuff like that. Now it is really exhausting that we always have to write these letters if we are on the go. It would be much easier to write Munchen (=München [de] = Munich [en]), Osterreich (Österreich [de] = Austria [en]) or something like Uberwasserstrasse (Überwasserstraße [de] = Over-Water-Street [en]). So my question is now:
A lot of apps show suggestions for our just typed word. I think in the code it is something like this:
String current = editText.getText().toString();
db.lookUp(current); // Of course SQL statement
Can we hook this so that Android thinks that we have typed an ä, ö, ü, ß if we write an a, o, u, ss and the system looks for words with one of these vowels and suggests both? Here I do not want to ask for code - I want to discuss if we are able to write a hack or hook for the Android system. Also, root-rights can be assumed with the solution. I'm looking forward to your ideas.
You could do this the other way around, by "normalizing" typed characters into their related non-diacritical versions. You can use the java.Text.Normalizer class for this. A good snippet can be found in this article:
public static String removeAccents(String text) {
return text == null ? null :
Normalizer.normalize(text, Form.NFD)
.replaceAll("\\p{InCombiningDiacriticalMarks}+", "");
}
When applied to "Münich", this returns "Munich". That way, you can use a simple string comparison using these normalized versions.
This wouldn't work for "ß" though. If that's the only special case, you could handle it separately.
What you are looking for is called accent-insensitive collating sequence. SQLite's COLLATE operator can be used to do such searches, but I learned from another post that there might be bugs you'll need to look out for.
Using this link I was able to create a filter using the regex (?!dalvikvm\b)\b\w+ to filter out messages with the tag dalvikvm, but I have tried several variations of the regex such as (?!dalvikvm-heap\b)\b\w+, (?!dalvikvm\\-heap\b)\b\w+, (?!dalvikvm[-]heap\b)\b\w+, and many others and I can't seem to get rid of the dalvikvm-heap messages. Ideally I would like to filter them both, but I haven't figured that part out yet either.
Any help would be appreciated.
Use ^(?!dalvikvm) in the tag field instead. That will show only messages whose tag doesn't start with "dalvikvm".
The below is notes about how this works; you can skip them if you're not interested. To start, you have to remember that the question, "Does this string match the regex?" really means, "Is there any position in this string where the regex matches?"
The tricky thing about (?!x) negative assertions is that they match wherever the next part of the string doesn't match x: but that's true of every place in the string "dalvikvm" except the start. The blog post you linked to adds a \b at the end so that the expression matches only at a place that's not just before "dalvikvm" and is a word boundary. But this would still match, because the end of the string is a word boundary, and it doesn't have "dalvikvm" after it. So the blog post adds the \w+ after it, to say that after the word boundary there have to be more word characters.
It works for exactly that case, but it's a bit of an odd way of making a regex, and it's relatively expensive to evaluate. And as you've noticed, you can't adapt it to (?!dalvikvm-heap\b)\b\w+. "-" is a non-word character, so immediately after it, there is a word boundary, followed by word characters, and not followed by "dalvikvm-heap", so the regex matches at that point.
Instead, I use ^, which only matches at the start of the string, along with the negative assertion. Overall, the regex only matches at the start of the string, and only then if the start of the string isn't followed by "dalvikvm". That means it won't match "dalvikvm" or "dalvikvm-heap". It's also cheaper to evaluate, because the regex engine knows it can only possibly match at the start.
Making the regex this way, you can filter out multiple tags by just putting them together. For example, ^(?!dalvikvm)(?!IInputConnectionWrapper) will filter out tags that start with "dalvikvm" or "IInputConnectionWrapper", because the start of the string has to not be followed by the first and not be followed by the second.
BTW, thanks for your link. I didn't realise that you could use the logcat filters that way, so I wouldn't have come up with my answer without it.
can any one know about how to add/insert emotions/smiles to text(which ever i typed in my edit text for my notes). i have little confusion about if i want to add these smiles of type .png in to edit text type of string, is it possible? and also i have to save these input into sqlite database. normally i know to store string data taken from edit text.
but along with that text i want to also add smiles symbols where ever my cursor placed and to be store in sqlite data base. and get it back to read.
so guys any ideas, most welcome!
Try to use java (i.e. android) spannable method to implement smiley (i.e.) images for that. You will surely get it, search in google for "how to add images/smiley with java spannable method in android?" you will get good idea.
Reading your question the first thing I can think of is Mapping each image to a sequence of letters, for example :) is smiley.png etc. Now your database also has these smaller representation However while reading from the database you can convert those special character sequences to appropriate image.
For simplicity in seraching those Strings You can wrap them in some less used characters i.e. [ or { or <.