To Draw an Underline Below the Textview in Android

Can I underline text in an Android layout?

It can be achieved if you are using a string resource xml file, which supports HTML tags like <b></b>, <i></i> and <u></u>.

<resources>
<string name="your_string_here"><![CDATA[This is an <u>underline</u>.]]></string>
</resources>

If you want to underline something from code use:

TextView textView = (TextView) view.findViewById(R.id.textview);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
textView.setText(content);

To draw an Underline below the TextView in Android

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));

OR

txtView.setText(Html.fromHtml("<u>underlined</u> text"));

How to set underline text on textview?

You have to use SpannableString for that :

String mystring=new String("Hello.....");
SpannableString content = new SpannableString(mystring);
content.setSpan(new UnderlineSpan(), 0, mystring.length(), 0);
yourtextview.setText(content);

Update : You can refer my answer on Underling TextView's here in all possible ways.

draw line under TextView on Android

This is the simplest and most similar to using the <hr> tag in HTML:

Put this in your XML layout where you want the line:

<View 
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#ffffff" />

This will draw a white line, 1 dp thick, across the screen. If you want it to be a fixed width, just change the layout_width to the dp size you want. Change the background to the HTML color code of your choice.

How do you underline a text in Android XML?

If you are using a string resource xml file (supports HTML tags), it can be done using<b> </b>, <i> </i> and <u> </u>.

<resources>
<string name="your_string_here">
This is an <u>underline</u>.
</string>
</resources>

If you want to underline something from code use:

TextView tv = (TextView) view.findViewById(R.id.tv);
SpannableString content = new SpannableString("Content");
content.setSpan(new UnderlineSpan(), 0, content.length(), 0);
tv.setText(content);


Related Topics



Leave a reply



Submit