Introduction
User engagement is essential in every application. When loading data, it is crucial to display a layout or graphics that users find useful, or that indicates something is loading. The Shimmer layout comes into play at that point.
As of now, Jetpack Compose does not have an official shimmer layout. However, I am going to create an extension function of the Modifier in Compose, which can be used on any composable to implement the shimmer effect. Let's dive into the implementation.
Step 1
Create a new Project in JetPack Compose, and in the main activity, use the following code.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ShimmerLayoutExampleTheme {
// A surface container using the 'background' color from the theme
var isLoading by remember {
mutableStateOf(true)
}
LaunchedEffect(key1 = true) {
delay(5*1000)
isLoading = false
}
LazyColumn(
modifier = Modifier.fillMaxSize()
) {
items(20) {
ShimmerItem(isLoading = isLoading, modifier = Modifier
.fillMaxWidth()
.padding(16.dp))
}
}
}
}
The provided code uses Jetpack Compose to create a loading placeholder with a Shimmer effect for a list of items. When the activity starts, the Shimmer effect is shown for each item in the list to indicate that data is being loaded. After a delay of 5 seconds, the Shimmer effect stops, and the actual content is displayed in the list. The Shimmer effect provides a subtle animation that helps improve the user experience by giving visual feedback during the data loading process.
Step 2
Now let's see how the ShimmerItem is defined.
@Composable
fun ShimmerItem(
isLoading: Boolean,
modifier: Modifier = Modifier
) {
Row(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Icon(
painter = painterResource(id = R.drawable.acc),
contentDescription = null,
modifier = Modifier
.align(Alignment.CenterVertically)
.size(60.dp)
.clip(CircleShape)
.shimmerEffect(isLoading)
.alpha(if (isLoading) 0f else 1f)
)
Spacer(
modifier = Modifier
.width(8.dp)
.background(Color.Gray)
)
Column(modifier = Modifier.fillMaxHeight().align(Alignment.CenterVertically)) {
Text(
text = "Name- MR. Ravi Sahu",
modifier = Modifier
.shimmerEffect(isLoading)
.alpha(if (isLoading) 0f else 1f)
)
Spacer(modifier = Modifier.height(10.dp))
Text(
text = "Age- 24 ",
modifier = Modifier
.shimmerEffectNew(isLoading)
.alpha(if (isLoading) 0f else 1f)
)
}
Spacer(modifier = Modifier.weight(1f))
Text(
text = "Contact here - ",
modifier = Modifier
.align(Alignment.CenterVertically)
.shimmerEffect(isLoading)
.alpha(if (isLoading) 0f else 1f)
)
}
}
Here we define a custom Composable function called 'ShimmerItem', which creates a row containing multiple UI elements with a shimmer effect. This shimmer effect acts as a loading placeholder. When the isLoading variable is set to 'true', indicating data is loading, the shimmer effect is shown. Once isLoading is set to false, the shimmer effect stops, and the actual content is displayed.



Join the conversation! Your thoughts help the community grow.