TextView carriage return not working - android

I am reading a string from an xml file stored on the sdcard that has some carriage returns stored with the "\n" escape sequence. Such as
<string mystring="Line1\nLine2">
But when I display the text in the emulator, the \n is showing up instead of making a newline. I am not doing anything unusual with the text--just reading it with a SAXParser and then adding it to a textview. Is there a setting I need to check to make sure newlines are rendered correctly? Do I need to store the carriage returns differently in the xml file?
I have also tried \r\n and that doesn't work either.
When I debug, I can see that an extra \ is placed in front of the existing \
I see that in my SAX startElement method, the line
String s = atts.getValue("mystring");
assigns "Line1\nLine2" to s, so the problem is with the SAXParser.

works very well in the middle of a quoted textview. I'd like to add that in Eclipses' graphic viewer it puts in an extra space (like it is double spaced). When it is compiled however and you run it on the emulator or a device, you only have the carriage return. So, trust MrGibbage and you'll save yourself some time.

\n is actually a line feed character, while carriage return is \r. Try using both together, e.g. \r\n.
More info about newline.
For handling the whitespace with SAX take a look at ignorableWhitespace(..) method.

Well, I figured out an answer. I had to store the newlines in my xml file ampersand-hash-x-A-semicolon

Use a
where you need the carraige return.

Related

Password regex string in strings.xml file in android

I have declared a regex for password validation purposes in strings.xml file.
The criteria is
-should be atleast 8 characters
-should contain atleast one upper case letter
-should contain atleast one lower case letter
-should contain atleast one special character within these "##$%^+&="
So my whole regex looks like this now
^(?=.[0-9])(?=.[a-z])(?=.[A-Z])(?=.[##$%^+&=])(?=\S+$).{8,}$
But when I enter this, I get an error saying that & is
"Unescaped or non terminated character entity/reference"
So instead I used the escape sequence as & but the validation fails for &
I would b glad if anyone could help me out on this!!
Use * quantifers in the look-aheads. Right now, you check if 2nd character in the string meets your conditions. We need to test them all in the string.
^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^+&=])(?=\S+$).{8,}$
Here is a demo.
EDIT
Since the regex is located inside the XML code, it should be properly encoded. Or, use it inside CDATA block.
Are you missing a parameter in your curly braces? The last bit "{8,}" seems off.

Apostrophe converting to question mark in textview

I am parsing some external XML into an object and displaying this inside a textview.
Apostrophe's/single quotes are being converted to these silly question mark symbols.
Nothing i've found is working - i've tried using replaceall and escaping it with \', it doesn't give me the desired result.
I've tried setting the textview using:
tv.setText(Html.fromHtml(news_item.getTitle()));
It doesn't seem to work, I can't find any other solutions to this one, your ideas appreciated.
Try this:
tv.setText(news_item.getTitle().replaceAll("\u2019", "'"));
For other Unicode characters, please see this link.
Found it!
The mark you are looking for is called RIGHT SINGLE QUOTATION MARK with a unicode code of U+2019. This particular mark should be replaced via:
String.replace("’", "’");
for proper display.
If that doesn't work, you should do a substitution from that mark to a apostrophe via:
String.replace("’", "'");
or directly:
String.replace("’", "&apos;");
to make sure the display actually displays it.
Close up of the difference between right single quotation mark vs apostrophe: ’ vs '
The documented solution will work, but it is not the right way of fixing this, as the root cause of the problem is encoding. In your case, the source's (XML document) encoding is most likely UTF-8 or some other multi-byte encoding. Your parser or consumer of the data is most likely ISO-8859-1 or ASCII. These characters (right/left apostrophes) are not part of that character set. Therefore, the correct solution is to change the encoding of your parser/processor/consumer to UTF-8.
If this is not the case, then it is probably the opposite. You have a process that writes down characters in UTF-8, but the XML's encoding is not compatible (i.e. ISO-8859-1).
Remember this: ALL characters in ISO-8859-1 are mapped in UTF-8, but not the other way around. So going from ISO-8859-1 to UTF-8 is not a problem. The problem is when you have to make the round trip to ISO-8859-1 to UTF-8. When converting UTF-8 characters, those characters NOT in the ISO character set, will show up funny on your display; either as question marks or "’

Android - reading local html as text file: differences between using InputStreamReader.read and BufferedReader.readLine

