Skip to content

Instantly share code, notes, and snippets.

@harisfi
Created October 4, 2023 06:31
Show Gist options
  • Save harisfi/655af3eea11982ae6a38290c70fda680 to your computer and use it in GitHub Desktop.
Save harisfi/655af3eea11982ae6a38290c70fda680 to your computer and use it in GitHub Desktop.
T6
package com.hryzx.interactionhandlingexample
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.interaction.collectIsPressedAsState
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ShoppingCart
import androidx.compose.material3.Button
import androidx.compose.material3.ButtonDefaults
import androidx.compose.material3.Icon
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.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Preview
import com.hryzx.interactionhandlingexample.ui.theme.InteractionHandlingExampleTheme
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
InteractionHandlingExampleTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
PressIconButton(
onClick = {},
icon = { Icon(Icons.Filled.ShoppingCart, contentDescription = null) },
text = { Text("Add to cart") }
)
}
}
}
}
}
@Preview(showBackground = true)
@Composable
fun IHPreview() {
InteractionHandlingExampleTheme {
PressIconButton(
onClick = {},
icon = { Icon(Icons.Filled.ShoppingCart, contentDescription = null) },
text = { Text("Add to cart") }
)
}
}
@Composable
fun PressIconButton(
onClick: () -> Unit,
icon: @Composable () -> Unit,
text: @Composable () -> Unit,
modifier: Modifier = Modifier,
interactionSource: MutableInteractionSource = remember { MutableInteractionSource() },
) {
val isPressed by interactionSource.collectIsPressedAsState()
Button(onClick = onClick, modifier = modifier,
interactionSource = interactionSource) {
AnimatedVisibility(visible = isPressed) {
if (isPressed) {
Row {
icon()
Spacer(Modifier.size(ButtonDefaults.IconSpacing))
}
}
}
}
}
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusState
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.unit.dp
import java.text.SimpleDateFormat
import java.util.*
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AgeCalculatorApp()
}
}
}
@Composable
fun AgeCalculatorApp() {
var birthDate by remember { mutableStateOf("") }
var age by remember { mutableStateOf("") }
val keyboardController = LocalSoftwareKeyboardController.current
val dateFormat = SimpleDateFormat("yyyy-MM-dd", Locale.US)
Column(
modifier = Modifier
.fillMaxSize()
.padding(16.dp),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
OutlinedTextField(
value = birthDate,
onValueChange = {
birthDate = it
calculateAge(dateFormat.parse(it))
},
label = { Text("Enter Birthdate (yyyy-MM-dd)") },
keyboardOptions = KeyboardOptions.Default.copy(
imeAction = ImeAction.Done
),
keyboardActions = KeyboardActions(
onDone = {
keyboardController?.hide()
}
),
singleLine = true,
modifier = Modifier
.fillMaxWidth()
)
Spacer(modifier = Modifier.height(16.dp))
Text(
text = "Age: $age",
style = MaterialTheme.typography.h5
)
}
}
private fun calculateAge(birthDate: Date?) {
if (birthDate != null) {
val today = Calendar.getInstance().time
val ageInMilliseconds = today.time - birthDate.time
val ageInYears = ageInMilliseconds / (365.25 * 24 * 60 * 60 * 1000)
age = String.format("%.2f", ageInYears)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment