2

I have a text view which has a certain text which is set in the xml file and in the strings.xml it is underlined.Now i am changing this textview form the contents in the edittext,i.e whatever the user is entering.To that text before setting it to the textview i am appending acertain string as well.I want this new string formed to be underlined with blue color and clickable. I tried using

 SpannableString phoneNum = new SpannableString(totalPhoneNum);
 phoneNum.setSpan(new UnderlineSpan(), 0, totalPhoneNum.length(), 0);   
 phoneNum.setText(totalPhoneNum);

This is not working! Can i use this in onResume() I have no idea how to change the color of the underline to blue :(

nandeesh
  • 24,740
  • 6
  • 69
  • 79
D-D
  • 954
  • 2
  • 12
  • 27
  • 2
    http://stackoverflow.com/questions/12284264/how-to-set-underline-and-color-to-partly-text-at-one-time-on-textview http://stackoverflow.com/questions/2394935/can-i-underline-text-in-an-android-layout – Android Nov 26 '12 at 05:59
  • 1
    mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); this worked...i get the underline now...how do i change the color of the underline – D-D Nov 26 '12 at 06:08
  • http://developer.android.com/reference/android/graphics/Paint.html#setColor(int) – MGDroid Nov 26 '12 at 07:23
  • Possible duplicate of [To draw an Underline below the TextView in Android](https://stackoverflow.com/questions/8033316/to-draw-an-underline-below-the-textview-in-android) – Sufian Sep 10 '19 at 06:21

6 Answers6

3

There are three ways of underling the text in TextView.

  1. SpannableString

  2. setPaintFlags(); of TextView

  3. Html.fromHtml();

Let me explain you all approaches :

1st Approach

For underling the text in TextView you have to use SpannableString

String udata="Underlined Text";
SpannableString content = new SpannableString(udata);
content.setSpan(new UnderlineSpan(), 0, udata.length(), 0);
mTextView.setText(content);

2nd Approach

You can make use of setPaintFlags method of TextView to underline the text of TextView.

For eg.

mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
mTextView.setText("This text will be underlined");

You can refer constants of Paint class if you want to strike thru the text.

3rd Approach

Make use of Html.fromHtml(htmlString);

String htmlString="<u>This text will be underlined</u>";
mTextView.setText(Html.fromHtml(htmlString));

From:
To draw an Underline below the TextView in Android

Community
  • 1
  • 1
D-D
  • 954
  • 2
  • 12
  • 27
0

You can use like this

phoneNum.setText(Html.fromHtml("W : "+"<u><FONT COLOR=\"#80776b\" >"+Your text+"</Font></u>"));

Use color code what you want.

Mehul Ranpara
  • 4,245
  • 2
  • 26
  • 39
  • 1
    mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); this worked...i get the underline now...how do i change the color of the underline. – D-D Nov 26 '12 at 06:05
  • you can use by using above code...where you can set the font color..as you want.. – Mehul Ranpara Nov 26 '12 at 06:10
  • 1
    Need to change the color for just the underline – D-D Nov 26 '12 at 06:41
0

try with this

String txt = "<u style=\"text-decoration: none; border-bottom: 1px solid #FF0000\">parragraph</u>";
phoneNum.setText(Html.fromHtml(txt));

this style will remove default underline and put a border bottom of 1 px as of color we pass.

Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75
  • mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); this worked...i get the underline now...how do i change the color of the underline. – D-D Nov 26 '12 at 06:05
0

Use this:

String text = "some string <u><font color=\"#00FF00\">some link</font></u>";
editText.setText(Html.fromHtml(text), TextView.BufferType.SPANNABLE);
Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
  • mTextView.setPaintFlags(mTextView.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG); this worked...i get the underline now...how do i change the color of the underline – D-D Nov 26 '12 at 06:08
0

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
</LinearLayout>

java:

public class TextDemoActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new ViewWithRedDot(this));
    }
}

class ViewWithRedDot extends View {
    public ViewWithRedDot(Context context) {
        super(context);
    }

    @Override
    protected void onDraw(Canvas canvas) {

        Paint circlePaint = new Paint();
        Typeface mType = Typeface.create(Typeface.SANS_SERIF, Typeface.ITALIC);
        circlePaint.setTextSize(25);
        circlePaint.setColor(Color.RED);
        circlePaint.setTypeface(mType);
        canvas.drawText("Default Typeface", 50, 100, circlePaint);
        //
        circlePaint.setFlags(Paint.UNDERLINE_TEXT_FLAG);
        circlePaint.setColor(Color.YELLOW);
        canvas.drawText("Underline Text Flag", 50, 120, circlePaint);
        //
        circlePaint.setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
        circlePaint.setColor(Color.GREEN);
        canvas.drawText("Strike Thru Text Flag", 50, 140, circlePaint);
        //
        circlePaint.setFlags(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.WHITE);
        canvas.drawText("Anti Alias Flag", 50, 160, circlePaint);
        //
        circlePaint.setFlags(Paint.DEV_KERN_TEXT_FLAG);
        circlePaint.setColor(Color.WHITE);
        canvas.drawText("Dev Kern Text Flag", 50, 180, circlePaint);
        //
        circlePaint.setFlags(Paint.FAKE_BOLD_TEXT_FLAG);
        circlePaint.setColor(Color.CYAN);
        canvas.drawText("Fake Bold Text Flag", 50, 200, circlePaint);
    }
}
Android
  • 2,383
  • 1
  • 26
  • 44
0

Add the following code in your layout xml file:

<TextView 
    android:autoLink="phone"
    android:linksClickable="true" 
    android:textColorLink="#5e9baa" />
Lokesh Mehra
  • 545
  • 6
  • 16
Geet
  • 13
  • 4