Multiline text in EditText with links, both clickable and editable

177 views Asked by At

I am working with multiline EditText view which can contain webUrl in input. For that I am using LinkMovementMethod make links in the EditText clickable.

I have the same problem as the question EditTexts with links both clickable and editable, but also I have multiline text.

Problem is that:

If the last part of string is a link, clicking anywhere causes the link to be opened.

Extensions:

When I am clicking in current area of EditText, It will be editable. https://i.stack.imgur.com/DBKAX.png

Example:

XML layout:

    <EditText
    android:id="@+id/description"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:autoLink="web"
    android:gravity="start"
    android:hint="@string/Event_Description"
    android:inputType="textCapSentences|textMultiLine"
    android:minLines="12"
    android:textColorLink="@color/link_color"
    android:textIsSelectable="true" />

Code example:

    etDescription = findViewById(R.id.description);
    etDescription.addTextChangedListener(descriptionTextWatcher);
    etDescription.setMovementMethod(LinkMovementMethod.getInstance());
    
private TextWatcher descriptionTextWatcher = new TextWatcher() {
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

    }

    @Override
    public void afterTextChanged(Editable s) {
        Linkify.addLinks(s, Linkify.WEB_URLS);
    }
};
1

There are 1 answers

0
Pavel On

Temporally I realize LongClick reaction, but I find this solution awkward, and I want to make it better.

Code example:

etDescription.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View view) {
        etDescription.requestFocus();

        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.showSoftInput(etDescription, InputMethodManager.SHOW_IMPLICIT);

        return true;
    }
});