dimanche 10 juillet 2016

How to extend Selection to a whole word in EditText?

So, I have an EditText view and my idea is on a, lets say for now, touch event to select and copy the touched word into clipboard memory. So far I can set Selection through offset, but it is only "", because getSelectionStart() = getSelectionEnd() = offset. How can I extend it until the next encountered whitespace or end/start of text.

How could I extend one such selection in the above described manner?

Code so far:

@Override
public boolean onTouch(View v, MotionEvent event)
{
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        float x = event.getX() + getScrollX();
        int offset = getOffset(event);
        if(offset != Integer.MIN_VALUE)
            if(x > getLayout().getLineMax(0)) {
                setSelection(offset);
                String selectedText = getText().toString()
                            .substring(getSelectionStart(), getSelectionEnd());

                   //TODO: extend selection otherwise copies ""

                putInClipMemory(selectedText);
            } else {
                setSelection(offset - 1);
                String selectedText = getText().toString()
                            .substring(getSelectionStart(), getSelectionEnd());

                   //TODO: extend selection otherwise copies ""

                putInClipMemory(selectedText);
            }
    }
    return true;
} 

Thanks.

Aucun commentaire:

Enregistrer un commentaire