/* 2023-03-09 File created. Copyright (c) Kari Laitinen This app demonstrates the use of AlertDialogs. If you want to test this app, create an Android Compose project with name AlertDialogDemoC, and use the package name shown below. Then, store this file to the folder where MainActivity.kt is and make the following change to AndroidManifest.xml: android:name=".AlertDialogDemoCActivity" Some 'warnings': - tab size in this file is 3 spaces - underscores may be used in the names invented by the programmer */ package alertdialog.democ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.ui.Modifier import androidx.compose.ui.tooling.preview.Preview import alertdialog.democ.ui.theme.AlertDialogDemoCTheme import androidx.compose.foundation.BorderStroke import androidx.compose.foundation.ExperimentalFoundationApi import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.gestures.detectTapGestures import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.material.ripple.rememberRipple import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.unit.dp import androidx.compose.ui.graphics.Color import androidx.compose.ui.graphics.Shape import androidx.compose.ui.input.pointer.pointerInput import androidx.compose.ui.semantics.Role import androidx.compose.ui.text.style.TextAlign import androidx.compose.ui.unit.sp // The following button function is copied from https://stackoverflow.com/questions/65835642/ // Later there should emerge an official material button that accepts long presses. @OptIn(ExperimentalMaterialApi::class, androidx.compose.foundation.ExperimentalFoundationApi::class) @Composable fun ButtonWithLongPress( onClick: () -> Unit, onDoubleClick:()->Unit = {}, onLongClick: () -> Unit, modifier: Modifier = Modifier, enabled: Boolean = true, interactionSource: MutableInteractionSource = remember { MutableInteractionSource() }, elevation: ButtonElevation? = ButtonDefaults.elevation(), shape: Shape = MaterialTheme.shapes.small, border: BorderStroke? = null, colors: ButtonColors = ButtonDefaults.buttonColors(), contentPadding: PaddingValues = ButtonDefaults.ContentPadding, content: @Composable RowScope.() -> Unit ) { val contentColor by colors.contentColor(enabled) Surface( onClick = { }, modifier = modifier .combinedClickable( interactionSource, rememberRipple(), true, null, Role.Button, null, onClick = { onClick() }, onLongClick = { onLongClick() }, onDoubleClick = {onDoubleClick()}), enabled = enabled, shape = shape, color = colors.backgroundColor(enabled).value, contentColor = contentColor.copy(alpha = 1f), border = border, elevation = elevation?.elevation(enabled, interactionSource)?.value ?: 0.dp, interactionSource = interactionSource, ) { CompositionLocalProvider(LocalContentAlpha provides contentColor.alpha) { ProvideTextStyle( value = MaterialTheme.typography.button ) { Row( Modifier .defaultMinSize( minWidth = ButtonDefaults.MinWidth, minHeight = ButtonDefaults.MinHeight ) .padding(contentPadding) .combinedClickable(interactionSource, null, true, null, Role.Button, null, onClick = { onClick() }, onLongClick = { onLongClick() }, onDoubleClick = { onDoubleClick() }), horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically, content = content ) } } } } class AlertDialogDemoCActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { AlertDialogDemoCTheme { // A surface container using the 'background' color from the theme Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) { val single_choice_dialog_visible = remember { mutableStateOf(false ) } val double_choice_dialog_visible = remember { mutableStateOf(false ) } // The following Column is the 'main view' of this app. Column( horizontalAlignment = Alignment.CenterHorizontally ) { Spacer( modifier = Modifier.weight( 0.5F ) ) // Text had to be put inside a Box in order to center it // inside the upper half of the screen. Box( modifier = Modifier.weight( 1F ), contentAlignment = Alignment.Center ) { Text( "You can do either\na short press or a long press\non the button below.", fontSize = 20.sp, textAlign = TextAlign.Center //changing_text.value, fontSize = 40.sp, //modifier = Modifier.weight(1F) ) } ButtonWithLongPress( onClick = { double_choice_dialog_visible.value = true }, onLongClick = { println( "JOOOOOOOO" ) single_choice_dialog_visible.value = true }, modifier = Modifier.requiredHeight( 100.dp ). requiredWidth( 200.dp).weight( 1F ) , shape = RoundedCornerShape( 20.dp ), colors = ButtonDefaults.buttonColors( backgroundColor = Color.Cyan ) ) { Text( text = "PRESS", fontSize = 32.sp ) } Spacer( modifier = Modifier.weight( 0.5F ) ) } if ( single_choice_dialog_visible.value ) { AlertDialog( onDismissRequest = { single_choice_dialog_visible.value = false }, title = { Text(text = "Single Choice Dialog") }, text = { Text( "You have now successfully opened an AlertDialog that " + "has a single button that closes the dialog. " + "The dialog closes also if you tap outside the dialog area." ) }, buttons = { Row( modifier = Modifier.padding(all = 8.dp), horizontalArrangement = Arrangement.Center ) { Button( modifier = Modifier.fillMaxWidth(), onClick = { single_choice_dialog_visible.value = false } ) { Text("Dismiss") } } } ) } if ( double_choice_dialog_visible.value ) { AlertDialog( onDismissRequest = { // Dismiss the dialog when the user clicks outside the dialog or on the back // button. If you want to disable that functionality, simply use an empty // onCloseRequest. double_choice_dialog_visible.value = false }, title = { Text(text = "Double Choice Dialog") }, text = { Text( "You have now successfully opened an AlertDialog that " + "has has two buttons that will close the dialog. " + "The dialog will close also when you tap outside the dialog area." ) }, confirmButton = { TextButton( onClick = { double_choice_dialog_visible.value = false } ) { Text("Confirm") } }, dismissButton = { TextButton( onClick = { double_choice_dialog_visible.value = false } ) { Text("Dismiss") } } ) } } } } } }