How to Display Styled Text from String Resources in Android using Jetpack Compose?
Last Updated :
29 Apr, 2022
In Android, we can store fixed strings in resources as well as hard code them in the front-end. When we store them in the resources, we can call them multiple times in our code and need not re-write them as we do when hard coding. Storing in this fashion gives us the privilege of formatting the text font style. Storing a text in this way is similar to that of HTML and we can add various attributes like bold, italics, etc. to the text. However, displaying the formatted text is a challenge as the regular Text() that we use to display any text or string in compose does not realize the styling and displays only the raw text.
So in this article, we will show you how you could display a styled resource string in Android using Jetpack Compose. Follow the below steps once the IDE is ready.
Step by Step Implementation
Step 1: Create a New Project in Android Studio
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. While choosing the template, select Empty Compose Activity. If you do not find this template, try upgrading the Android Studio to the latest version. We demonstrated the application in Kotlin, so make sure you select Kotlin as the primary language while creating a New Project.
Step 2: Add a string in res > values > strings.xml
Navigate to res > values > strings.xml and add a string between the resources tags as shown below.
XML
<resources>
<string name="foo">Sample text with <b>bold styling</b> to test</string>
</resources>
Step 3: Working with the MainActivity.kt file
Go to the MainActivity.kt file and refer to the following code. Below is the code for the MainActivity.kt file. Comments are added inside the code to understand the code in more detail.
Kotlin
package com.geeksforgeeks.styledtextres
import android.os.Bundle
import android.widget.TextView
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.*
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.ReadOnlyComposable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.compose.ui.viewinterop.AndroidView
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
// Creating a Simple Scaffold
// Layout for the application
Scaffold(
// Creating a Top Bar
topBar = { TopAppBar(title = { Text("GFG | Styled Text from Resources", color = Color.White) }, backgroundColor = Color(0xff0f9d58)) },
// Creating Content
content = {
// Creating a Column Layout
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
// Displaying the string using regular text function
Text(textResource(id = R.string.foo).toString())
// Adding a space of height 100dp
Spacer(Modifier.height(100.dp))
// Displaying the string using the custom function
StyledText(textResource(id = R.string.foo))
}
}
)
}
}
// Creating a function to get string id
@Composable
@ReadOnlyComposable
fun textResource(@StringRes id: Int): CharSequence =
LocalContext.current.resources.getText(id)
// Creating a function to display the styled text
@Composable
fun StyledText(text: CharSequence, modifier: Modifier = Modifier) {
AndroidView(
modifier = modifier,
factory = { context -> TextView(context) },
update = {
it.text = text
}
)
}
}
Output:
We can see that we are able to display the styled string from the resources.
Similar Reads
How to Create Outlined Text in Android using Jetpack Compose? In Android, we can customize various aspects of a Text for displaying them accordingly. We can change the font size, and font family as well as apply various functions like bold, italics, etc. However, it is not possible to display an outline font for the same text. In case, you are unaware of what
3 min read
How to Disable Text Selection in Android using Jetpack Compose? In Android, a TextView is used to display text inside the activity on the screen. Similarly, in Jetpack Compose, a Text element is used to display text on the activity screen. By default, the text displayed in the Text element cannot be selected. To do so, the Text element has to be declared inside
3 min read
How to make Text Selectable in Android using Jetpack Compose? By default, Composable Texts are not selectable which means users cannot copy text from your application, and to enable text selection you need to wrap the text elements into Selection Container. In this article, we will use Androidâs Jetpack Compose to create those chips. A sample image is given be
2 min read
How to Create Superscript and Subscript Text using Jetpack Compose in Android? In this article, we will explain how to create a Superscript and Subscript text using Jetpack Compose. Below is the sample picture to show what we are going to build. Note that we are going to implement this project using the Kotlin language. Prerequisites: Basic Knowledge of Kotlin.Jetpack Compose
2 min read
How to Display Image From Image File Path in Android using Jetpack Compose? ImageViews are used to display images in different formats within the Android Application. We can display images within our Image View from the image file name, bitmap, drawable file, and the image URL as well. In this article, we will take a look at How to load images from user devices within Image
5 min read
Display List of Sensors in Android using Jetpack Compose Many sensors are present within mobile devices that are used for different purposes for performing various functions within the android device and sensing the data. In this article, we will be building a simple application to display the list of available sensors within our android applications usin
5 min read
How to Remove TextField Padding in Android using Jetpack Compose? In Android Jetpack Compose, a TextField is used to take input from the user in the form of text. TextField when in focus or when clicked invokes a soft keyboard. In general, TextField is highly attributed for a better feel and appearance. However, we can use a pre-developed TextField, named BasicTex
3 min read
How to Load Image From URL in Android using Jetpack Compose? Most of the applications in android use images for displaying some useful information within the applications. Images are considered as easy to convey information to the user. An android application uses more than 100 different types of images. So it is not practically possible to add all the images
4 min read
Implement Markdown Text in Android using Jetpack Compose Markdown is a third-party git library that can be used to identify particular types of regex (Regular Expressions) that specifies a search pattern in the text. For example, if you write a passage and mention a website and a telephone number in it, Markdown Text would detect these patterns and create
3 min read
How to Convert Pixel to DP in Android using Jetpack Compose? We have seen the usage of many units while building android applications. These units are given different views such as image view, and text view to specify their height and width within the android application. Many times we also want to convert one unit to another unit. In this article, we will ta
5 min read