package com.example.textcontextmenu
//chatGPT
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.background
import androidx.compose.foundation.gestures.detectTapGestures
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ContentCopy
import androidx.compose.material.icons.filled.GTranslate
import androidx.compose.material.icons.filled.Share
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MaterialTheme {
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
TextListScreen()
}
}
}
}
}
@Composable
fun TextListScreen() {
val context = LocalContext.current
val textItems = listOf(
"Jetpack Compose is modern Android UI toolkit.",
"Kotlin is officially supported for Android development.",
"Android Studio is the best IDE for Android apps.",
"Compose makes UI development easier and faster.",
"Long press any text to show context menu.",
"You can copy, share, or translate selected text.",
"Material3 provides beautiful Android UI components."
)
var selectedText by remember { mutableStateOf("") }
var showMenu by remember { mutableStateOf(false) }
Box(
modifier = Modifier
.fillMaxSize()
.padding(16.dp)
) {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(12.dp)
) {
itemsIndexed(textItems) { _, item ->
Card(
modifier = Modifier
.fillMaxWidth()
.pointerInput(Unit) {
detectTapGestures(
onLongPress = {
selectedText = item
showMenu = true
}
)
},
elevation = CardDefaults.cardElevation(4.dp)
) {
Text(
text = item,
modifier = Modifier.padding(16.dp),
fontSize = 18.sp,
fontWeight = FontWeight.Medium
)
}
}
}
DropdownMenu(
expanded = showMenu,
onDismissRequest = { showMenu = false },
modifier = Modifier
.background(Color.White)
.align(Alignment.Center)
) {
// COPY
DropdownMenuItem(
text = { Text("Copy") },
leadingIcon = {
Icon(
imageVector = Icons.Default.ContentCopy,
contentDescription = null
)
},
onClick = {
copyText(context, selectedText)
showMenu = false
}
)
// COPY ALL
DropdownMenuItem(
text = { Text("Copy All") },
leadingIcon = {
Icon(
imageVector = Icons.Default.ContentCopy,
contentDescription = null
)
},
onClick = {
val allText = textItems.joinToString("\n\n")
copyText(context, allText)
showMenu = false
}
)
// SHARE
DropdownMenuItem(
text = { Text("Share") },
leadingIcon = {
Icon(
imageVector = Icons.Default.Share,
contentDescription = null
)
},
onClick = {
shareText(context, selectedText)
showMenu = false
}
)
// TRANSLATE
DropdownMenuItem(
text = { Text("Translate") },
leadingIcon = {
Icon(
imageVector = Icons.Default.GTranslate,
contentDescription = null
)
},
onClick = {
translateText(context, selectedText)
showMenu = false
}
)
}
}
}
fun copyText(context: Context, text: String) {
val clipboard =
context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clip = ClipData.newPlainText("Copied Text", text)
clipboard.setPrimaryClip(clip)
}
fun shareText(context: Context, text: String) {
val intent = Intent(Intent.ACTION_SEND).apply {
type = "text/plain"
putExtra(Intent.EXTRA_TEXT, text)
}
context.startActivity(
Intent.createChooser(intent, "Share Text")
)
}
fun translateText(context: Context, text: String) {
val url =
"https://translate.google.com/?sl=auto&tl=en&text=${
Uri.encode(text)
}&op=translate"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
context.startActivity(intent)
}