Created
November 10, 2019 10:24
-
-
Save carotkut94/5125b7b0db617339bc5ca3be8e37f27f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.death.demo | |
import android.os.Bundle | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.compose.Composable | |
import androidx.compose.state | |
import androidx.compose.unaryPlus | |
import androidx.ui.core.Text | |
import androidx.ui.core.setContent | |
import androidx.ui.layout.Column | |
import androidx.ui.material.AlertDialog | |
import androidx.ui.material.Button | |
import androidx.ui.material.MaterialTheme | |
// Here i created the variable of type State<Boolean> which will be used to | |
// show/hide the alert dialog | |
private val showDialog = +state{ | |
false | |
} | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
MaterialTheme { | |
Column { | |
showAlertButton() | |
presentDialog() | |
} | |
} | |
} | |
} | |
@Composable | |
fun showAlertButton(){ | |
Button("Show Alert", | |
onClick = { | |
// Here changing the value to true so that the alert dialog can be shown | |
showDialog.value = true | |
} | |
) | |
} | |
@Composable | |
fun presentDialog() { | |
if(showDialog.value) | |
AlertDialog( | |
onCloseRequest = { | |
showDialog.value = false | |
}, | |
title = { | |
Text(text = "Alert Dialog") | |
}, | |
text = { | |
Text("JetPack Compose Alert Dialog!") | |
}, | |
confirmButton = { | |
Button("Confirm", onClick = { | |
// reset the value to false, which will make the dialog go away | |
showDialog.value = false | |
}) | |
} | |
) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment