Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ClickableSpan in clickable TextView

I have a textview that can contain clickable links. When one of this links is clicked, I want to start an activity. This works fine, but it should also be possible to click the whole textview and start another activity.

So that's my current solution:

    TextView tv = (TextView)findViewById(R.id.textview01);           Spannable span = Spannable.Factory.getInstance().newSpannable("test link span");        span.setSpan(new ClickableSpan() {           @Override         public void onClick(View v) {               Log.d("main", "link clicked");             Toast.makeText(Main.this, "link clicked", Toast.LENGTH_SHORT).show();          } }, 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);     tv.setText(span);       tv.setOnClickListener(new OnClickListener() {         @Override         public void onClick(View v) {             Log.d("main", "textview clicked");             Toast.makeText(Main.this, "textview clicked", Toast.LENGTH_SHORT).show();                        }     });      tv.setMovementMethod(LinkMovementMethod.getInstance()); 

The problem is, that when I set an OnClickListener, everytime I click on a link first the listener for the whole textview and then the one for the ClickableSpan is called.

Is there a way to prevent android from calling the listener for the whole textview, when a link is clicked? Or to decide in the listener for the whole view, if a link was clicked or not?

like image 448
Lukas Avatar asked Mar 03 '11 16:03

Lukas


People also ask

How do I make part of a TextView clickable?

The target=”_blank” attribute is just set to open a new tab in the browser. In Android, the most common way to show a text is by TextView element. The whole text in the TextView is easy to make clickable implementing the onClick attribute or by setting an onClickListener to the TextView.


1 Answers

Found a workaround that is quite straight forward. Define ClickableSpan on all the text areas that are not part of the links and handle the click on them as if the text view was clicked:

TextView tv = (TextView)findViewById(R.id.textview01);       Spannable span = Spannable.Factory.getInstance().newSpannable("test link span");    span.setSpan(new ClickableSpan() {       @Override     public void onClick(View v) {           Log.d("main", "link clicked");         Toast.makeText(Main.this, "link clicked", Toast.LENGTH_SHORT).show();      } }, 5, 9, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  // All the rest will have the same spannable. ClickableSpan cs = new ClickableSpan() {       @Override     public void onClick(View v) {           Log.d("main", "textview clicked");         Toast.makeText(Main.this, "textview clicked", Toast.LENGTH_SHORT).show();      } };  // set the "test " spannable. span.setSpan(cs, 0, 5, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  // set the " span" spannable span.setSpan(cs, 6, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  tv.setText(span);  tv.setMovementMethod(LinkMovementMethod.getInstance()); 

Hope this helps (I know this thread is old, but in case anyone sees it now...).

like image 106
Yoel Gluschnaider Avatar answered Sep 18 '22 18:09

Yoel Gluschnaider