khoben / alertdialog

Simplify android alert dialog building with retained callback

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AlertDialog - Customizable alert dialog with retained callback actions

Sample app (apk)

Usage

Setup alert dialog config (optional)

Should be called once at Application.OnCreate() or smth:

Alert.defaultConfig {
   dialogStyle = R.style.MaterialAlertDialog_MaterialComponents
   headerLayoutResource = R.layout.dialog_header
   footerLayoutResource = R.layout.dialog_footer
   dialogIsCancellable = false
}

Create AlertDialog

private val SAMPLE_DIALOG_TAG = "SAMPLE_DIALOG_TAG"

...

alert { // build and show alert
    style(R.style.MaterialAlertDialog_MaterialComponents)
    header(R.layout.dialog_header)
    footer(R.layout.dialog_footer)
    title { 
        text("Hi")
        alignment(LayoutAlign.RIGHT)
    }
    message {
        text("How do you do?")
        alignment(LayoutAlign.RIGHT)
    }
    controlsAlignCenter(true) // make button layout centered
    buttonCallback(callbackTag = SAMPLE_DIALOG_TAG) { // enable callback
        positiveButton("Yes")
        neutralButton("Cancel")
        negativeButton("No")
    }
    customView(R.layout.activity_main) // set custom view
    cancellable(false)
}


Simple


With header


Full equipment

Listen alert dialog button click events:

// Implement AlertEventListener to catch button click events from alert dialog buttons
class MainActivity : AppCompatActivity(R.layout.activity_main), 
AlertEventListener {

    ...

    // from AlertEventListener
    override fun onAlertEvent(event: AlertEvent) {
       event.doIfMatches(SAMPLE_DIALOG_TAG) {
          when(event) {
             is AlertEvent.Negative -> {
                showToast("Negative via ${event.callbackTag}")
             }
             is AlertEvent.Neutral -> {
                showToast("Neutral via ${event.callbackTag}")
             }
             is AlertEvent.Positive -> {
                showToast("Positive via ${event.callbackTag}")
             }
          }
       }
    }
}

Installation

  1. Add the JitPack repository to your build file:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }
  2. Add the dependency

    Latest version isâ €

    dependencies {
        implementation 'com.github.khoben:alertdialog:<latest_version>'
    }

About

Simplify android alert dialog building with retained callback


Languages

Language:Kotlin 100.0%