6

I would like to make a TextView to be entirely underlined but I can't use a text resource and <u> tag because it is dynamic text.

Related: Can I underline text in an android layout?

So far the only way I know to do this is at runtime. Is this really the only way? Is there a way I could do it in the XML files?

Community
  • 1
  • 1
700 Software
  • 85,281
  • 83
  • 234
  • 341

5 Answers5

18

The easiest solution is probably to create a custom UnderLineTextView component deriving from TextView, override setText() and set the entire text as underlined, something like this (underline code from the link you referred to above):

@Override
public void setText(CharSequence text, BufferType type) {
    // code to check text for null omitted
    SpannableString content = new SpannableString(text);
    content.setSpan(new UnderlineSpan(), 0, text.length(), 0);
    super.setText(content, BufferType.SPANNABLE);

}

It's then just a matter of using your new component in the layout and setting the text as usual. The rest is handled automatically.

More info on custom components: http://developer.android.com/guide/topics/ui/custom-components.html

peter3
  • 1,074
  • 13
  • 22
14

You can underline text over Html.fromHtml(String source)

Example:

textView.setText(Html.fromHtml("this is <u>underlined</u> text"));
Henry Pootle
  • 1,196
  • 1
  • 11
  • 30
  • This will not work because opening bracket of the `` and `` tags is not escaped. For proper example please see http://stackoverflow.com/a/9955051/425183 – Ognyan Mar 31 '12 at 10:25
1

You can also do this via the /res/values/string.xml file if you prefer: For example, in the /res/values/string.xml you could add an entry like:

<string name="createAccount"><u>Create Account</u></string>

And then in the onCreate(Bundle savedInstanceState) method of your activity you would add the following code to cause "Create Account"> to appear as underlined in the UI that you set for the createAccountText TextView that you defined in the xml file in /res/layout/ for your activity:

TextView createAccountText = (TextView) findViewById(R.id.createAccountText);
Resources res = getResources();
CharSequence styledText = res.getText(R.string.createAccount);
createAccountText.setText(styledText, TextView.BufferType.SPANNABLE);
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Danny Remington - OMS
  • 5,244
  • 4
  • 32
  • 21
  • getResources().getText() solved my problem, don't know why with a `textView.setText(getResources().getString(R.string.string_id));` the text was not underlined. – Luca Nicoletti May 23 '17 at 13:27
0

peter3 wrote before to extend TextView class and override setText method.

This solution won't work as setText method is marked as FINAL.

http://developer.android.com/reference/android/widget/TextView.html#setText(java.lang.CharSequence)

Not sure if the code could work with some modifications.

Jorge Mathias
  • 96
  • 1
  • 3
  • The method that peter3 is overriding in his answer is not public final void setText (CharSequence text) but public void setText (CharSequence text, TextView.BufferType type). The method peter3 is describing is not final. (A little late to be adding the comment but I also had to look twice so thought it's worth mentioning). – Stanley Mar 30 '16 at 15:51
0
 SpannableString content = new SpannableString(name);
 content.setSpan(new UnderlineSpan(), 0, name.length(), 0);
 geometrical_textview.setText(content, TextView.BufferType.SPANNABLE);