0

I'm trying to create a small strikethrough application for myself. The idea is simple: select the text, select the MStrike option in the floating menu that appears and then continue typing.

Except that after I press MStrike the still remains selected. Yet, while pressing other options (such as 'Copy') the text doesn't remain selected. I've tried searching Google, StackOverlow and Reddit but I didn't get any answer.

Here are some parts of my code if it's of any help:


public class MainActivity extends Activity {
    public String editedText;
    public boolean readOnly;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Intent intent = getIntent();
        if (intent.getAction() != null && intent.getAction().equals(Intent.ACTION_PROCESS_TEXT)){
            String selectedText = intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT);
            readOnly = intent.getBooleanExtra(
                    Intent.EXTRA_PROCESS_TEXT_READONLY,
                    false);
            //function to Strikethrough the text
            editedText = editText(selectedText);
        }

        finish();
    }

    @Override
    public void finish(){
        if (readOnly){
            ClipboardManager clipboardManager =
                    (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
            ClipData clip = ClipData.newPlainText(
                    "StyleText",
                    editedText
            );
            clipboardManager.setPrimaryClip(clip);
        }
        else {
            Intent back = new Intent();
            back.putExtra(Intent.EXTRA_PROCESS_TEXT, editedText);
            setResult(RESULT_OK, back);
        }
        super.finish();
    }
}

p.s. I'm still a beginner so my code might be not well-written. I'm open to any suggestions you have.

maaryus
  • 1
  • 1
  • I would imagine that that's the intended behavior, but I don't know that it's documented anywhere one way or the other. Those other operations like copy and paste happen directly, within the same app. Though your `MainActivity` is immediately finishing itself without presenting a UI, other apps handling `PROCESS_TEXT` may need the user to view or interact with their `Activity`, in which case the user would be away from the originating app for a moment, and it makes sense to leave the selection highlighted, from a UX perspective anyway. FWIW, I observe the same thing. – Mike M. Aug 21 '21 at 15:50
  • That is the intended behavior, it seems, [as far as the code goes anyway](https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/widget/TextView.java#2132). Upon receiving the result, `TextView` calls `replaceSelectionWithText()` which uses [`Editable#replace()`](https://developer.android.com/reference/android/text/Editable#replace(int,%20int,%20java.lang.CharSequence,%20int,%20int)), its docs stating: "Existing spans within the Editable that entirely cover the replaced range are retained". The selection is such a span, and is therefore retained. – Mike M. Aug 23 '21 at 02:22

0 Answers0