Skip to content

Instantly share code, notes, and snippets.

@jojeask
Created February 6, 2025 11:41
Show Gist options
  • Save jojeask/8d5f848eac51bf1758e1ab886c157596 to your computer and use it in GitHub Desktop.
Save jojeask/8d5f848eac51bf1758e1ab886c157596 to your computer and use it in GitHub Desktop.
AndroidUppgift1
package com.example.uppgift1
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonColors
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
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 com.example.uppgift1.ui.theme.Uppgift1Theme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
Uppgift1Theme {
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
Uppgift()
}
}
}
}
}
@Composable
fun Uppgift() {
var currentNumber by remember { mutableIntStateOf(0) }
fun addition() {
currentNumber += 1
}
fun subtraction() {
currentNumber -= 1
}
Column(modifier = Modifier.background(Color.Blue).fillMaxSize()) {
Column(modifier = Modifier
.padding(top = 10.dp)
.background(Color.Green)
.fillMaxWidth()
.height(80.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Text("Uppgift 1", fontSize = 30.sp)
}
Spacer(modifier = Modifier.height(200.dp))
Column(modifier = Modifier
.fillMaxWidth(),
horizontalAlignment = Alignment.CenterHorizontally,
) {
if(currentNumber < 11) {
Button(
onClick = { addition() },
colors = ButtonDefaults.buttonColors(
containerColor = Color.Red,
contentColor = Color.Black
),
modifier = Modifier
.padding(bottom = 10.dp)
.size(150.dp, 60.dp)
) {
Text("PLUS")
}
}
Text("$currentNumber", fontSize = 120.sp)
if(currentNumber > 0) {
Button(onClick = { subtraction() },
colors = ButtonDefaults.buttonColors(
containerColor = Color.Red,
contentColor = Color.Black
),
modifier = Modifier
.padding(top = 10.dp)
.size(150.dp, 60.dp)
) {
Text("MINUS")
}
}
}
}
}
@Preview(showBackground = true)
@Composable
fun Uppgift1Preview() {
Uppgift1Theme {
Uppgift()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment