Create Email Android Application

Introduction

 
Android is one of the most popular operating systems for mobiles. In this article, I will show you how to create an Email Android application using Android Studio.
 
Requirements
Steps to be followed
 
Follow these steps to create a native Email Android application using Android Studio. I have attached the source code too with this article.
 
Step 1
 
Open Android Studio and start a new Android Studio project.
 
ANDROID
 
Step 2
 
Choose your application name and choose where your project is stored at the location. If you wish to use C++ for coding the project, mark the "Include C++ support" checkbox, and click the "Next" button.
 
ANDROID
 
Now, select the version of Android and select the target Android devices.
 
ANDROID
 
Step 3
 
Now, add an Empty Activity and click the "Next" button.
 
ANDROID
 
Add the Activity name and click "Finish".
 
ANDROID
 
Step 4
 
Go to activity_contact_form.xml. This XML file contains the designing code for an Android app.
 
ANDROID
 
The XML code is given below.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="abu.contactform.ContactActivity">  
  8.   
  9.     <LinearLayout  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:orientation="vertical">  
  13.   
  14.         <TextView  
  15.             android:layout_width="fill_parent"  
  16.             android:layout_height="wrap_content"  
  17.             android:paddingLeft="3dp"  
  18.             android:text="@string/contact_form_name"  
  19.             android:textAllCaps="true"  
  20.             android:textColor="@color/colorPrimary"  
  21.             android:textSize="12sp" />  
  22.   
  23.         <EditText  
  24.             android:id="@+id/your_name"  
  25.             android:layout_width="fill_parent"  
  26.             android:layout_height="38dp"  
  27.             android:layout_marginBottom="20dp"  
  28.             android:inputType="text"  
  29.             android:singleLine="true"  
  30.             android:textSize="14sp" />  
  31.   
  32.         <TextView  
  33.             android:layout_width="fill_parent"  
  34.             android:layout_height="wrap_content"  
  35.             android:paddingLeft="3dp"  
  36.             android:text="@string/contact_form_email"  
  37.             android:textAllCaps="true"  
  38.             android:textColor="@color/colorPrimary"  
  39.             android:textSize="12sp" />  
  40.   
  41.         <EditText  
  42.             android:id="@+id/your_email"  
  43.             android:layout_width="fill_parent"  
  44.             android:layout_height="38dp"  
  45.             android:layout_marginBottom="20dp"  
  46.             android:inputType="textEmailAddress"  
  47.             android:singleLine="true"  
  48.             android:textSize="14sp" />  
  49.   
  50.   
  51.         <TextView  
  52.             android:layout_width="fill_parent"  
  53.             android:layout_height="wrap_content"  
  54.             android:paddingLeft="3dp"  
  55.             android:text="@string/contact_form_subject"  
  56.             android:textAllCaps="true"  
  57.             android:textColor="@color/colorPrimary"  
  58.             android:textSize="12sp" />  
  59.   
  60.         <EditText  
  61.             android:id="@+id/your_subject"  
  62.             android:layout_width="fill_parent"  
  63.             android:layout_height="38dp"  
  64.             android:layout_marginBottom="20dp"  
  65.             android:inputType="text"  
  66.             android:singleLine="true"  
  67.             android:textSize="14sp" />  
  68.   
  69.         <TextView  
  70.             android:layout_width="fill_parent"  
  71.             android:layout_height="32dp"  
  72.             android:paddingLeft="3dp"  
  73.             android:text="@string/contact_form_message"  
  74.             android:textAllCaps="true"  
  75.             android:textColor="@color/colorPrimary"  
  76.             android:textSize="12sp" />  
  77.   
  78.         <EditText  
  79.             android:id="@+id/your_message"  
  80.             android:layout_width="fill_parent"  
  81.             android:layout_height="wrap_content"  
  82.             android:layout_marginBottom="20dp"  
  83.             android:height="180dp"  
  84.             android:gravity="top"  
  85.             android:inputType="textMultiLine"  
  86.             android:textSize="14sp" />  
  87.   
  88.         <Button  
  89.             android:id="@+id/post_message"  
  90.             android:layout_width="wrap_content"  
  91.             android:layout_height="32dp"  
  92.             android:layout_gravity="center"  
  93.             android:background="@color/colorPrimary"  
  94.             android:paddingBottom="1dp"  
  95.             android:paddingLeft="15dp"  
  96.             android:paddingRight="15dp"  
  97.             android:paddingTop="1dp"  
  98.             android:text="@string/contact_form_button"  
  99.             android:textAllCaps="true"  
  100.             android:textColor="@android:color/white"  
  101.             android:textSize="13sp" />  
  102.     </LinearLayout>  
  103.   
  104. </android.support.constraint.ConstraintLayout>  
Step 5
 
Go to App ⇒ Res ⇒values⇒String.xml.
 
ANDROID
 
The string XML code
  1. <resources>  
  2.     <string name="contact_form">Contact Us</string>  
  3.     <string name="contact_form_name">Your Name</string>  
  4.     <string name="contact_form_email">Your Email</string>  
  5.     <string name="contact_form_subject">Subject</string>  
  6.     <string name="contact_form_message">Message</string>  
  7.     <string name="contact_form_button">Send Message</string>  
  8.     <string name="contact_form_post_message">Please wait! We\'re sending your message to the support department...</string>  
  9.     <string name="contact_posted">Your message has been successfully delivered.</string>  
  10.     <string name="app_name">contact form</string>  
  11. </resources>  
Step 6
 
Go to MainActivity.java. This Java program is that of Android. Add the following code.
  1. package abu.contactform;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.support.v7.app.AppCompatActivity;  
  7. import android.text.TextUtils;  
  8. import android.view.View;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import abu.contactform.R;  
  12. import java.util.regex.Matcher;  
  13. import java.util.regex.Pattern;  
  14.   
  15. public class ContactActivity extends AppCompatActivity {  
  16.  private Activity activity;  
  17.  @Override  
  18.  protected void onCreate(Bundle savedInstanceState) {  
  19.   super.onCreate(savedInstanceState);  
  20.   setContentView(R.layout.activity_contact_form);  
  21.   
  22.   final EditText your_name = (EditText) findViewById(R.id.your_name);  
  23.   final EditText your_email = (EditText) findViewById(R.id.your_email);  
  24.   final EditText your_subject = (EditText) findViewById(R.id.your_subject);  
  25.   final EditText your_message = (EditText) findViewById(R.id.your_message);  
  26.   
  27.   Button email = (Button) findViewById(R.id.post_message);  
  28.   email.setOnClickListener(new View.OnClickListener() {  
  29.    @Override  
  30.    public void onClick(View v) {  
  31.   
  32.     String name = your_name.getText().toString();  
  33.     String email = your_email.getText().toString();  
  34.     String subject = your_subject.getText().toString();  
  35.     String message = your_message.getText().toString();  
  36.     if (TextUtils.isEmpty(name)) {  
  37.      your_name.setError("Enter Your Name");  
  38.      your_name.requestFocus();  
  39.      return;  
  40.     }  
  41.   
  42.     Boolean onError = false;  
  43.     if (!isValidEmail(email)) {  
  44.      onError = true;  
  45.      your_email.setError("Invalid Email");  
  46.      return;  
  47.     }  
  48.   
  49.     if (TextUtils.isEmpty(subject)) {  
  50.      your_subject.setError("Enter Your Subject");  
  51.      your_subject.requestFocus();  
  52.      return;  
  53.     }  
  54.   
  55.     if (TextUtils.isEmpty(message)) {  
  56.      your_message.setError("Enter Your Message");  
  57.      your_message.requestFocus();  
  58.      return;  
  59.     }  
  60.   
  61.     Intent sendEmail = new Intent(android.content.Intent.ACTION_SEND);  
  62.   
  63.     /* Fill it with Data */  
  64.     sendEmail.setType("plain/text");  
  65.     sendEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {  
  66.      "dr.m.karthiik@gmail.com"  
  67.     });  
  68.     sendEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);  
  69.     sendEmail.putExtra(android.content.Intent.EXTRA_TEXT,  
  70.      "name:" + name + '\n' + "Email ID:" + email + '\n' + "Message:" + '\n' + message);  
  71.   
  72.     /* Send it off to the Activity-Chooser */  
  73.     startActivity(Intent.createChooser(sendEmail, "Send mail..."));  
  74.   
  75.    }  
  76.   });  
  77.  }  
  78.   
  79.  @Override  
  80.  public void onResume() {  
  81.   super.onResume();  
  82.   //Get a Tracker (should auto-report)    
  83.  }  
  84.   
  85.  @Override  
  86.  protected void onStart() {  
  87.   super.onStart();  
  88.  }  
  89.   
  90.  @Override  
  91.  protected void onStop() {  
  92.   super.onStop();  
  93.  }  
  94.   
  95.  // validating email id    
  96.  private boolean isValidEmail(String email) {  
  97.   String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" +  
  98.    "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";  
  99.   Pattern pattern = Pattern.compile(EMAIL_PATTERN);  
  100.   Matcher matcher = pattern.matcher(email);  
  101.   return matcher.matches();  
  102.  }  

Step 6
 
Now, either go to the menu bar and click "Make a project" or press ctrl+f9 to debug the error.
 
ANDROID
 
Step 7
 
Then, click the "Run" button or press shift+f10 to run the project. Select the "Virtual Machine" option and click OK.
 
ANDROID
 

Conclusion

 
We have successfully created an Email app for Android using Android Studio.
 
ANDROID
 
ANDROID