Skip to content

Instantly share code, notes, and snippets.

@dovahkiin98
Created October 6, 2021 07:32
Show Gist options
  • Save dovahkiin98/95157e662daacddfbc1b60e0fb8bb7c0 to your computer and use it in GitHub Desktop.
Save dovahkiin98/95157e662daacddfbc1b60e0fb8bb7c0 to your computer and use it in GitHub Desktop.
LongClickButton
import android.widget.Toast
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.combinedClickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material.Button
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Text
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
@OptIn(ExperimentalFoundationApi::class)
@Composable
fun LongClickButton() {
val context = LocalContext.current
Column(
modifier = Modifier
.fillMaxSize(),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
) {
Button(
onClick = {
Toast.makeText(context, "Normal Click", Toast.LENGTH_SHORT).show()
},
modifier = Modifier
.combinedClickable(
onClick = {
Toast.makeText(context, "Combined Click", Toast.LENGTH_SHORT).show()
},
onLongClick = {
Toast.makeText(context, "Combined Long Click", Toast.LENGTH_SHORT).show()
},
)
) {
Text("Button")
}
IconButton(
onClick = {
Toast.makeText(context, "Normal Click", Toast.LENGTH_SHORT).show()
},
modifier = Modifier
.combinedClickable(
onClick = {
Toast.makeText(context, "Combined Click", Toast.LENGTH_SHORT).show()
},
onLongClick = {
Toast.makeText(context, "Combined Long Click", Toast.LENGTH_SHORT).show()
},
)
) {
Icon(Icons.Default.ArrowBack, null)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment