1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192
|
package com.example.crimeaidecompose.ui.component
import android.content.Intent
import android.net.Uri
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.compose.foundation.layout.*
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.material.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
@RequiresApi(Build.VERSION_CODES.R)
@Composable
fun OpenEmailer(navController: NavController) {
// in the below line, we are
// creating variables for URL
val senderEmail = remember {
mutableStateOf(TextFieldValue())
}
val emailSubject = remember {
mutableStateOf(TextFieldValue())
}
val emailBody = remember {
mutableStateOf(TextFieldValue())
}
// on below line we are creating
// a variable for a context
val ctx = LocalContext.current
// on below line we are creating a column
Column(
// on below line we are specifying modifier
// and setting max height and max width
// for our column
modifier = Modifier
.fillMaxSize()
.fillMaxHeight()
.fillMaxWidth()
// on below line we are
// adding padding for our column
.padding(5.dp),
// on below line we are specifying horizontal
// and vertical alignment for our column
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
// on the below line, we are
// creating a text field.
TextField(
// on below line we are specifying
// value for our text field.
value = senderEmail.value,
// on below line we are adding on value
// change for text field.
onValueChange = { senderEmail.value = it },
// on below line we are adding place holder as text
placeholder = { Text(text = "Enter sender email address") },
// on below line we are adding modifier to it
// and adding padding to it and filling max width
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// on below line we are adding text style
// specifying color and font size to it.
textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
// on below line we are
// adding single line to it.
singleLine = true,
)
// on below line adding a spacer.
Spacer(modifier = Modifier.height(10.dp))
// on the below line, we are creating a text field.
TextField(
// on below line we are specifying
// value for our text field.
value = emailSubject.value,
// on below line we are adding on value change
// for text field.
onValueChange = { emailSubject.value = it },
// on below line we are adding place holder as text
placeholder = { Text(text = "Enter email subject") },
// on below line we are adding modifier to it
// and adding padding to it and filling max width
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// on below line we are adding text style
// specifying color and font size to it.
textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
// on below line we are
// adding single line to it.
singleLine = true,
)
// on below line adding a spacer.
Spacer(modifier = Modifier.height(10.dp))
// on the below line, we are creating a text field.
TextField(
// on below line we are specifying
// value for our text field.
value = emailBody.value,
// on below line we are adding on value
// change for text field.
onValueChange = { emailBody.value = it },
// on below line we are adding place holder as text
placeholder = { Text(text = "Enter email body") },
// on below line we are adding modifier to it
// and adding padding to it and filling max width
modifier = Modifier
.padding(16.dp)
.fillMaxWidth(),
// on below line we are adding text style
// specifying color and font size to it.
textStyle = TextStyle(color = Color.Black, fontSize = 15.sp),
// on below line we are
// adding single line to it.
singleLine = true,
)
// on below line adding a spacer.
Spacer(modifier = Modifier.height(20.dp))
// on below line adding a
// button to send an email
Button(onClick = {
// on below line we are creating
// an intent to send an email
val i = Intent(Intent.ACTION_SEND)
// on below line we are passing email address,
// email subject and email body
val emailAddress = arrayOf(senderEmail.value.text)
i.putExtra(Intent.EXTRA_EMAIL,emailAddress)
i.putExtra(Intent.EXTRA_SUBJECT,emailSubject.value.text)
i.putExtra(Intent.EXTRA_TEXT,emailBody.value.text)
//the problem occurs here the mail app notify "sorry we're unable to find the attached file"
i.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.ressource://com.example.crimeaidecompose.ui/res/drawable/seringue.png"))
// on below line we are
// setting type of intent
i.type = "message/rfc822"
// on the below line we are starting our activity to open email application.
ctx.startActivity(Intent.createChooser(i,"Choose an Email client : "))
}) {
// on the below line creating a text for our button.
Text(
// on below line adding a text ,
// padding, color and font size.
text = "Send Email",
modifier = Modifier.padding(10.dp),
color = Color.White,
fontSize = 15.sp
)
}
}
} |
Partager