How to respect breaks of text lines copied to the clipboard

0

Good day, I have a small code that manages to copy a text that is inside a webView which loads a web page. The problem is that I do not get that when extracting the text from the clipboard this does not respect the line breaks.

final ClipboardManager clipboard = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
        clipboard.addPrimaryClipChangedListener(new ClipboardManager.OnPrimaryClipChangedListener() {
            @Override
            public void onPrimaryClipChanged() {
                ClipData clipData = clipboard.getPrimaryClip();

                ClipData.Item item = null;
                if (clipData != null) {
                    item = clipData.getItemAt(0);
                    if (!mPreviousText[0].equals(item.getText().toString())){

                        mPreviousText[0] = item.getText().toString();
                        myEditText.setText(mPreviousText[0]);

                    }
                }
            }
        });

assuming that the text you are copying is as follows:

"A word from the translator to his friends

I offer

naked, virgins, intact and simple. "

what it returns to me would be:

"A word from the translator to his friends I offer naked, virgins, intact and simple."

And I do not know what is due, thank you in advance.

    
asked by Mau Ü Garcia 05.01.2019 в 05:29
source

1 answer

0

Knowing that you are extracting the text from a WebView, surely the line breaks are represented as <br>

   "Una palabra del traductor a sus amigos
    <br>
    Yo ofrezco
    <br>
    desnudas, vírgenes, intactas y sencillas."

Therefore I suggest you make a replacement of the line breaks in html by \n using the .replace() method:

.replace("<br>","\n")

This would be the way to do it in your code:

item.getText().toString().replace("<br>","\n")
    
answered by 07.01.2019 в 05:49