user1206401
user1206401

Reputation: 101

Android: How to make Linkify links not long-clickable

I have some TextViews that I use Linkify on. I'd like for the links to not be long-clickable. Using setLongClickable(false) on the TextViews doesn't have any affect on the Linkify links. Is there any way to make these links not long-clickable?

Upvotes: 0

Views: 2858

Answers (5)

konmik
konmik

Reputation: 3210

If you need it to be still clickable but not long-clickable, consider overload activity's dispatchTouchEvent to listen for ACTION_UP and set setOnTouchListener in your TextView to listen for ACTION_DOWN. Then just route these events directly into MovementMethod.

This way you'll be able to only get clicks for your links while having the text behavior correct in all other cases (background onClick animation, clickable in other textview areas, etc).

Yeah, don't forget to call textView.setMovementMethod(null) when installing the listener.

Upvotes: 0

Alex Yermolenko
Alex Yermolenko

Reputation: 47

Here is a little class I wrote for this case:

public class NoLongClickMovementMethod extends LinkMovementMethod {

long longClickDelay = ViewConfiguration.getLongPressTimeout();
long startTime;

private static NoLongClickMovementMethod linkMovementMethod = new NoLongClickMovementMethod();

@Override
public boolean onTouchEvent(android.widget.TextView widget, android.text.Spannable buffer, MotionEvent event) {
    int action = event.getAction();

    if (action == MotionEvent.ACTION_DOWN) {
        startTime = System.currentTimeMillis();
    }

    if (action == MotionEvent.ACTION_UP) {
        long currentTime = System.currentTimeMillis();
        if (currentTime - startTime >= longClickDelay)
            return true;
    }
    return super.onTouchEvent(widget, buffer, event);
}

public static android.text.method.MovementMethod getInstance() {
    return linkMovementMethod;
}

Usage: textView.setMovementMethod(NoLongClickMovementMethod.getInstance());

Upvotes: 0

user1206401
user1206401

Reputation: 101

Turns out, the solution was quite simple:

textview.setOnLongClickListener(new OnLongClickListener(){
    public boolean onLongClick(View v) {
        return true;
    }
});

Upvotes: 4

Sniper
Sniper

Reputation: 2452

try this...

    Link.setOnLongClickListener(new OnLongClickListener() {
        public boolean onLongClick(View v) {
            Link.setLongClickable(false);
            return false;
        }
    });

    Link.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            if (!Link.isLongClickable()) {
                Link.setLongClickable(true);
                return;
            }

            //  Link your page 
        }
    });

Upvotes: 1

Vishal Pawar
Vishal Pawar

Reputation: 4340

this will be helpful to you

Spannable span = Spannable.Factory.getInstance().newSpannable("my span text"); 
             Linkify.addLinks(span, Linkify.ALL);
         URLSpan[] uspans = span.getSpans(0, mTextAdData.length(), URLSpan.class);

    int i = 0;
            for (URLSpan uspan : uspans) {
                int start = span.getSpanStart(uspan);
                int end = span.getSpanEnd(uspan);
                span.removeSpan(uspan);
                MyUrlSpan uspan1 = new MyUrlSpan(uspan.getURL());
                span.setSpan(uspan1, start, end, 0);
                if (i != start) {
                    // From i to start of this span set a custom span
                    span.setSpan(new MyUrlSpan(), i, start,
                            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                }
                i = end;
            }

    public class MyUrlSpan extends URLSpan implements OnLongClickListener {
                public MyUrlSpan(String string) {
                    super(string);
                }

                       @Override
                       public void onClick(View v){
                       }

                       @Override
                public boolean onLongClick(View v) {
                    return false;
                }
        }

Upvotes: 4

Related Questions