how to make a specific text on TextView BOLD
Just build your String in HTML and set it:
String sourceString = "<b>" + id + "</b> " + name;
mytextview.setText(Html.fromHtml(sourceString));
How to use Span to make part of TextView Bold untill special character?
Programmatically would be as next:
Java
void format(final TextView view) {
final CharSequence text = view.getText();
if (!TextUtils.isEmpty(text)) {
final int index = text.toString().indexOf(',');
if (index > 0) {
final SpannableStringBuilder sb = new SpannableStringBuilder(text);
sb.setSpan(new StyleSpan(Typeface.BOLD), 0, index, Spannable.SPAN_INCLUSIVE_EXCLUSIVE);
view.setText(new SpannableString(sb));
}
}
}
Kotlin
fun format(view: TextView) {
val text = view.text
if (!TextUtils.isEmpty(text)) {
val index = text.indexOf(',')
if (index > 0) {
val sb = SpannableStringBuilder(text)
sb.setSpan(StyleSpan(Typeface.BOLD), 0, index, Spannable.SPAN_INCLUSIVE_EXCLUSIVE)
view.text = SpannableString(sb)
}
}
}
If on the other hand, is a resource string set to never change, then you can set bold tags directly into your strings resource xml file, with <b> </b>:
<string name="string_name"><b>Hello</b>, World</string>
How to make a part of text in TextView bold?
There are many ways to achieve what you want.
1) You can insert HTML inside your text view as explained by "Tanis" here "How to display HTML in TextView?"
((TextView)findViewById(R.id.carmodel)).setText(Html.fromHtml("<h2>CarModel: </h2>") + getCarModel+"");
2) You can create two text views like
((TextView)findViewById(R.id.carmodelBold)).setText("CarModel: ");
((TextView)findViewById(R.id.carmodel)).setText(getCarModel);
and style the text view with ID "carmodelBold" as you want.
3) You can create a table
How to make only some Words bold in Text View in XML (Android Studio)?
Try this:
<string name="bold">abc<b>Enter_Text_Here_You_Want_To_BOLD</b>efg</string>
And In XML:
android:text="@string/bold"
Set some text bold in textview, but not everything
From the Android docs: https://developer.android.com/reference/android/text/style/StyleSpan
SpannableString string = new SpannableString("Bold and italic text");
string.setSpan(new StyleSpan(Typeface.BOLD), 0, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
string.setSpan(new StyleSpan(Typeface.ITALIC), 9, 15, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
Then call textView.setText(string);
like you normally would.
EDIT: You can also try usingHtmlCompat.fromHtml(html, HtmlCompat.FROM_HTML_MODE_LEGACY);
which also returns a Spanned
of its own.
However, if you're putting this html as a string resource, you will need to escape some XML characters.
Android TextView in BOLD and Normal text
in your strings file
<string name="your_text">
<![CDATA[
<p> <b>Title</b> </p>
<p><b><i>Step 1</i></b><p>step1 content content content content content content</p></p>
<p><b><i>Step 2</i></b><p>step2 content content content content content content content</p></p>
]]>
</string>
Then in your activity
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Html.fromHtml(getString(R.string.your_text)));
And output
How to set the font style to bold, italic and underlined in an Android TextView?
I don't know about underline, but for bold and italic there is "bolditalic"
. There is no mention of underline here: http://developer.android.com/reference/android/widget/TextView.html#attr_android:textStyle
Mind you that to use the mentioned bolditalic
you need to, and I quote from that page
Must be one or more (separated by '|') of the following constant values.
so you'd use bold|italic
You could check this question for underline: Can I underline text in an android layout?
How to make part of the text Bold in android at runtime?
Let's say you have a TextView
called etx
. You would then use the following code:
final SpannableStringBuilder sb = new SpannableStringBuilder("HELLOO");
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold
final StyleSpan iss = new StyleSpan(android.graphics.Typeface.ITALIC); //Span to make text italic
sb.setSpan(bss, 0, 4, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold
sb.setSpan(iss, 4, 6, Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make last 2 characters Italic
etx.setText(sb);
Extract part of string and change it to bold and add it to TextView
Here i made a little example for you. It is exactly what you want.
public class TestActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
button = findViewById(R.id.button_action);
textView = findViewById(R.id.title);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore [et dolore magna aliquyam erat], sed diam voluptua. ?At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren";
Pattern pattern = Pattern.compile("\\[[\\w ]+\\]");
Matcher matcher = pattern.matcher(text);
while (matcher.find())
text = text.replace(matcher.group(), matcher.group().replace("[", "<b>").replace("]", "</b>"));
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(text, Html.FROM_HTML_MODE_COMPACT));
} else {
textView.setText(Html.fromHtml(text));
}
}
});
}
}
Related Topics
Execution Failed App:Processdebugresources Android Studio
Using Intents to Pass Data Between Activities
How to Disable Recyclerview Scrolling
How to Run a Method Every X Seconds
How to Keep Onitemselected from Firing Off on a Newly Instantiated Spinner
Convert View to Bitmap on Android
Android: Java.Lang.Securityexception: Permission Denial: Start Intent
What Is Difference Between Getsupportfragmentmanager() and Getchildfragmentmanager()
Eclipse Error: R Cannot Be Resolved to a Variable
Android Open and Save Files To/From Google Drive Sdk
How to Fire Onlistitemclick in Listactivity with Buttons in List
More Than One File Was Found with Os Independent Path 'Meta-Inf/License'
Tileprovider Using Local Tiles
Determine If Android App Is Being Used For the First Time