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);
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.
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);
Hope this helps
To draw an Underline below the TextView in Android
There are three ways of underling the text in TextView.
SpannableString
setPaintFlags(); of TextView
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 Underline Text of Button in Android?
Code only
Java:
Button button = (Button) findViewById(R.id.park);
button.setPaintFlags(button.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
Kotlin:
val button = findViewById<Button>(R.id.park);
button.paintFlags = button.paintFlags or Paint.UNDERLINE_TEXT_FLAG
Resource string with static text (xml only)
If you have a static text in your resources you could also use the following approach in your strings.xml
:
<string name="underlined_text"><u>I\'m underlined</u></string>
Resource string with dynamic text (xml + code)
If you're using dynamic text but don't like the first approach (which isn't the best imho either), you could also use following:
strings.xml
<string name="underlined_dynamic_text"><u>%s</u></string>
Java:
button.setText(getString(R.string.underlined_dynamic_text, "I'm underlined");
Kotlin:
button.text = getString(R.string.underlined_dynamic_text, "I'm underlined")
Related Topics
Firebase Firestore Variable Name Changed
Adb Not Finding My Device/Phone (Macos X)
How to Return to a Parent Activity Correctly
How to Use Opencv in Using Gradle
How to Store(Bitmap Image) and Retrieve Image from SQLite Database in Android
Handling Registration Id Changes in Google Cloud Messaging on Android
Android Intent for Twitter Application
Android Multiple Email Attachments Using Intent
Android Room - Simple Select Query - Cannot Access Database on the Main Thread
Steps to Create APK Expansion File
Android: Scale a Drawable or Background Image
Android Studio Marks R in Red with Error Message "Cannot Resolve Symbol R", But Build Succeeds
Using Alarmmanager to Start a Service at Specific Time
How to Display a Dialog from a Service
Outlined Edit Text from Material Design
How to Configure Gradle to Work "Offline" (Using Cached Dependencies)