OnClickListener for TextView does not get called

741 views Asked by At

I have a couple of text links in my Android applications which are either email addresses or web links. I use the following code snippet to set them up (example for a web link):

TextView textView = (TextView) findViewById(textViewId);
spannedText = Html.fromHtml("<a href=\"" + url + "\">" + titleText + "</a>");
textView.setText(spannedText, TextView.BufferType.SPANNABLE);
textView.setMovementMethod(LinkMovementMethod.getInstance());
textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Handle click
    }
});

The TextView has no special attributes set such as android:linksClickable, android:clickable or android:focusable. Its style inherits from @android:style/TextAppearance.Small.

For some reason the OnClickListener never gets called.

3

There are 3 answers

1
SorryForMyEnglish On BEST ANSWER

remove this code

textView.setMovementMethod(LinkMovementMethod.getInstance());

and manually add the following a link

textView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        // Handle click
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);

    }
});
0
barq On

It is not possible to tell from the snippet of code you have pasted. Please show more code. For instance, what does your declaration of textView look like?

0
sirFunkenstine On

Do you have an onClick listener on a parent view that might be overriding the text view? Also youmight want to try not using an anonymous inner class as your listener an instead use the view.OnClickListener below for click events.

myActivity extends Activity implements View.OnClickListener {
    @Override
    publiv void onClick(View v) {
        if(V.getId() == textViewId) {
             // do work
        }
    }
}