Advertisement
markruff

Android Dev - Hoist State Button Click

Aug 6th, 2023
1,342
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.81 KB | None | 0 0
  1. package com.test.testbutton
  2.  
  3. import android.os.Bundle
  4. import androidx.activity.ComponentActivity
  5. import androidx.activity.compose.setContent
  6. import androidx.compose.foundation.layout.Arrangement
  7. import androidx.compose.foundation.layout.Column
  8. import androidx.compose.foundation.layout.Spacer
  9. import androidx.compose.foundation.layout.fillMaxSize
  10. import androidx.compose.foundation.layout.fillMaxWidth
  11. import androidx.compose.foundation.layout.height
  12. import androidx.compose.material3.Button
  13. import androidx.compose.material3.MaterialTheme
  14. import androidx.compose.material3.Surface
  15. import androidx.compose.material3.Text
  16. import androidx.compose.runtime.Composable
  17. import androidx.compose.runtime.getValue
  18. import androidx.compose.runtime.mutableStateOf
  19. import androidx.compose.runtime.remember
  20. import androidx.compose.runtime.saveable.rememberSaveable
  21. import androidx.compose.runtime.setValue
  22. import androidx.compose.ui.Alignment
  23. import androidx.compose.ui.Modifier
  24. import androidx.compose.ui.graphics.Color
  25. import androidx.compose.ui.tooling.preview.Preview
  26. import androidx.compose.ui.unit.dp
  27. import androidx.compose.ui.unit.sp
  28. import id.ruff.testbutton.ui.theme.TestButtonTheme
  29.  
  30. class MainActivity : ComponentActivity() {
  31.     override fun onCreate(savedInstanceState: Bundle?) {
  32.         super.onCreate(savedInstanceState)
  33.         setContent {
  34.             TestButtonTheme {
  35.                 // A surface container using the 'background' color from the theme
  36.                 Surface(
  37.                     modifier = Modifier.fillMaxSize(),
  38.                     color = MaterialTheme.colorScheme.background
  39.                 ) {
  40.                     TestButtonPreview()
  41.                 }
  42.             }
  43.         }
  44.     }
  45. }
  46.  
  47. @Composable
  48. fun TestButton(modifier: Modifier = Modifier) {
  49.  
  50.     var currentState by remember { mutableStateOf(true) }
  51.  
  52.     val myWord = when (currentState) {
  53.         true -> "RED"
  54.         false -> "BLUE"
  55.     }
  56.  
  57.     val myColor = when (currentState) {
  58.         true -> Color(red = 205, green = 45, blue = 85)
  59.         false -> Color(red = 45, green = 145, blue = 205)
  60.     }
  61.  
  62.     Column(
  63.         horizontalAlignment = Alignment.CenterHorizontally,
  64.         verticalArrangement = Arrangement.Center
  65.     ) {
  66.         Text(myWord, fontSize = 128.sp, color = myColor)
  67.         Spacer(Modifier.height(16.dp))
  68.  
  69.         // Hoist State of the button click activity to change colour here
  70.         ButtonSection({currentState = !currentState},modifier)
  71.     }
  72. }
  73.  
  74. @Composable
  75. fun ButtonSection(
  76.     onButtonPressed: () -> Unit,
  77.     modifier: Modifier = Modifier) {
  78.         Button(
  79.             onClick = { onButtonPressed() }
  80.         ) {
  81.             Text("clicky click")
  82.         }
  83. }
  84.  
  85. @Preview(showBackground = true)
  86. @Composable
  87. fun TestButtonPreview() {
  88.     TestButtonTheme {
  89.         TestButton()
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement