Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to stop Linkify on Long-Press?

I am working on my project where I have a listView and each item is a LinaerLayout that has a TextView with Linkify hyperlink. So, when I press the an item in the List view, it opens a dialog, which is fine. When I press the linked text in the listView, it opens a dialog, which is fine. PROBLEM: When I LONG-PRESS the linked text in the Listview, it opens a dialog AND an activity of the given link at the same time! In this case, I only want it to open the dialog only. In other words, I want to ignore Linkify's hyperlink on Long press. Does anyone know how I can do this? I don't know where to apply LongPress attributs... Thanks in advance.

FYI, I tried the following but doesn't work.

public class URLSpanNoUnderline extends URLSpan implements OnLongClickListener {
    public URLSpanNoUnderline(String url) {
        super(url);
    }

    @Override
    public void updateDrawState(TextPaint textPaint) {
        super.updateDrawState(textPaint);

        textPaint.setUnderlineText(false);
    }

    @Override
    public void onClick(View v) {}

    @Override
    public boolean onLongClick(View v) {
        Log.d("log", "lonnnnnnnnnnnnnnnng click");
        return false;
    }
}
like image 326
user2062024 Avatar asked Apr 16 '13 21:04

user2062024


1 Answers

you need a longClick mark,set it when in textview longclicklistener,and in touchlistener,when action equals MotionEvent.ACTION_UP and longClick is true,return true。

textview.setOnLongClickListener(new View.OnLongClickListener() {
    @Override 
    public boolean onLongClick(View v) {
        isLongClick= true;
        return false;
    }
});

textview.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_UP && isLongClick){
            isLongClick= false;
            return true;
        }
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            isLongClick= false;
        }
        return v.onTouchEvent(event);
    }
});

this problem happend in some phone.

like image 174
afei Avatar answered Oct 13 '22 20:10

afei