How to extract HTML styled text from an EditText in Android? - android

I am using HTML.fromHTML(...) to style the text of an EditText in Android. I need to pass the styled text back as a result to another activity. However, when I use an intent to pass the contents of the EditText I am unable to figure out how to retain the HTML style of the original text.
As an example, suppose that the original text in the EditText is:
Today is the 21st
When I extract the text of using edittext.getText() and send it back as a result the resulting text is:
Today is the 21st
Is there a way to extract the HTML styled string from the EditText?

Use this to get the HTML of the styled text. You can use the HTML in EditText, TextView or WebView
String htmlString=Html.toHtml(edittext.getText());

You can send the HTML text itself and then call Html.fromHTML in the activity to which you are passing this text. fromHTML is meant to be used for text which has to be displayed on the screen

This won't work if your text is not spanned:
edittext.setText("");
//error here:
String htmlString = Html.toHtml((Spanned) edittext.getText());
You need to cast it by creating an instance first:
String htmlString = Html.toHtml(new SpannableString(edittext.getText()));

Related

Extracting and replacing text with a Spannable clickable

#mention functionality like whatsapp
This is a message for :
#9170211268628 hi how are you
Expected :
#Raviraj hi how are you
In onBindViewHolder of your RecyclerView.Adapter<T> implementation, use a SpannableStringBuilder to create a new spannable string with the replaced text then call setText on the text view.
you want to remove number and comma,
first of all you find out index of comma and then separate string using substring(stratindex,endindex) then put name of user after #.

How do I cut blank lines from EditText using XML that the unnecessary lines aren't shown in TextView

I have an EditText where I put a message in. Let's say I write a message like "Hi" followed by lot of blank lines, then the ChatBubble wraps in the blank lines as well. How can I remove them without converting the Message Object to a String? Is there a way using XML only?
You could solve your problem by first taking the text that you enter in the EditText and save it to a String variable, like so:
String message = String.valueOf(editText.getText())
Then you could simply replace any new lines (or any text that you are looking for) in that String variable.
newMessage = message.replace("\n", "")
Replace the "\n" with whatever you want to replace. Using the newMessage variable, create your ChatBubble and display the text you want. Hope it helps!

TextView: set Html and own SpannableStringBuilder

I have a string that contain html tags. So for set text into TextView I use:
textView.setText(Html.fromHtml(myString));
It's works good.
Also I have own method convertText() with type SpannableStringBuilder. In which method I convert text, where I have something like [text="information"]. I use next for set text:
textView.setText(convertText(myString), TextView.BufferType.SPANNABLE);
For example: myString = "<h1> This is [text="information"]. </h1>"
The result must be:
This is INFORMATION. (with h1 formatted text. caps lock of word information works in method convretText() good)
It's also works good. But how I can use these two setText approach in one time?

How to highlight characters in String for Textview?

String = " This is SAMPLE"
I want to increase the size of the SAMPLE alone in String to highlight, but need to be in a single string, this is to set in TextView.
Thanks in Advance.
textView.setText(Html.fromHtml("This is <font color='#707070' size='20'>
SAMPLE</font>"));
Try this:
String str = "This is <b>SAMPLE</b>";
yourTextView.setText(Html.fromHtml(str), BufferType.SPANNABLE);
Moreover, you may decorate your string (str) with html tags to alter the looks.
Or to highlight a part of text without using html stuff, read this
Use Html.fromHtml() in setText() of TextView. Supported Html Tags can be found here Html Tags Supported by TextView
I think this is possible if you pass the text as html with your highlighted part enclosed in html <bold> tag.
You have two options:
1, Format your string in HTML and use HTML.from("Your text");
2, Use spannable. A complete guide can find link here

Editable text to string

How can I convert editable text into string in Android ? Any solution?
If I understand correctly, you want to get the String of an Editable object, right? If yes, try using toString().
Based on this code (which you provided in response to Alex's answer):
Editable newTxt=(Editable)userName1.getText();
String newString = newTxt.toString();
It looks like you're trying to get the text out of a TextView or EditText. If that's the case then this should work:
String newString = userName1.getText().toString();
This code work correctly only when u put into button click because at that time user put values into editable text and then when user clicks button it fetch the data and convert into string
EditText dob=(EditText)findviewbyid(R.id.edit_id);
String str=dob.getText().toString();

Categories

Resources