/* (c) naturalprogramming.com 2022-09-21 File created by Kari Laitinen 2022-09-28 Last modification. This simple app contains a Text and a Button, and Spacers to center the other components vertically. This is an Android app made of this single file. If you want to test this app, create an Android Compose project with 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=".ButtonDemoCActivity" Some 'warnings': - tab size in this file is 3 spaces - underscores are used in the names invented by the programmer */ package button.democ import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.layout.* import androidx.compose.foundation.shape.RoundedCornerShape import androidx.compose.material.* import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.sp import button.democ.ui.theme.ButtonDemoCTheme class ButtonDemoCActivity : ComponentActivity() { override fun onCreate( savedInstanceState: Bundle? ) { super.onCreate( savedInstanceState ) setContent { ButtonDemoCTheme { // A surface container using the 'background' color from the theme Surface(modifier = Modifier.fillMaxSize(), color = MaterialTheme.colors.background) { val changing_text = remember { mutableStateOf("Hello!") } 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( changing_text.value, fontSize = 40.sp, //modifier = Modifier.weight(1F) ) } Button( onClick = { if ( changing_text.value == "Hello!" ) { changing_text.value = "Well done!" } else { changing_text.value = "Hello!" } }, 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 ) ) } } } } } }