Open In App

How to Display Styled Text from String Resources in Android using Jetpack Compose?

Improve
Improve
Like Article
Like
Save
Share
Report

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.

Output

 



Last Updated : 29 Apr, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads