Write PDF using EditText:
1) We will use iText PDF Library to create pdf
2) We will input text Using EditText
3) Button to save text as PDF file
4) It will require WRITE_EXTERNAL_STORAGE permission to save pdf file ,
5) We'll handle runtime permission too
>>Watch For Java
MainActivity.kt
1) We will use iText PDF Library to create pdf
2) We will input text Using EditText
3) Button to save text as PDF file
4) It will require WRITE_EXTERNAL_STORAGE permission to save pdf file ,
5) We'll handle runtime permission too
>>Watch For Java
VIDEO
Step 1: Create a new Project or open new project
Step 2: Code
activity_main.xml<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="10dp" tools:context=".MainActivity"><!--EditText: Input the recipient--><EditTextandroid:id="@+id/recipientEt"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/bg_edittext"android:hint="Recipient email(s)"android:inputType="textEmailAddress"android:padding="10dp"android:textColor="@color/colorBlack" /><!--EditText: Input the subject of email--><EditTextandroid:id="@+id/subjectEt"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/bg_edittext"android:hint="Subject"android:layout_marginTop="2dp"android:layout_marginBottom="2dp"android:inputType="text|textCapSentences"android:padding="10dp"android:textColor="@color/colorBlack" /><!--EditText: Input the message--><EditTextandroid:id="@+id/messageEt"android:layout_width="match_parent"android:layout_height="wrap_content"android:background="@drawable/bg_edittext"android:gravity="start"android:hint="Enter message here..."android:inputType="text|textCapSentences"android:minHeight="150dp"android:padding="10dp"android:textColor="@color/colorBlack" /><!--Button: Launch existing email clients to send email--><Buttonandroid:id="@+id/sendEmailBtn"style="@style/Base.Widget.AppCompat.Button.Colored"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="end"android:text="send Email" /></LinearLayout>
MainActivity.kt
package com.blogspot.atifsoftwares.emailintent_kotlinimport android.content.Intentimport android.net.Uriimport android.support.v7.app.AppCompatActivityimport android.os.Bundleimport android.widget.Toastimport kotlinx.android.synthetic.main.activity_main.*class MainActivity : AppCompatActivity() {override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)//button click to get input and call sendEmail methodsendEmailBtn.setOnClickListener {//get input from EditTexts and save in variablesval recipient = recipientEt.text.toString().trim()val subject = subjectEt.text.toString().trim()val message = messageEt.text.toString().trim()//method call for email intent with these inputs as parameterssendEmail(recipient , subject , message)}}private fun sendEmail(recipient: String , subject: String , message: String) {/*ACTION_SEND action to launch an email client installed on your Android device.*/val mIntent = Intent(Intent.ACTION_SEND)/*To send an email you need to specify mailto: as URI using setData() methodand data type will be to text/plain using setType() method*/mIntent.data = Uri.parse("mailto:")mIntent.type = "text/plain"// put recipient email in intent/* recipient is put as array because you may wanna send email to multiple emails so enter comma( ,) separated emails , it will be stored in array*/mIntent.putExtra(Intent.EXTRA_EMAIL , arrayOf(recipient))//put the Subject in the intentmIntent.putExtra(Intent.EXTRA_SUBJECT , subject)//put the message in the intentmIntent.putExtra(Intent.EXTRA_TEXT , message)try {//start email intentstartActivity(Intent.createChooser(mIntent , "Choose Email Client..."))}catch (e: Exception){//if any thing goes wrong for example no email client application or any exception//get and show exception messageToast.makeText(this , e.message , Toast.LENGTH_LONG).show()}}}
0 Response to "Send Email Using Intent - Android Studio - Kotlin"