Android Question EditText Behavior When Paste

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,

I noticed that when I use the copy method to copy/paste the text from an email or website to an EditText box I receive all formatting (underline, big fonts, bold) of the website/email text. But when I transfer this text to a string and later to other EditText object the fonts formatting is lost.
Question: is there anything that I could do to maintain the formatting? I noticed that WhatsApp messages boxes have different fonts and styles (links underlined, different font colors in the same object). How can I do that?
B4X:
Dim MiddleText as string

MiddleText = EditTextSource.text '<-this received a copy and paste text from an email
TargetText.text = MidleText   '<- the text lost formatting
 

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello,

to clarify the problem, I created a small app where I have a two textviews. I cut and paste a formatted text in the textview on the bottom of the screen. When clicking the button, the app copies the content from the second to the first textview (in the image you can see that the copied text is showed formatted in the bottom textview). After clicking the button, the first textview receives a non formatted content besides the source is formatted!
It looks that the .text method of textview object doesn't transport the entire content and so, if I have a database between the source and target, the things are worst because I really didn't find how is possible to recover the formatted content and recover later... even between two identical objects (two textviews)...
Please help!
 

Attachments

  • texttest.zip
    9 KB · Views: 286
  • Screenshot_20190216-221023.png
    Screenshot_20190216-221023.png
    78.5 KB · Views: 326
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
Sub GetCharSequenceFromEditText(et As EditText) As Object
 Dim jo As JavaObject = et
 Return jo.RunMethod("getText", Null)
End Sub

B4X:
EditText1.Text = GetCharSequenceFromEditText(EditText2)
Thanks @Erel , I'll try and post the results here.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
Sub GetCharSequenceFromEditText(et As EditText) As Object
 Dim jo As JavaObject = et
 Return jo.RunMethod("getText", Null)
End Sub

B4X:
EditText1.Text = GetCharSequenceFromEditText(EditText2)
Hello @Erel ... I tested and still have some doubts:
- In the sample test I transferred the text content from one TextView to other
- In a real program, I have the same problem but need also to store in a sqlite database to recover later on screen

I noticed that the return of GetCharSequenceFromEditText is an object, not a text or string, what means that maybe it is not possible to store it in a simple string field and, even if I build a routine to get the extra data from object, I really don't know how to rebuild this from a storage...
SO, the question: how can I do this sequence as follows:

- EditText2.text -> sqlite database -> EditText1.text

Notice that I'm not talking about how to create or store data in a sqlite database. The problem is how to parse and store the result of GetCharSequenceFromEditText to rebuild later in a new EditText view...

Thanks!
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Try this:
B4X:
Sub GetCharSequenceFromEditText(et As EditText) As Object
 Dim jo As JavaObject = et
 Return jo.RunMethod("getText", Null)
End Sub

B4X:
EditText1.Text = GetCharSequenceFromEditText(EditText2)
Hello @Erel ,

I done some research and noticed that the storage and rebuild of an spannable text in a EditText is a known problem in many Android forums, because normally we have to store the content in a string format (in a sqlite database or flat text), and then rebuild it in the original spannable format/object.
I found this article that uses Android html method to do this: https://stackoverflow.com/questions/21444635/android-spannable-text-save-store-issue
But I really don't know how to implement this in B4A. This resource could be very useful to community because creates an easy method to store and rebuild full formatted text on screen...
I'll continue trying here but if you could help me I'm sure that will be A LOT faster ;) - thanks!
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello @Erel ,

I finished the development and created a small java code based on https://stackoverflow.com/questions/21444635/android-spannable-text-save-store-issue that I think that could be very useful to community. What this does:
- Get a spannable text in a textview
- Converts it into html, that could be stored in a flat text, string, sqlite database
- Converts again to a spannable text from html. This object can then be loaded into a text in a label ou textview and will rebuild the original formatting

LIMITATIONS:
- The alignment is lost then converting to html (an Adroid limitation of toHtml method)
I'm sharing the app here and the java code. I hope that this can help the community ...

B4X:
Sub Button1_Click
   
    Dim Jo As JavaObject
    Jo.InitializeContext

    Dim Cs As CSBuilder
    Cs.Initialize.Append(GetCharSequenceFromEditText(EditText2))
    Dim Html As String

    Html = Jo.RunMethod("toHtml",Array(GetCharSequenceFromEditText(EditText2)))

    EditText1.Text = Jo.RunMethod("fromHtml",Array(Html))

End Sub

Sub GetCharSequenceFromEditText(et As EditText) As Object
   
    Dim jo As JavaObject = et
    Dim Test As Object
    Test = jo.RunMethod("getText", Null)
    Return jo.RunMethod("getText", Null)
   
End Sub

#If Java
import android.widget.TextView;
import android.text.Html;
import android.text.Spannable;
import android.text.Spanned;

    public static String toHtml(Spannable sourceText) {
       
        String genHtm = Html.toHtml(sourceText);
        return genHtm;
       
    }
   
    public static Spanned fromHtml(String htmlString) {
   
         Spanned returnView = Html.fromHtml(htmlString);
        return returnView;
    }
   
#End If
 

Attachments

  • spannabletohtml.zip
    9.4 KB · Views: 310
Upvote 0
Top