package com.example.buttons import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.background import androidx.compose.foundation.layout.Column import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.wrapContentSize import androidx.compose.material3.Button import androidx.compose.material3.Text import androidx.compose.runtime.Composable import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.* // remember, mutableStateOf import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.Color import androidx.compose.ui.layout.layout import androidx.compose.ui.tooling.preview.Preview class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyButtons() } } } @Composable fun MyButtons(){ // stan przycisku - zmienna bgColor - gdy klikniemy na przycisk zmieni się -> upadate aplikacji // Compose is declarative and as such the only way to update it is by calling the same composable with new arguments. // These arguments are representations of the UI state. /* "by" - allows the variable bgColor to delegate its getter and setter to the underlying state holder, which in this case is mutableStateOf. "remember" - Composable functions can use the remember API to store an object in memory. A value computed by remember is stored in the Composition during initial composition, and the stored value is returned during recomposition. remember can be used to store both mutable and immutable objects. "mutableStateOf(Color.Green)" - This creates a mutable state holder that holds a value (in this case, a color). mutableStateOf creates a state that can be read and modified, and when the value changes, it will inform the Compose UI to recompose (update) any composables that are dependent on that state. The initial value of this state is set to Color.Green. source :https://deepai.org/chat, https://developer.android.com/ */ var bgColor by remember { mutableStateOf(Color.Green) } // funkcja wywoływana po przyciśnieciu przycisku fun onClick(){ // Generate a random color /* 0xFF shl 24 is a way to set the alpha channel of a color to fully opaque in a 32-bit ARGB color value. The hexadecimal value 0xFF represents the alpha channel (opacity) and is 11111111 in binary. After shifting left by 24 bits: 11111111000000000000000000000000 in binary, or 0xFF000000 in hexadecimal. Math.random() - generates a random floating-point number between 0.0 (inclusive) and 1.0 (exclusive). The multiplication 0xFFFFFF * Math.random() gives you a floating-point number in the range [0, 0xFFFFFF) (or [0, 16777215) in decimal), which means it can represent any RGB color value. OR operation - combines all components into a single ARGB source :https://deepai.org/chat, https://developer.android.com/ */ bgColor = Color((0xFF shl 24) or (0xFFFFFF * Math.random()).toInt()) } // layout Column(modifier = Modifier // layout - obiekty układają sie jeden pod drugim (stos) .fillMaxSize() // cały ekran .background(bgColor) // kolor tła jest pobierany ze zmiennej stanu bgColor .wrapContentSize(Alignment.Center) // przycisk na środku ekranu ) { Button(onClick = {onClick()}){ // przycisk, po kliknieciu wywoływana jest funkcja onClick() zmieniająca zmienną stanu bgColor Text("change bg color") // napis na przycisku } } } @Preview(showBackground = true) // Design @Composable fun ButtonsPreview() { MyButtons() }