Leave a rating/review
In the previous episode, you prepared TaskListScreen but had no tasks to display. Instead of displaying a blank screen, you should show a view with the message No tasks yet**. In this challenge, you’ll an create an EmptyView Composable with the No tasks yet text and add it to the TaskListScreenContent.
Create a file named EmptyView.kt in the views package. In the EmptyView file, create a Column that has the following properties:
- A Modifier that has a fill maximum size property.
-
Contents inside the
Columnshould be centered horizontally and vertically. -
The
Columnshould have anIconcomposable displayingIcons.Default.List. -
The
Columnshould have aTextcomposable with the text No tasks yet. -
A space of 16 dp between the
IconandTextcomposables. -
Add a preview for the
EmptyViewcomposable. -
Use your Compose skills to add the
EmptyViewcomposable to theTaskListScreenContentcomposable.
Pause the video and attempt the challenge.
Welcome back! Hope you had fun with the challenge. Here’s a quick demo of what you should have done.
Create a file named EmptyView.kt in the views package. In the EmptyView file, add the following code:
@Composable
fun EmptyView(message: String) {
Column(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Icon(
imageVector = Icons.Default.List,
contentDescription = stringResource(id = com.kodeco.android.listmaker.R.string.cd_list_icon),
modifier = Modifier.size(100.dp)
)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = message,
style = MaterialTheme.typography.titleMedium
)
}
}
@Preview(showBackground = true)
@Composable
fun EmptyViewPreview() {
EmptyView(message = "No tasks yet")
}
Create the cd_list_icon string resource in the strings.xml file with the value List icon. Here’s a breakdown of the code:
-
The
EmptyViewcomposable has amessageparameter which is aStringtype. Themessageparameter is used to display the message in theTextcomposable. -
The
EmptyViewcomposable has aColumncomposable which has the following properties:-
A
Modifierthat has afillMaxSizeproperty. -
The
Columncomposable has averticalArrangementproperty set toArrangement.Centerwhich centers the contents vertically. -
The
Columncomposable has ahorizontalAlignmentproperty set toAlignment.CenterHorizontallywhich centers the contents horizontally.
-
A
-
The
Columncomposable has anIconComposable with the following properties:-
The
imageVectorproperty is set toIcons.Default.Listwhich displays the list icon. -
The
contentDescriptionproperty is set to the string resource cd_list_icon. -
The
Modifierproperty has asizeproperty set to 100.dp which sets the size of the icon to 100.dp.
-
The
-
The
Columncomposable has aSpacercomposable with aModifierwhich has a height of 16.dp. -
The
Columncomposable has aTextcomposable with the following properties:-
The
textproperty is set to themessageparameter. -
The
styleproperty is set toMaterialTheme.typography.titleMediumwhich sets the style of the text totitleMedium.
-
The
-
Lastly, you create the
EmptyViewPreviewcomposable which is used to preview theEmptyViewcomposable. You can build the project to be able to preview theEmptyViewcomposable.
Next, head over to the TaskListContent composable. Add a check to see if the tasks parameter is empty. If it is empty, display the EmptyView composable. If it is not empty, display the LazyColumn composable. Here’s the final code:
@Composable
fun TaskListContent(modifier: Modifier, tasks: List<TaskList>, onClick: (String) -> Unit) {
if (tasks.isEmpty()) {
EmptyView(message = stringResource(id = R.string.text_no_tasks))
} else {
LazyColumn(
modifier = modifier,
content = {
items(tasks) {
ListItemView(
value = it.name,
onClick = onClick
)
}
}
)
}
}
Create the text_no_tasks string resource with the value No tasks yet. The Composable function is still the same as before with the only addition being the check if your tasks parameter is empty.
Build and run your app. Now the app should display the No tasks yet message since there are no tasks.
Congratulations on being able to display a message when there are no tasks!