Jetpack Compose

Oct 11 2022 · Kotlin 1.7.10, Android 13, Android Studio Chipmunk

Part 2: Build Complex UI with Compose

11. Build Custom Dialogs

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: 10. Add Actions & Handlers Next episode: 12. Add Themes & Styling to the App

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.

Notes: 11. Build Custom Dialogs

The student materials have been reviewed and are updated as of September 2022.

Heads up... You've reached locked video content where the transcript will be shown as obfuscated text.

Demo

You can also build custom dialogs in compose! Let’s build one to be able to add reading lists. Create a new file within the readingLists.ui package, named ReadingListDialogs, and add the following code:

@Composable
fun AddReadingList(
  onAddList: (String) -> Unit = {},
  onDismiss: () -> Unit = {}
) {
  val inputState = remember { mutableStateOf("") }

  Dialog(onDismissRequest = onDismiss) {
    val shape = RoundedCornerShape(16.dp)
  }
}
    Column(
      modifier = Modifier.background(
        MaterialTheme.colors.surface,
        shape = shape
      ).border(width = 1.dp, color = MaterialTheme.colors.primary, shape = shape),
      verticalArrangement = Arrangement.Center,
      horizontalAlignment = Alignment.CenterHorizontally
    ) {

    }
      Spacer(modifier = Modifier.height(16.dp))

      Text(
        text = stringResource(id = R.string.add_reading_list_title),
        fontSize = 18.sp,
        fontWeight = FontWeight.Bold
      )

      InputField(
        label = stringResource(id = R.string.reading_list_name_hint),
        value = inputState.value,
        isInputValid = inputState.value.isNotEmpty(),
        onStateChanged = { newValue -> inputState.value = newValue }
      )
      ActionButton(
        modifier = Modifier.fillMaxWidth(),
        text = stringResource(id = R.string.add_reading_list_button_text),
        isEnabled = inputState.value.isNotEmpty(),
        onClick = { onAddList(inputState.value) },
      )
val readingListsState = mutableStateOf(emptyList<ReadingListsWithBooks>())

private val _isShowingAddReadingListState = mutableStateOf(false)

  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    loadReadingLists()
  }

  private fun loadReadingLists() {
    lifecycleScope.launch {
      readingListsState.value = repository.getReadingLists()
    }
  }
val readingLists = readingListsState.value

Box(
      modifier = Modifier.fillMaxSize(),
      contentAlignment = Alignment.Center
    ) {

    ReadingLists(readingLists, onItemClick = { onItemSelected(it) })
    
      val isShowingAddList = _isShowingAddReadingListState.value

      if (isShowingAddList) {
        AddReadingList(
          onDismiss = { _isShowingAddReadingListState.value = false },
          onAddList = { name ->
            addReadingList(name)
            _isShowingAddReadingListState.value = false
          })
      }
    }
  @Composable
  fun AddReadingListButton() {
    FloatingActionButton(onClick = {
      _isShowingAddReadingListState.value = true
    }) {
      Icon(asset = Icons.Default.Add)
    }
  }


  fun addReadingList(readingListName: String) {
    lifecycleScope.launch {
      repository.addReadingList(ReadingList(name = readingListName, bookIds = emptyList()))

      readingListsState.value = repository.getReadingLists()
    }
  }