Many applications display tons of textual data in form of passages and have the feature of expanding or contracting it. Generally, 2-3 out of n lines of a passage are displayed along with “View More”, “Read More”, and “…” at the end. These appear like hyperlinks that upon click expand the text to full.
So in this article, we will show you how you could create an expandable text 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: 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.
package com.geeksforgeeks.expandabletext
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.tween
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.*
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
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 | Expandable Text" , color = Color.White) }, backgroundColor = Color( 0xff0f9d58 )) },
// Creating Content
content = {
// Creating a Column Layout
Column(Modifier.fillMaxSize(), horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
// Creating a boolean value for
// storing expanded state
var showMore by remember { mutableStateOf( false ) }
// Creating a long text
val text = "A computer science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions."
Column(modifier = Modifier.padding( 20 .dp)) {
// Creating a clickable modifier
// that consists text
Column(modifier = Modifier
.animateContentSize(animationSpec = tween( 100 ))
.clickable(
interactionSource = remember { MutableInteractionSource() },
indication = null
) { showMore = !showMore }) {
// if showMore is true, the Text will expand
// Else Text will be restricted to 3 Lines of display
if (showMore) {
Text(text = text)
} else {
Text(text = text, maxLines = 3 , overflow = TextOverflow.Ellipsis)
}
}
}
}
}
)
}
}
} |
Output:
You can see that we are successfully able to implement expandable text in our application.