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”.



If you want to use bold and italic. Use pipeline symbol “|” . just like this “bold|italic”.

Way 2 – make Android TextView bold using setTypeface() method

TextView has a method called setTypeface() which needs

  1. Typeface
  2. Int styleflag

In our case, leave Typeface as null, use styleflag Typeface.BOLD.

textview2.setTypeface(null,Typeface.BOLD);
textview2.text= "TEXTVIEW 2"


You can use Typeface.ITALIC for italic style.

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.

val html = "This is TEXTVIEW 3"
textview3.text = Html.fromHtml(html)


Html.fromHtml(String source) was deprecated in API level 24. Use androidx.core.text.HtmlCompat instead. For using HtmlCompat, you need to include dependency in your project.

implementation 'androidx.core:core:1.0.1'

If you got Manifest merger failed error, then add below code in gradle.properties.

android.useAndroidX=true
android.enableJetifier=true

        val html = "This is TEXTVIEW 3"
        textview3.text = HtmlCompat.fromHtml(html,HtmlCompat.FROM_HTML_MODE_LEGACY)

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

Application name: TextView Bold – Kotlin

Company domain: androidride.example.com

check to include Kotlin support.


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

Click Finish.


build.gradle(Project: TextView Bold – Kotlin)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    ext.kotlin_version = '1.2.71'
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

build.gradle(Module: app)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.example.androidride.textviewbold_kotlin"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies
        {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'com.android.support:appcompat-v7:28.0.0'

    testImplementation 'junit:junit:4.12'
      implementation 'androidx.core:core:1.0.1'
        }

gradle.properties

# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx1024m
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# Kotlin code style for this project: "official" or "obsolete":
kotlin.code.style=official
android.useAndroidX=true
android.enableJetifier=true


colors.xml



    #008577
    #00574B
    #D81B60

strings.xml


    TextView Bold Example


styles.xml



    
    

    



activity_main.xml


 	 	 	 	 	 	 	 	

    

    

    

    




MainActivity.kt

package com.example.androidride.textviewbold_kotlin

import android.graphics.Typeface
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.text.HtmlCompat
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        //Make textview bold - programmatically
        textview2.setTypeface(null,Typeface.BOLD)
        textview2.text= "TEXTVIEW 2"

        //Using fromHtml() method
        val html = "This is TEXTVIEW 3"
        textview3.text = HtmlCompat.fromHtml(html,HtmlCompat.FROM_HTML_MODE_LEGACY)

    }


}

Run Now

android textview bold example - android set text bold
android textview bold

If you find anything useful, please share it.
Thank you.

Please disable your adblocker or whitelist this site!