How to format a string for a textview android - android

I have a TextView and when I call .setText(string), it displays the string exactly as it is.
For instance, if the string contains " ' " , ' will be displayed.
How can I fix this?

Looks like your outside source already does the convertion from ' to ' since regular single quote will be displayed just fine in a TextView. What you can to do in this case is:
yourTextView.setText(Html.fromHtml(stringSource));
where stringSource is your string that comes from outside source. This will take care of more than just the single quote character.

The single quote ' in java needs to be escaped using a backslash (\). You can find other characters that need to be escaped here.

Related

RegEx for Find and Replace not escaped apostrophes in Android Studio IDE

I'm trying to make a RegEx to find and replace all non escaped apostrophes in Android Studio.
So far I've came up with this:
[^\][']
This will select an apostrophe and the char to the left of it, if it's not a backslash (i.e. if it is not escaped already!).
The problem is that if I have a word " d'effectuer " it will put the cursor on " d' ", but I don't want to replace the letter to the left of the apostrophe. It should find the same words but the selection should be only on the apostrophe itself so I can then replace it with " \' ". Could this be done somehow?
Note: I also tried doing this in Notepad++
with >'\< and then replacing with \' and it doesn't work for some reason. The find actually works correctly but when I hit the replace button it doesn't replace anything...
Try enclosing the substring that you want to keep within parentheses to form a capturing group:
([^\])[']
Then you can reference it using $1 when doing the replacement, e.g. replace it with $1replacement where replacement is whatever will replace the apostrophe.
Without RegEX- We can directly remove ' symbol.
String data ="d'effectuer";
String a=data.trim().replace("'","");
The regex [^\w ] will match anything that is not alphanumeric or space. So you can use the [^\w] for Replace apostrophe .
I faced this same issue. I found that the easiest thing to do is
Find all the escaped characters (\') and replace them the not escaped version (') - gets you to having no correctly escaped characters
Find those not escaped characters (') and replace them with the escaped version (\') - now you can escape all characters at once
Simple workaround for a task you don't want to devote a lot of time to

Android Return Line (\n) in String

In android, a String resource can be created in XML by using
<string name="name">data</string>
to create a new line, \n may be used. However, if the user personally inputs data into an EditText and includes a \n, the writing is saved to a String and when the writing is displayed again in a TextView the result would be something such as:
Line 1\nLine2
How come the \n doesn't apply to user written Strings?
The end goal for me is to be able to have the user type a \n into a String to make sure the String is displayed in multiple lines.
How would this be achieved? Thanks in advance!
Use this :
TextView view = your text view;
view.setSingleLine(false);
view.setText("first line" + "\n" + "second line" + "\n" + "third line");
How are you doing setText() in the textView.
Try this...
TextView view = your text view;
view.setSingleLine(false);
view.setText("first line\n"+"second line\n"+"third line");
with
System.getProperty("line.separator")
you get a String which results in a new line.
I would replace \n by this expression.
The text entered in an input field is used as an actual text. That means, when the user enters the Hello \n World and the code calls getText().toString(), what will actually be returned is: Hello \\n World that means, it's the actual slash symbol and not the "carriage return" symbol.
I don't believe android has any built-in API to give you the actual code typed by the user, that means, that you have to write that code yourself. If all you need is the \n as a carriage return it is simple:
getText().toString().replace("\\n", "\n");

how to set apostrophe in TextView programming

TextView.setText(String with ') , i wanna set a text to the text view but this text keep coming uncompleted in case it has Space or apostrophe
i have tried to use
String SomeString="MacDonald's";
or
SomeString="Fast Food";
and i tried the following
HTML.Fromhtml(SomeString).tostring()
and
SomeString.replace("'","\\\'")
but with no good result
the Result always
MacDon
Fast
any good ideas ?!
Make sure your apostrophe is ' and not special one like ‘
From android developer documentation
you can set apostrophe from XML
Single quote (')
Any of the following:
1. &apos;
2. \'
3. Enclose the entire string in double quotes ("This'll work", for example)
Sample
1. <string name="travelers_details">Traveler&apos;s Information</string>
2. <string name="travelers_details">Traveler\'s Information</string>
Apastrophe cannot be directly displayed. Instead of directly placing Apastrophe use the below when you encounter the apastrophe as below:
if (YourString.contains("'")) {
YourString= YourString.replaceAll("'", "\'");
}
This will help you
I believe, calling yourTextView.setText("MacDonald's"); will simply set MacDonald's inside your TextView. You don't need anything fancy to get it printed.

How to insert a new line in strings in Android

I'm creating an android application and within it there is a button that will send some information in an email, and I don't want to have everything all in one paragraph.
Here's what my app is doing for the putExtra for the email's body:
I am the first part of the info being emailed. I am the second part. I am the third part.
Here's what I want it to do:
I am the first part of the info being emailed.
I am the second part.
I am the third part.
How would I put a new line into a string or with the putExtra method to accomplish that?
Try:
String str = "my string \n my other string";
When printed you will get:
my string
my other string
Try using System.getProperty("line.separator") to get a new line.
I would personally prefer using "\n". This just puts a line break in Linux or Android.
For example,
String str = "I am the first part of the info being emailed.\nI am the second part.\n\nI am the third part.";
Output
I am the first part of the info being emailed.
I am the second part.
I am the third part.
A more generalized way would be to use,
System.getProperty("line.separator")
For example,
String str = "I am the first part of the info being emailed." + System.getProperty("line.separator") + "I am the second part." + System.getProperty("line.separator") + System.getProperty("line.separator") + "I am the third part.";
brings the same output as above. Here, the static getProperty() method of the System class can be used to get the "line.seperator" for the particular OS.
But this is not necessary at all, as the OS here is fixed, that is, Android. So, calling a method every time is a heavy and unnecessary operation.
Moreover, this also increases your code length and makes it look kind of messy. A "\n" is sweet and simple.
I use <br> in a CDATA tag.
As an example, my strings.xml file contains an item like this:
<item><![CDATA[<b>My name is John</b><br>Nice to meet you]]></item>
and prints
My name is John
Nice to meet you
If you want to add line break at runtime into a String from same string you are deriving the value then this Kotlin code works for me:
str = "<br>"+str?.replace("," , "</br><br>")+"</br>"
value = HtmlCompat.fromHtml(${skill_et_1}",Html.FROM_HTML_MODE_LEGACY)
tv.text = value

New Line character \n not displaying properly in textView Android

I know that if you do something like
myTextView.setText("This is on first line \n This is on second line");
Then it will display properly like this:
This is on first line
This is on second line
When I store that string in a database and then set it to the view it displays as such:
This is on first line \n This is on second line
Here is the line of code I use to extract the string from the database:
factView.setText(factsCursor.getString(MyDBAdapter.FACT_COLUMN));
I simply populate the database from a text file where each line is a new entry into the table so a line would look like this "This is on first line \n This is on second line" and it is stored as text.
Is there a reason that it isn't displaying the \n characters properly? It must be something to do with the string being in the database. Any suggestions?
I found this question Austyn Mahoney's answer is correct but here's a little help:
private String unescape(String description) {
return description.replaceAll("\\\\n", "\\\n");
}
description being the string coming out of your SQLite DB
As Falmarri said in his comment, your string is being escaped when it is put into the database. You could try and unescape the string by calling String s = unescape(stringFromDatabase) before you place it in your TextView.
As a side note, make sure you are using DatabaseUtils.sqlEscapeString() on any kind of data that is from the user or an unknown changeable source when inserting data into the database. This will protect you from errors and SQL Injection.
Try \\n instead of \n. If it throws an exception than use newline keyword in place of \n....newline is one character, ascii 10; it's often entered in a string literal...and will serve your purpose....:)
"This is on first line"||x'0A'||"This is on second line"
The || concatenates strings and the x'0A' is an unescaped newline.
If you're inserting records you'll have to replace every newline with "||x'0A'||" (If your string is double quoted). This may seem clumsy compared to the other asnswers. However if your lines are in separate columns this also works in a select:
SELECT firstline||x'0A'||secondline FROM wherever;
I found this while having the same problem you are: http://www.mail-archive.com/sqlite-users#sqlite.org/msg43557.html
A text area can be in multi line or single line mode. When it is in single line mode newline characters '\n' will be treated as spaces. When in doubt, to switch multi line mode on you can use the following code:
setInputType(getInputType() | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
I had the problem that the same code did not work on honeycomb and on froyo, which seem to have different defaults. I am now also excluding the flag when I want to force a field to be single lined.
From the Android doc:
public static final int TYPE_TEXT_FLAG_MULTI_LINE Added in API level 3
Flag for TYPE_CLASS_TEXT: multiple lines of text can be entered into
the field. If this flag is not set, the text field will be
constrained to a single line. Constant Value: 131072 (0x00020000)
http://developer.android.com/reference/android/text/InputType.html#TYPE_TEXT_FLAG_MULTI_LINE
You have to set the flag before you populate the field.

Categories

Resources