Your Second Kotlin Android App

Aug 29 2023 · Kotlin 1.8.21, Android 13, Android Studio Flamingo | 2022.2.1

Part 2: Create Your Views

12. Create Shared UI Components

Episode complete

Play next episode

Next
About this episode

Leave a rating/review

See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 11. Build a List Item View Next episode: 13. Use LazyLayouts

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

You have created a list item view that can display a list of items in your app. In this episode, you’ll create more UI components that you can share among different screens in your app.

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ListMakerTopAppBar(title: String, showBackButton: Boolean, onBackPressed: () -> Unit) {
    TopAppBar(
        title = {
            Text(text = title)
        },
        colors = TopAppBarDefaults.topAppBarColors(
            containerColor = MaterialTheme.colorScheme.primaryContainer
        ), navigationIcon = {
            if (showBackButton) {
                IconButton(onClick = onBackPressed) {
                    Icon(
                        imageVector = Icons.Default.ArrowBack,
                        contentDescription = stringResource(id = R.string.cd_back_icon)
                    )
                }
            }
        }
    )
}
@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun ListMakerFloatingActionButton(
  title: String,
  inputHint: String,
  onFabClick: (String) -> Unit
) {
  var showDialog by remember { mutableStateOf(false) }
  var taskName by remember { mutableStateOf("") }

  FloatingActionButton(
    onClick = {
      showDialog = true
    },
    content = {
      Icon(
        imageVector = Icons.Default.Add,
        contentDescription = stringResource(id = R.string.cd_add_icon)
      )
    }
  )

  if (showDialog) {
    AlertDialog(
      onDismissRequest = { showDialog = false },
      title = { Text(text = title) },
      text = {
        OutlinedTextField(
          value = taskName,
          onValueChange = { taskName = it },
          label = { Text(text = inputHint) },
          singleLine = true
        )
      },
      confirmButton = {
        Button(
          onClick = {
            showDialog = false
            onFabClick(taskName)
            taskName = ""
          },
          content = {
            Text(text = stringResource(id = R.string.label_create))
          }
        )
      },
    )
  }
}