Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2022 The Android Open Source Project
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package com.example.dice
- import android.os.Bundle
- import androidx.activity.ComponentActivity
- import androidx.activity.compose.setContent
- import androidx.compose.foundation.Image
- import androidx.compose.foundation.layout.Column
- import androidx.compose.foundation.layout.fillMaxSize
- import androidx.compose.foundation.layout.height
- import androidx.compose.foundation.layout.Spacer
- import androidx.compose.foundation.layout.wrapContentSize
- import androidx.compose.material.Button
- import androidx.compose.material.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.setValue
- import androidx.compose.ui.Alignment
- import androidx.compose.ui.Modifier
- import androidx.compose.ui.res.painterResource
- import androidx.compose.ui.res.stringResource
- import androidx.compose.ui.tooling.preview.Preview
- import androidx.compose.ui.unit.dp
- import androidx.compose.ui.unit.sp
- import com.example.dice.R
- import com.example.dice.ui.theme.DiceTheme
- import com.example.dice.ui.theme.DiceTheme
- class MainActivity : ComponentActivity() {
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContent {
- DiceTheme() {
- DiceRollerApp()
- }
- }
- }
- }
- @Preview
- @Composable
- fun DiceRollerApp() {
- DiceWithButtonAndImage(modifier = Modifier
- .fillMaxSize()
- .wrapContentSize(Alignment.Center)
- )
- }
- @Composable
- fun DiceWithButtonAndImage(modifier: Modifier = Modifier) {
- var result by remember { mutableStateOf( 1) }
- val imageResource = when(result) {
- 1 -> R.drawable.dice_1
- 2 -> R.drawable.dice_2
- 3 -> R.drawable.dice_3
- 4 -> R.drawable.dice_4
- 5 -> R.drawable.dice_5
- else -> R.drawable.dice_6
- }
- Column(modifier = modifier, horizontalAlignment = Alignment.CenterHorizontally) {
- Image(painter = painterResource(imageResource), contentDescription = result.toString())
- Spacer(modifier = Modifier.height(16.dp))
- Button(onClick = { result = (1..6).random() }) {
- Text(text = stringResource(R.string.roll), fontSize = 24.sp)
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement