I have an EditText
view in my Android app. I need "inner links" in it, this means that I need some buttons or span inside EditText
and with onClick
to this button I can do some actions (not redirect to web page).
I realized this buttons with ClickableSpan()
like this
linkWord = "my link";
link = new SpannableString(linkWord);
cs = new ClickableSpan(){
private String w = linkWord;
@Override
public void onClick(View widget) {
wrd.setText(w);
}
};
link.setSpan(cs, 0, linkWord.length(), 0);
et.append(link);
For make this span clickable I used
et.setMovementMethod(LinkMovementMethod.getInstance());
"Inner links" works fine, but after using et.setMovementMethod()
copy and paste items are disable on OnLongClick
menu. And this is a problem, because I need "links" in EditText
and copy text from this view in the same time.
I have idea to set in listener OnLongClickListener
something like removeMovementMethod()
for temporary disable "links" function and use menu with copy/paste and after coping text switch on setMovementMethod()
method again. But I don't know how to realize this.
Can you help me? You may be there are some another ways...
Thank you!
I solved this problem and may be this will be interesting for someone...
For clickable links inside EditText I used
in this case in longClick menu there are not copy/paste items. For activate them I need back to normal EditText state, I can do it with:
After this method links will not work but appear normal longClick menu.
Therefore I added new item to the context menu and switched between this two options:
Also I registered EditText for context menu:
Have a hope that this will help someone!
I don't think that having the user switch between link and copy mode will win you a usability prize. My solution allows you to select text and open the links at the same time. To achieve this I simply extend ArrowKeyMovementMethod, which allows to select text, and add the onTouchEvent() method from the LinkMovementMethod which handles the clicking/touching of links. There's but one line of code that needs to be changed, which is the one removing the selection from the TextView when no link could be found at the coordinates the screen was touched.
Here's the complete class:
Doing this is pretty safe even if the documentation states:
The code above extends a documented class rather than implement the interface. All it does is adding a check to see if a link was tapped and otherwise uses the super class methods.