I am writing my data in a database and getting it in android to set it in a textview but when I write for example:
"hello,
how are you?"
which means each line on a row if the row in the phone completes I get an extra line space and I don't want this need to remove it how can I do that?
thanks.
EDIT:
how can I place a line space in an arabic database: I added \n didn't work "\n" didn't work '\n' didn't work!! any ideas?
even if the database wasn't arabic that didn't work so can I place the \n in the database in a way that the android program takes it as a line space?
Edit2
Is there any other method? other than the one that I've mentioned in my answer? thanks.
check to see if you insert to your DB with that extra line.
showing more code would help us understand whats causing your issue...
but if you want to brute force it you could always use substring() for each or your elements or
`String input = EditTextinput.getText().toString();
input = input.replace(" ", "");`
but like #Nitin Sethi said more code especially the entry to your DB would give us a better understanding of what's going on
so what I did is that I've added the word "line space" in my database and I added
in my android code:
String input = //here I take the cde from cursor;
input = input.replace("line space", "\n");
and it worked perfectly but is there another way?
Related
I have following data in my db row:
Espresso: 7,00
Double Espresso: 8,00
Ristretto: 7,00
Espresso Machiato: 8,00
Espresso Con Panna: 8,00
I write it on Word, then copy & paste to MySQL editor. When I save it, my IOS and Android apps cannot show the prices aligned because of the tab characters.
What is the best way to do that?
The best way- those 2 things are different data and are in different columns in your database, I would expect (if not, you need to fix your schema). So put the 2 strings in separate TextViews, and align the text view in xml.
First you can split the data from tab character and use formatting like below in java. I believe in objective-c it should be similar.
String ehe = String.format("%-20s : \t %4dTL \n","ehemehe",23);
String ehe2 = String.format("%-20s : \t %4dTL \n","ehemeheadawd",44);
System.out.println(ehe);
System.out.println(ehe2);
Output it produces is
ehemehe : 23TL
ehemeheadawd : 44TL
replace the "/t" chars with "" before you display the text. by using
String.replace("/t","");
Use a fixed-width font if you have precalculated the number of spaces.
I am trying to print a text which is fetched from the server. What is the best way to escape all special characters and print safely?
Because the string which is fetched from the server is entered by user an stores it on database. So there is a possibility use <?php ?> , & etc which may cause errors. I have tried < > which solved this problem.
But when setText() the string to an EditText the string gets truncated after &
So I need a best solution in which the text entered by the user will save safely in the database and retrieve the multi-line string with special characters safely.
What is the best way to do this?
I think you should use StringBuilder instead of simply reading the text from XML.
Follow these steps :
1) For each new tag in XML (in startElement) , create a String builder
2) Append the text to same StringBuilder in reading from Character method.
3) At last, assign that StringBuider to some String at the End of the tag (in EndElement).
Hope this will give you some idea to solve the problem .
Try ...!!!
Anyways the problem solved by using URLDecode.decode() at the app and urldecode(),stripslashes() at the server side.
Dont know whether this is the perfect solution, but it worked for me.
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 <.
I have had a problem with formattings symbols shown on the image. I got help from this site and I used Html.fromHtml() and hope that it would use the formatting, the only thing it did was to remove the formatting symbol but not anything else like newline or so.
If I cant find a way to use the formatting I wonder if it is possible to add a "\n" everytime the symbol is shown. The thing is if I try to use a method like
(Ps the "[]" is used here instead of the real symbol cause I cant find out how to write it.
int nr=theTextString.indexOf("[]")
and then replace the text with
theTextString=theTextString.substring(0,nr)+ "\n" + theTextString.substring(nr);
but the problem is that the symbol is not represented as a character to look for the index at, not in any way I know of. Would really be thankful for help.
If [] crap character is only shown on the last chacter do this
Log.i("My String before:", theTextString);
theTextString = theTextString.substring(0, theTextString.length()-2);
Log.i("My String after:", theTextString);
Btw: [] <-- is not the same character (Infact, there are two characters) it is something else.
Solution: To encode the string
theTextString = URLDecoder.decode(theTextString, HTTP.UTF-8);
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.