Thursday, November 4, 2021

jetpack compose - Long click listener

MainActivity.kt

package com.cfsuman.jetpackcompose

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.tween
import androidx.compose.foundation.*
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.*
import androidx.compose.ui.graphics.Color
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.SolidColor
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedObjectState: Bundle?) {
        super.onCreate(savedObjectState)
        setContent {
            GetScaffold()
        }
    }


    @Composable
    fun GetScaffold() {
        Scaffold(
            topBar = {
                TopAppBar(
                    title = {
                        Text(
                            text = "Compose - Long click listener"
                        )
                    },
                    backgroundColor = Color(0xFF4f7942),
                )
            },
            content = { MainContent() },
            backgroundColor = Color(0xFFaddfad)
        )
    }


    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun MainContent() {
        val message = remember { mutableStateOf("Click Action")}
        val shape = remember { mutableStateOf(RoundedCornerShape(48.dp))}

        Column(
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center,
            modifier = Modifier.fillMaxSize().padding(12.dp)
        ) {
            Box(
                modifier = Modifier
                    .size(300.dp)
                    .clip(shape.value)
                    .background(SolidColor(Color(0xFF50c878)))
                    .combinedClickable(
                        enabled = true,
                        onClick = {
                            shape.value = RoundedCornerShape(48.dp)
                            message.value = "Clicked"
                        },
                        onClickLabel = "Click to apply rounded shape",
                        onLongClick = {
                            shape.value = CircleShape
                            message.value = "Long Clicked"
                        },
                        onLongClickLabel = "Long click to apply circle shape",
                    )
                    .padding(12.dp),
                contentAlignment = Alignment.Center
            ) {
                Text(
                    text = message.value,
                    style = MaterialTheme.typography.h5,
                    fontWeight = FontWeight.Bold
                )
            }
        }
    }
}