Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get rid of the underline in a Spannable String with a Clickable Object?

I have a Spannable Object with a Clickable Object set to it. When the Spannable String is displayed in the TextView it has blue text and a blue underline (indicating to the user that this Text is Clickable). My problem is how can I prevent appearing the blue underline in TextView?

like image 733
Ash Avatar asked Apr 15 '13 03:04

Ash


People also ask

How do I make a string clickable?

The most important attribute that allows one to make links in HTML is the href attribute of the <a> element. As mentioned before, the href attribute indicated the link's destination. To break the code that helps you make text clickable in HTML and understand it better, <a href=” “> helps one to specify the target.

How do you underline text in TextView?

You can also underline a portion of the text via code, it can be done by creating a SpannableString and then setting it as the TextView text property: SpannableString text = new SpannableString("Voglio sottolineare solo questa parola");text. setSpan(new UnderlineSpan(), 25, 6, 0);textView. setText(text);


4 Answers

Use the below code and try

String mystring =" Hello";
SpannableString ss= new SpannableString(mystring);
ss.setSpan(new MyClickableSpan(mystring), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  

class MyClickableSpan extends ClickableSpan{// extend ClickableSpan     

    String clicked;
    public MyClickableSpan(String string) {
        super();
        clicked = string;
    }
    @Override
    public void onClick(View tv) {
       Toast.makeText(MainActivity.this,clicked , Toast.LENGTH_SHORT).show();
    }

    @Override
    public void updateDrawState(TextPaint ds) {// override updateDrawState
        ds.setUnderlineText(false); // set to false to remove underline
    }
}
like image 55
Raghunandan Avatar answered Oct 25 '22 06:10

Raghunandan


This works for me. No need to create custom ClickableSpan class. Just override updateDrawState(TextPaint ds).

SpannableString span = new SpannableString("Some text");
ClickableSpan clickSpan = new ClickableSpan() {
    @Override
    public void updateDrawState(TextPaint ds) {
        ds.setColor(ds.linkColor);    // you can use custom color
        ds.setUnderlineText(false);    // this remove the underline
    }

    @Override
    public void onClick(View textView) {
        // handle click event
    }
};

span.setSpan(clickSpan, 5, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(span);
like image 33
ARiF Avatar answered Oct 25 '22 05:10

ARiF


Raghunandan's answer works perfectly for me. Here is a pared-down version of it:

public abstract class NoUnderlineClickableSpan extends ClickableSpan {    
    public void updateDrawState(TextPaint ds) {
        ds.setUnderlineText(false);
    }
}
like image 37
Jo Jo Avatar answered Oct 25 '22 06:10

Jo Jo


Override updateDrawState method of ClickableSpan class

String mystring =" Hello";
SpannableString ss= new SpannableString(mystring);
ss.setSpan(new MyClickableSpan(mystring), 0, ss.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  

class MyClickableSpan extends ClickableSpan{// extend ClickableSpan     

String clicked;
public MyClickableSpan(String string) {
    // TODO Auto-generated constructor stub
super();
clicked =string;
}

public void onClick(View tv) {

   Toast.makeText(MainActivity.this,clicked ,
        Toast.LENGTH_SHORT).show();
}

public void updateDrawState(TextPaint ds) {// override updateDrawState
   ds.setUnderlineText(false); // set to false to remove underline
}

For changing color of spannable String

  SpannableString    ss = new SpannableString("android Stack Overflow");

  ForegroundColorSpan fcs=newForegroundColorSpan(Color.parseColor("#01579B"));
  ss.setSpan(fcs, 8,13, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
like image 24
Sanjay Jain Avatar answered Oct 25 '22 07:10

Sanjay Jain