Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.test.testbutton
- import android.os.Bundle
- import androidx.activity.ComponentActivity
- import androidx.activity.compose.setContent
- import androidx.compose.foundation.layout.Arrangement
- import androidx.compose.foundation.layout.Column
- import androidx.compose.foundation.layout.Spacer
- import androidx.compose.foundation.layout.fillMaxSize
- import androidx.compose.foundation.layout.fillMaxWidth
- import androidx.compose.foundation.layout.height
- import androidx.compose.material3.Button
- import androidx.compose.material3.MaterialTheme
- import androidx.compose.material3.Surface
- import androidx.compose.material3.Text
- import androidx.compose.runtime.Composable
- import androidx.compose.runtime.getValue
- import androidx.compose.runtime.mutableStateOf
- import androidx.compose.runtime.remember
- import androidx.compose.runtime.saveable.rememberSaveable
- import androidx.compose.runtime.setValue
- 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 id.ruff.testbutton.ui.theme.TestButtonTheme
- class MainActivity : ComponentActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContent {
- TestButtonTheme {
- // A surface container using the 'background' color from the theme
- Surface(
- modifier = Modifier.fillMaxSize(),
- color = MaterialTheme.colorScheme.background
- ) {
- TestButtonPreview()
- }
- }
- }
- }
- }
- @Composable
- fun TestButton(modifier: Modifier = Modifier) {
- var currentState by remember { mutableStateOf(true) }
- val myWord = when (currentState) {
- true -> "RED"
- false -> "BLUE"
- }
- val myColor = when (currentState) {
- true -> Color(red = 205, green = 45, blue = 85)
- false -> Color(red = 45, green = 145, blue = 205)
- }
- Column(
- horizontalAlignment = Alignment.CenterHorizontally,
- verticalArrangement = Arrangement.Center
- ) {
- Text(myWord, fontSize = 128.sp, color = myColor)
- Spacer(Modifier.height(16.dp))
- // Hoist State of the button click activity to change colour here
- ButtonSection({currentState = !currentState},modifier)
- }
- }
- @Composable
- fun ButtonSection(
- onButtonPressed: () -> Unit,
- modifier: Modifier = Modifier) {
- Button(
- onClick = { onButtonPressed() }
- ) {
- Text("clicky click")
- }
- }
- @Preview(showBackground = true)
- @Composable
- fun TestButtonPreview() {
- TestButtonTheme {
- TestButton()
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement