Search code examples
androidandroid-studiocopy-paste

Android Studio : How to copy a text from TextView or EditText in a single Line


I am making an application that encrypt a text and copy it automaticly.

Here is the XML :

  <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <EditText
            android:id="@+id/txt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter Text To Encrypt or To Decrypt"
            android:inputType="text" />

        <EditText
            android:id="@+id/txtkey"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter KEY"
            android:inputType="text" />

        <EditText
            android:id="@+id/txtsalt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Enter SALT"
            android:inputType="text" />

        <EditText
            android:id="@+id/txtres"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="Result"
            android:clickable="false"
            android:inputType="none"
            android:singleLine="true" />

        <Button
            android:id="@+id/btnencrypt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Encrypt" />

        <Button
            android:id="@+id/btndecrypt"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Decrypt" />

        <Button
            android:id="@+id/btnclear"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Clear" />

    </LinearLayout>

Here is the Btnencrypt code which it does encryption + copy to the encrypted text

        btnencrypt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String encrypted = Encrypt(txt.getText().toString(), txtkey.getText().toString(),txtsalt.getText().toString());
            txtres.setText(encrypted);
            ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText("label", txtres.getText().toString().trim());
            clipboard.setPrimaryClip(clip);
            Toast.makeText(getApplicationContext(), "Encrypted Text Copied", Toast.LENGTH_SHORT).show();
            txt.setText("");
        }
    });

when I past the encrypted text into Notepad++ , it takes a weird format : 2 lines of text and one empty line. What I want is to past the whole text in a single line , see the image bellow

http://hpics.li/e355430

EDIT:

By Adding .trim() , The useless line is deleted but Im still having 2 lines of text instead of a single line

btnencrypt.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String encrypted = Encrypt(txt.getText().toString().trim(), txtkey.getText().toString().trim(),txtsalt.getText().toString().trim());
        txtres.setText(encrypted.trim());
        ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
        ClipData clip = ClipData.newPlainText("label", txtres.getText().toString());
        clipboard.setPrimaryClip(clip);
        Toast.makeText(getApplicationContext(), "Encrypted Text Copied", Toast.LENGTH_SHORT).show();
        txt.setText("");
    }
});

Solution

  • Can you not simply strip out the formatting?

    ClipData.newPlainText("label", textView.getText().toString().replaceAll("\n", "").trim());