Skip to content

Country Picker Dialog

Harsh B. Bhakta edited this page Aug 6, 2020 · 6 revisions

For some reason, if you don't want to use default Country Picker View and want to launch dialog on your own. You can use extension function to launch the dialog.

  1. Launch using Extension function (source)

With Kotlin in action, you can launch dialog using,

   context.launchCountryPickerDialog { selectedCountry: CPCountry? ->
      // your code to handle selected country
   }

Configuration - CPDialogConfig source

cpDialogViewIds

  • This can be used to apply your custom dialog layout when dialog is launched.
  • By default, it uses default cp_dialog.xml and it's children views
  • To use your custom layout do following
    1. Create a layout file (e.g. custom_cp_dialog.xml)
    2. Add following view(s) with custom IDs - these view IDs will be used later
      1. RecyclerView
      2. TextView for dialog title (optional)
      3. EditText for filter query (optional)
      4. ImageView to clear edit text (optional)
      5. Button to clear selection (optional)
    3. Create instance of CPDialogViewIds using these view IDs
      • val cpDialogViewIds = CPDialogViewIds(
            R.layout.id_of_layout_file,
            R.id.id_of_recyclerView,
            R.id.id_of_title_textview, // optional
            R.id.id_of_filter_editText, // optional
            R.id.id_of_clearQuery_imageView, // optional
            R.id.id_of_clearSelection_button // optional
        )
    4. Provide this cpDialogViewIds through extension functions [todo: add link] or CPDialogHelper [todo: add link].

allowSearch

  • Enable / disable search feature
  • By default, search is enabled
  • Options
    • allowSearch = true to enable search
    • allowSearch = false to disable search
  • Provide this through extension functions [todo: add link] or CPDialogHelper [todo: add link].

allowClearSelection

  • Show / hide Clear Selection button
  • By default, showTitle = false
  • When user click on Clear Selection button, callback will be triggered with selectedCountry = null
  • Options
    • allowClearSelection = true to show Clear Selection button
    • allowClearSelection = false to hide Clear Selection button
  • Provide this through extension functions [todo: add link] or CPDialogHelper [todo: add link].

showTitle

  • Show / hide dialog title
  • By default, showTitle = true
  • Options
    • showTitle = true to show dialog title
    • showTitle = false to hide dialog title
  • Provide this through extension functions [todo: add link] or CPDialogHelper [todo: add link].

showFullScreen

  • Show regular size / full size dialog
  • By default, showFullScreen = false
  • Options
    • showFullScreen = true to show full screen dialog
    • showFullScreen = false to show regular size dialog
  • Provide this through extension functions [todo: add link] or CPDialogHelper [todo: add link].
  • Example:
  • Notes
    • Do not use fullScreen if you have small master country list that do not fill entire screen.

Launch using CPDialogHelper (source)

To launch dialog using Extension function (todo: add link) keeps your code clean. If you want more granular control over instance creation, then you can do following:

        // Check CPDataStoreGenerator for available configuration
        val cpDataStore = CPDataStoreGenerator.generate(context)

        // Check CPListConfig for available configuration
        val cpListConfig = CPListConfig()

        // Check CPRowConfig for available configuration
        val cpRowConfig: CPRowConfig = CPRowConfig()

        // Check CPDialogConfig for available configuration
        val cpDialogConfig: CPDialogConfig = CPDialogConfig()

        // Prepare dialogHelper
        val cpDialogHelper = CPDialogHelper(
            cpDataStore = cpDataStore, // required
            cpListConfig = cpListConfig, // Default: CPListConfig()
            cpRowConfig = cpRowConfig, // Default: CPRowConfig()
            cpDialogConfig = cpDialogConfig // Default: CPDialogConfig()
        ){ selectedCountry: CPCountry? ->
            // required: handle selected country
            // note: this is nullable to support clear selection
        }

        // prepare dialog instance
        val dialog = cpDialogHelper.createDialog(context)
        
        // show dialog
        dialog.show()