- 400+ Android & Flutter Code
- Monday, January 5, 2015
- How to display bold text in a TextView in android
- TextView bold text programmatically
- Android — Setting TextView to Bold Not Working
- 10 Answers 10
- 4 Ways to make Android TextView Bold
- Way 1 – make Android TextView bold using android:textStyle attribute
- Way 2 – make Android TextView bold using setTypeface() method
- Way 3 – make Android TextView bold using Html.fromHtml() method
- Way 4 – Make Android TextView Bold using separate style
- Step 1
- Step 2
- Step 1
- Step 2
- build.gradle(Project: TextView Bold – Kotlin)
- build.gradle(Module: app)
- gradle.properties
- colors.xml
- strings.xml
- styles.xml
- activity_main.xml
- MainActivity.kt
- How do you change text to bold in Android?
- 21 Answers 21
- how to make a specific text on TextView BOLD
- 27 Answers 27
400+ Android & Flutter Code
Example code for android + flutter app developers.
Monday, January 5, 2015
How to display bold text in a TextView in android
TextView widget display text on android app. we can render a TextView to display text as bold or italic both programmatically by java file and syntactically by xml layout file. we also can bold TextView text syntactically by using string ressource file. this first android app demonstrate us how can we render TextView text bold, italic, normal and even underlined by declarative syntax.
the android:textStyle attribute allow us to set the text style to normal, bold or italic. we can combine this style by separated them using keyboard pipe | symbol. so if we want to render TextView text as bold and italic then we need to set the android:textStyle attribute value as android:textStyle=»blod|italic». android:textStyle=»normal|italic» value display text as italic, android:textStyle=»bold» value display text only bold and android:textStyle=»normal» value define TextView text appearance as normal.
this is very interesting that we can render a TextView’s part of text as bold, italic or underlined by using string resource file. the string resource file allow us to declare a string resource which value contain html tags. so the final output show the stylish text on android app. strings.xml resource file exist under res/values folder.
in the following example code, we declare a string key value pair name stylish_text and its value. stylish_text key’s value contain html tags those make part of text bold, italic and underlined. in the xml layout file we can assign the string resource as android:text=»@string/stylish_text».
TextView bold text programmatically
the following android example app code demonstrate us how can we bold, italic or underline TextView text programmatically at run time in java file.
setTypeface() method allow us to set the typeface and style in which the text should be displayed in a widget. all Typeface families have not bold and italic variants. geTypeface() method allow us to get the current typeface and style in which the text is being displayed. we can define a TextView widget’s font and font variant by the setTypeface() method as setTypeface(Typeface.MONOSPACE, Typeface.BOLD_ITALIC). the setTypefce() method only render a TextView text to bold, italic and normal. it does not support underline text or any other formatting.
if we want to display bold and underlined text programmatically on an app, we need to take help the setText() method. to do this, we need to provide html tags with text as setText() method parameter. the setText() method render the html tags and display formatted text on application.
another way to render TextView text bold programmatically in java code by using style resource file. we can put a styles.xml file on res/values folder. then assign styles item and value to the style file. finally we can apply any style from this style file to the TextView by using the setTextAppearnce() method.
setTextAppearance() method allow us to set the text color, size, style, hint color etc from the specified TextAppearance resource.
Источник
Android — Setting TextView to Bold Not Working
Here is the XML:
Although the Graphical Layout shows bold text, on the device it’s not. Is it a device thing or what?
Update:
For people who keeps on asking for the ful XML, here it is updated. A simple relative layout. And here is the Java Code:
Things to think about: Could it be due to the device typefaces? Does anyone have Galaxy S3 to confirm? Could it be the activity style? Here is the one used for the whole app:
The TransparentActivity style is used for the concerned activity.
10 Answers 10
Well, I found a silly answer to this problem. I was using a custom font on the device, not the default one. Once I switched to the default one, every things worked as expected!
there is one method, you can set textview typeface by calling setTypeface method.
and also refer this link.
The reason TextView not changes to bold when you run your application on mobile device is that you must have used Calligraphy in your activity. So, to change the style to bold all you have to do is write the following code in your java file.
Most of the answers are correct.
You can use it this way :
What is nice about the SpannableString is that you can apply mutliple spans, each on different parts of the string ! As you noticed, I applied the bold type face only on a part of the string (you specify the start and end indexes) , and it should look like this :
In the method setSpan, you specify the SPAN to apply, the starting index, the ending index, and flags (I always use 0), in that specific order.
You can even apply other spans, like change the text size (use RelativeSizeSpan ), or even color (use ForegroundColorSpan ), and much more !
Here is an example for the color span, that you can achieve in the following manner :
And now, the first part of the string (containing the word no) will be displayed in red !
The font size defined in the dimensions file might not have been picked up during run. Guess you are not running the app in emulator.
Check the following to ensure that its not device thing.
Источник
4 Ways to make Android TextView Bold
In this tutorial, you will learn how to make Android TextView bold. There are 4 ways in this tutorial, you can easily learn, adapt the code and use it in your project as you like. Let’s start.
Way 1 – make Android TextView bold using android:textStyle attribute
android:textStyle attribute is the first and one of the best way to make the text in TextView bold. just use “bold”.
Way 2 – make Android TextView bold using setTypeface() method
TextView has a method called setTypeface() which needs
- Typeface
- Int styleflag
In our case, leave Typeface as null, use styleflag Typeface.BOLD.
Way 3 – make Android TextView bold using Html.fromHtml() method
The fromHtml() method returns displayable styled text from given Html string. Using this advantage, we can make our text in TextView bold.
If you got Manifest merger failed error, then add below code in gradle.properties.
HtmlCompat.FROM_HTML_MODE_LEGACY – It just adds two newline character between block level elements.
Way 4 – Make Android TextView Bold using separate style
In this example, we create a separate style resource and set it to our TextView. The advantage of this technique – you can use this style for many TextViews. Just specifying style attribute.
Step 1
Create a separate style resource named “boldStyle”, add “android:textStyle” as item and provide value “bold”.
Step 2
Set style to TextView using style attribute.
Let’s create an Android app with these examples. Open your Android Studio,
Step 1
Start a new Android Studio project
Company domain: androidride.example.com
Step 2
Select minimum SDK: API 15 – Android 4.0.3 (Ice Cream Sandwich) and click Next.
Next dialog, Select Empty Activity and click Next.
Activity Name: MainActivity
Check Generate layout file
Layout Name: activity_main
build.gradle(Project: TextView Bold – Kotlin)
build.gradle(Module: app)
gradle.properties
colors.xml
strings.xml
styles.xml
activity_main.xml
MainActivity.kt
Run Now android textview bold
If you find anything useful, please share it.
Thank you.
Источник
How do you change text to bold in Android?
How do you change text/font settings in an Android TextView ?
For example, how do you make the text bold?
21 Answers 21
To do this in the layout.xml file:
Programmatically the method is:
Sets the typeface and style in which the text should be displayed. Note that not all Typeface families actually have bold and italic variants, so you may need to use setTypeface(Typeface, int) to get the appearance that you actually want.
Here is the solution
Simply you can do the following:
Set the attribute in XML
Programatically the method is:
Hope this helps you thank you.
You can only use specific fonts sans , serif & monospace via xml, Java code can use custom fonts
Programmatically (Java code)
For case where you are using custom fonts, but do not have bold typeface for the font you can use:
From the XML you can set the textStyle to bold as below
You can set the TextView to bold programmatically as below
Set the attribute
If you’re drawing it then this will do it:
In the ideal world you would set the text style attribute in you layout XML definition like that:
There is a simple way to achieve the same result dynamically in your code by using setTypeface method. You need to pass and object of Typeface class, which will describe the font style for that TextView. So to achieve the same result as with the XML definition above you can do the following:
Источник
how to make a specific text on TextView BOLD
I don’t know how to make a specific text on TextView become BOLD.
I want the output to be like this:
id and name are variables that I have retrieved the value from database, and I want to make the id to bold, but only the id so the name will not affected, I have no idea how to do this.
27 Answers 27
Just build your String in HTML and set it:
While you can use Html.fromHtml() you can use a more native approach which is SpannableStringBuilder , this post may be helful.
First: You don’t need to worry about using the slow performance code from the Raghav Sood’s answer.
Second: You don’t need to write an extension function provided by w3bshark’s answer when using Kotlin.
Finnaly: All you need to do is to use the Kotlin android-ktx library from Google (refer here to find more information and how to include it on your project):
Produces: 1111 neil
UPDATE:
Because I think it can help someone else as well as to demonstrate how far you can go here are more use cases.
When you need to display a text with some parts in blue and italic:
When you need to display a text in both bold and italic:
In short, bold , append , color and italic are extension functions to SpannableStringBuilder . You can see another extension functions in the official documentation, from where you can think for other possibilities.
I thought that the chosen answer didn’t provide a satisfactory result. I have written my own function which takes 2 strings; The full text and the part of the text you want to make bold.
It returns a SpannableStringBuilder with the ‘textToBold’ from ‘text’ bolded.
I find the ability to make a substring bold without wrapping it in tags useful.
As wtsang02 said, using HTML is an expensive overhead. Just use the native solution. If you don’t have to modify the string, just use SpannableString, not SpannableStringBuilder.
In case you want to use the string from XML, you can do something like this:
strings.xml (the «CDATA» part is important, otherwise it won’t work)
layout file
code
Its simple just close the specified text like this for example «your text here:»
result: Headquarters: Mooresville, North Carolina, U.S.
If you are using Kotlin, it becomes even easier to do by using core-ktx , as it provides a domain-specific-language (DSL) for doing this:
More options provided by it are:
At last, you can just to:
Based on @mladj0ni’s answer, I got the code below to work. The problem was that if you use String.format, it strips out the html markup, so you have to escape the bracket symbols in strings.xml:
strings.xml:
Hello, %1$s! You have %2$d new messages.
code.java:
It’s simpler than SpannableStringBuilder. As for performance, if you’re displaying just one string, then the user won’t notice the extra millisecond to parse it.
See the documentation here.
Here is better solution if you want to make multiple text to bold. I’ve improved Eitan’s code. thanks Eitan.
You can use this code to set part of your text to bold. For whatever is in between the bold html tags, it will make it bold.
wtsang02 answer is the best way to go about it, since, Html.fromHtml(«») is now deprecated. Here I’m just going to enhance it a little bit for whoever is having problem in dynamically making the first word bold, no matter whats the size of the sentence.
First lets create a method to get the first word:
Now let’s say you have a long string like this:
And you want your sentence to be like yourAwesomeName@gmail.com want’s to be your friend! All you have to do is- get the firstWord and get the lenght of it to make the firstWord bold, something like this:
Now just follow wtsang02 ‘s steps, like this:
And that’s it! Now you should be able to bold a word with any size from long/short sentence.
Источник