I have to read an html as a text file and I have implemented two different methods as described on SO threads. I clean the html leaving just the body content to put it into DOM with document.write(string) with both methods, but it only works if I use Bufferedreader.readLine() (appending a stringBuilder in a loop). In fact, if I print the stringBuilder.toString() in the Logcat window I see that when BufferedReader.readLine() is used the stringBuilder is displayed on a single line in the Logcat window, while with InputStreamReader.read(charArray,0,(int)numBytes) it is displayed multi-line (I am using a real device). Maybe this can be related and suggest which is the issue. My question is : how have I to clean or process that char Array read by read(charArray,0,(int)numBytes) to be the same of the stringBuilder appended in the readLine() loop?
The BufferedReader.readLine() method reads till the end of the line and returns the value excluding the newline character. Hence when you append it to a StringBuffer everything gets appended to a single line. However, in the case of InputStreamReader.read, the newline character is also included, causing the string to be displayed in multiple lines.
To make the behavior similar, you will have to skip newline character while using InputStreamReader.read.

extracting strings from KML file

I am extracting strings from KML file, if the string contains special character like !, #, #, ', " etc. its using codes like '
I am not able to extract entire string if it is like above, by calling getNodeValue(). It is terminating the string at special character.
<name>Continue onto Royal's Market</name>
If i extract the string i am getting only ""Continue onto Royal". I want entire string as
Continue onto Royal's Market.
How to achieve this ?? If anybody familiar with this please reply to this one.
Thanks
Your problem has nothing to do with KML but is general for XML parsning:
Don't use getNodeValue(), as there is no guarantee in DOM that text isn't actually split over several nodes.
Try using getTextContent() instead.
You might also have to replace entities, as in: node.getTextContent().replaceAll("'","'");
In general I wouldnt use DOM at all for extracting data.
I'd use the XmlPullParser as its simpler to work with - and parses faster.

How to edit multiline strings in Android strings.xml file?

I have several cases where my string in strings.xml is quite long and has multiple lines done with \n.
Editing however is quite annoying since it is a long line in Eclipse.
Is there a better way to edit this so it looks like it will be presented later in the textview, ie the line breaks as line breaks and the text in multiline edit mode?
Two possibilities:
1. Use the Source, Luke
XML allows literal newline characters in strings:
<string name="breakfast">eggs
and
spam</string>
You just have to edit the XML code instead of using the nifty Eclipse GUI
2. Use actual text files
Everything inside the assets directory is available as a input stream from the application code.
You can access those file input streams of assets with AssetManager.open(), a AssetManager instance with Resources.getAssets(), and… you know what, here’s the Java-typical enormously verbose code for such a simple task:
View view;
//before calling the following, get your main
//View from somewhere and assign it to "view"
String getAsset(String fileName) throws IOException {
AssetManager am = view.getContext().getResources().getAssets();
InputStream is = am.open(fileName, AssetManager.ACCESS_BUFFER);
return new Scanner(is).useDelimiter("\\Z").next();
}
the use of Scanner is obviously a shortcut m(
Sure, you could put newlines into the XML, but that won't give you line breaks. In strings.xml, as in all XML files, Newlines in string content are converted to spaces. Therefore, the declaration
<string name="breakfast">eggs
and
spam</string>
will be rendered as
eggs and spam
in a TextView. Fortunately, there's a simple way to have newlines in the source and in the output - use \n for your intended output newlines, and escape the real newlines in the source. The above declaration becomes
<string name="breakfast">eggs\n
and\n
spam</string>
which is rendered as
eggs
and
spam
For anyone looking for a working solution that allows the XML String content to have multiple lines for maintainability and render those multiple lines in TextView outputs, simply put a \n at the beginning of the new line... not at the end of the previous line. As already mentioned, one or more new lines in the XML resource content is converted to one single empty space. Leading, trailing and multiple empty spaces are ignored. The idea is to put that empty space at the end of the previous line and put the \n at the beginning of the next line of content. Here is an XML String example:
<string name="myString">
This is a sentence on line one.
\nThis is a sentence on line two.
\nThis is a partial sentence on line three of the XML
that will be continued on line four of the XML but will be rendered completely on line three of the TextView.
\n\nThis is a sentence on line five that skips an extra line.
</string>
This is rendered in the Text View as:
This is a sentence on line one.
This is a sentence on line two.
This is a partial sentence on line three of the XML that will be continued on line four of the XML but will be rendered completely on line three of the TextView.
This is a sentence on line five that skips an extra line.
You may easily use "" and write any word even from other languages with out error:
<string name="Hello">"Hello
world!
سلام
دنیا!"
</string>

Categories

Resources