Latest News

Send Email Using Intent - Android Studio - Java

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 Kotlin

VIDEO


Step 1: Create a new project OR Open your 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.java

package com.blogspot.atifsoftwares.emailintent_java;import android.content.Intent;import android.net.Uri;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends AppCompatActivity {//declare viewsEditText mRecipientEt , mSubjectEt , mMessageEt;Button mSendEmailBtn;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//initializing views with activity_main.xmlmRecipientEt = findViewById(R.id.recipientEt);mSubjectEt = findViewById(R.id.subjectEt);mMessageEt = findViewById(R.id.messageEt);mSendEmailBtn = findViewById(R.id.sendEmailBtn);//button click to get input and call sendEmail methodmSendEmailBtn.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {//get input from EditTexts and save in variablesString recipient = mRecipientEt.getText().toString().trim(); //trim will remove space before and after the textString subject = mSubjectEt.getText().toString().trim();String message = mMessageEt.getText().toString().trim();//method call for email intent with these inputs as parameterssendEmail(recipient , subject , message);}});}private void sendEmail(String recipient , String subject , String message) {/*ACTION_SEND action to launch an email client installed on your Android device.*/Intent mEmailIntent = new 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*/mEmailIntent.setData(Uri.parse("mailto:"));mEmailIntent.setType("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*/mEmailIntent.putExtra(Intent.EXTRA_EMAIL , new String[]{recipient});//put subject of emailmEmailIntent.putExtra(Intent.EXTRA_SUBJECT , subject);//put message of email in intentmEmailIntent.putExtra(Intent.EXTRA_TEXT , message);try {//no error , so start intentstartActivity(Intent.createChooser(mEmailIntent , "Choose an Email Client"));}catch (Exception e){//if anything goes wrong e.g no internet or no email client lie gmail is available//get and show exception messageToast.makeText(this , e.getMessage() , Toast.LENGTH_SHORT).show();}}}

Step 3: Run Project

Output
Email Intent - Android Studio - Java

0 Response to "Send Email Using Intent - Android Studio - Java"