Converting text from EditText to html and back loses formatting - android

I have an EditText called content. Inside it is some formatted text. I then want to switch between seeing the formatted text and the html by doing :
if(!showHtml)
content.setText(Html.fromHtml(content.getText().toString()), BufferType.SPANNABLE);
else
content.setText(Html.toHtml(content.getText()));
If the formatted text is "test test", the html comes out as <p>test <b>test </b></p> which is fine, but when going back, the formatting is lost and I get "test test".
If the formatted text is "test test", the html comes out as <p><b>test</b> test</p>, which is correct once again. However, the text obtained when going back is "test test".
So what it looks like is that the formatting of the first word is applied to the rest of the text (I've tested it on longer strings).
Has anyone encountered this before, and how could I go about solving this?
Edit 1 It seems that with EditText.setText(), it automatically uses the Editable flag and completely ignores my request for a Spannable. Could this be where the problem is coming from?

I was being a fool. I had completely forgotten that I had a TextWatcher that modified the styles that were applied. All I needed to do was set a flag telling it not to modify the styles if it was after converting from html.

Related

how to handle TextView set text of "\\uxxxx" failed in android?

I've got a TextView in my app to put my data obtained from scraping.
the data is some chinese words (unicode), the view shows the unicode instead of these chinese words.
I've find out the problem is caused by "\uxxxx" and "\uxxxx". The system return the value of "\uxxxx".
The chinese words can be show if I hard code the string pass into it, for example
Title.setText("\u4F60\u597D\u55CE");
\\ the chinese words can show properly as "你好嗎"
Title.setText("\\u4F60\\u597D\\u55CE");
\\ the words show as "\u4F60\u597D\u55CE"
I try to compare the different:
Log.i("setTitle", String.valueOf(Title.equals("\u4F60\u597D\u55CE")));
//returned false but should be true
Log.i("setTitle", String.valueOf(Title.equals("\\u4F60\\u597D\\u55CE")));
//returned true but should be false
I've tried
Title.replace("\\\\u","\\u");
Title.replace("\\\\","\\");
these all provides the same result in my comparison code
I've even tried
Title.replace("\\","").replace("u", "\\u")
I still cannot get the result I want.
Just want to ask is there any way I can show chinese character with unicode in TextView.setText()?
Just replace all \\u with \u before setting it to the Textview and that should work.

android SPAN_EXCLUSIVE_EXCLUSIVE not working properly

I am trying to set span on a SpannableStringBuilder using flag SPAN_EXCLUSIVE_EXCLUSIVE and I am facing problem on further editing the text to which I am setting span.
Expected behaviour
1: Original text.
2: Text added before.
3: Text added after with space.
Unexpected Behaviour on adding text after styled text
I don't want the added text to be styled, and want to know what am I doing wrong.
EDIT 1:
The issue is happening on Moto X Play, but is not reproduced on Nexus 5X. Still testing on other devices.
You just probably add text not the way you should. Use .insert() and .append() methods of SpannableStringBuilder to add additional text.
I just tried what you try to achieve and here is the result:
TextView hratkyTextView = (TextView) findViewById(R.id.spannableHratkyTextView);
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
// "Test text" part (in bold)
SpannableStringBuilder builder = new SpannableStringBuilder("Test text");
builder.setSpan(bss, 0, builder.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
// Prepending "before" (non-bold)
builder.insert(0, "before");
// Appending " after_with_space" to the end of the string
builder.append(" after_with_space");
hratkyTextView.setText(builder);
Result:
Nexus 7 Emulator running MainActivity with this code
TL;DR: Using some IMEs, like Gboard, when adding a char directly after a word (without space) the IME will replace the whole word tric with trick instead of just appending the c.
Detailed asnwer: How IMEs work with editors.
How some IMEs dictate commands to editors
IMEs communicate with editors (e.g. EditText) through InputConnection interface where they can send commands following user input, and get current text.
Gboard IME works in the following way:
gets text before and after cursor
detects the currently "composing" word and asks the editor to highlight and remember it (usually results in the word being underlined - check screenshot below)
Being aware of the currently composing word enables many features like suggesting words or auto-correcting spelling.
Whenever a char is inputted by the user, Gboard will ask the editor to set the currently composing text to a new value, i.e. replace trick by tricky
After a space is inputted, Gboard will do a final replace of currently composing region, eventually auto-correcting spelling
Currently composing region is reset to the next word.
This unfortunately breaks what we would normally expect from SPAN_EXCLUSIVE_EXCLUSIVE.

font color android html

I am making a note application and want the user to be able to edit in rich text.
The user writes the note, then it gets converted into html and saved into a database.
This way when it is retrieved from the database, it does not lose its rich text.
Yet I am having a problem, if the user adds a color to their text and saves the note it converts it to html like this
"<p><font color=#0000ff>user text</font></p>"
when retrieved from the database no text color shows up. this is because android saves the html wrong. In order for android to get color from html the letters need to be capitalized like this
"<p><font color=#0000FF>user text</font></p>"
This confuses me because if android can only read it in caos why doesn't it convert it in that way.
How do I get it so when this code runs
//--save to string--//
Editable e = noteContent.getText();
String s2 = Html.toHtml(e);
Spanned s3 = Html.fromHtml(s2);
classes.setText(s3);
to save the text color to the caps. so when it is retrieved form the database the text color shows up.
Thanks,
Jordan
Make sure your RGB value is CAPITALIZED.
Try to use:
Html.fromHtml("<![CDATA[<font color='#145A14'>text</font>]]>");
Hope this help!

How to edit arabic string properly?

<string name="message">هذا المجلد يحتويى على %1$s ملفات. الرجاء التأكد قبل الحذف. الملفات المحذوفة غير قابلة للإسترجاع.</string>
I wanna put "%1$s" in an arabic string, but as you can see here, word, notepad++, utraedit, all failed to get the right string. how you guys edit arabic string?
In android Studio 2, RTL support is not turned on by default,
Configure it Manually:
1. In your Computer, go to the [android-studio2.0]/bins/idea.properties
2. add editor.new.rendering=true to the end of idea.properties
3. restart your android studio.
This is a source of frustration when editing mixed-direction text. What counts is the logical order of the text, not how any of the editors display it. When you finally format the string at run time in the app, the %1$s will be replaced by whatever string you pass to the formatting method. The only thing that matters is how the string will be rendered after the substitution.
The easiest thing to do is to write the message without the %1$s, then position the insertion caret, paste in the format code, and simply ignore how the editors screw up the bidi analysis. (The screw-up is because the editors are using a left-to-right base level. In some editors, you can set the base flow to right-to-left, but then the xml markup ends up being unreadable.)
When I work with RTL text and I need to put a place holder(%1$s) or LTR words, I just write it in MS Word and copy to the IDE.
It works for me in Eclipse and Android Studio.
All you need to check is that the components that displays that string like TextView has the right gravity.
You can try in Activity..
String formatedString = String.format("%1$s", getString(R.string.your_string));

is it possible to store a character as underlined within a sqlite database?

I am trying to create a database for an android app including, in part, non-English words which require underlines and accents for proper spelling. I set my encoding for this package to utf-8, which allowed the accented characters to store and display properly. However, I cannot seem to get a single character underlined. It displays an empty box for an unrecognized character.
An example of my database helper to create the sqlite is as follows:
cv.put(ENGLISH, "to be alive");
cv.put(NATIVE, "okch_á_a or okchaha");
cv.put(PART_OF_SPEECH, "verb");
cv.put(AUDIO, "alive");
cv.put(VIDEO, "none");
cv.put(IMAGE_DEFAULT, "none");
cv.put(IMAGE_OPTIONAL, "none");
cv.put(IMAGE_TO_USE, "none");
db.insert("words", ENGLISH, cv);
That
_ a _
is the best I can come up with so far, but the a should actually be an underlined character.
I tried html tags like u and /u:
<u>a</u>
since that works with string arrays, but it displays as:
<u>a</u>
(the html is never interpreted).
I tried using:
"\u0332"
as explained at http://www.fileformat.info/info/unicode/char/332/index.htm , but that, too, is never interpreted, so it displays as:
a\u0332
I also tried:
& # 818 ;
and:
& # x332 ;
in a similar manner, with similar lack of results.
Any ideas?
You can store your string in Html format and call .setText(Html.fromHtml(somestring)) from the textview were you want to display it.

Categories

Resources