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 "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 "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.   
  12. import abu.contactform.R;  
  13.   
  14. import java.util.regex.Matcher;  
  15. import java.util.regex.Pattern;  
  16.   
  17. public class ContactActivity extends AppCompatActivity {  
  18.     private Activity activity;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_contact_form);  
  24.   
  25.         final EditText your_name        = (EditText) findViewById(R.id.your_name);  
  26.         final EditText your_email       = (EditText) findViewById(R.id.your_email);  
  27.         final EditText your_subject     = (EditText) findViewById(R.id.your_subject);  
  28.         final EditText your_message     = (EditText) findViewById(R.id.your_message);  
  29.   
  30.   
  31.   
  32.         Button email = (Button) findViewById(R.id.post_message);  
  33.         email.setOnClickListener(new View.OnClickListener() {  
  34.             @Override  
  35.             public void onClick(View v) {  
  36.   
  37.                 String name      = your_name.getText().toString();  
  38.                 String email     = your_email.getText().toString();  
  39.                 String subject   = your_subject.getText().toString();  
  40.                 String message   = your_message.getText().toString();  
  41.                 if (TextUtils.isEmpty(name)){  
  42.                     your_name.setError("Enter Your Name");  
  43.                     your_name.requestFocus();  
  44.                     return;  
  45.                 }  
  46.   
  47.                 Boolean onError = false;  
  48.                 if (!isValidEmail(email)) {  
  49.                     onError = true;  
  50.                     your_email.setError("Invalid Email");  
  51.                     return;  
  52.                 }  
  53.   
  54.                 if (TextUtils.isEmpty(subject)){  
  55.                     your_subject.setError("Enter Your Subject");  
  56.                     your_subject.requestFocus();  
  57.                     return;  
  58.                 }  
  59.   
  60.                 if (TextUtils.isEmpty(message)){  
  61.                     your_message.setError("Enter Your Message");  
  62.                     your_message.requestFocus();  
  63.                     return;  
  64.                 }  
  65.   
  66.                 Intent sendEmail = new Intent(android.content.Intent.ACTION_SEND);  
  67.   
  68.             /* Fill it with Data */  
  69.                 sendEmail.setType("plain/text");  
  70.                 sendEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"[email protected]"});  
  71.                 sendEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);  
  72.                 sendEmail.putExtra(android.content.Intent.EXTRA_TEXT,  
  73.                         "name:"+name+'\n'+"Email ID:"+email+'\n'+"Message:"+'\n'+message);  
  74.   
  75.             /* Send it off to the Activity-Chooser */  
  76.                 startActivity(Intent.createChooser(sendEmail, "Send mail..."));  
  77.   
  78.   
  79.             }  
  80.         });  
  81.     }  
  82.   
  83.     @Override  
  84.     public void onResume() {  
  85.         super.onResume();  
  86.         //Get a Tracker (should auto-report)  
  87.   
  88.   
  89.     }  
  90.   
  91.     @Override  
  92.     protected void onStart() {  
  93.         super.onStart();  
  94.   
  95.     }  
  96.   
  97.     @Override  
  98.     protected void onStop() {  
  99.         super.onStop();  
  100.   
  101.     }  
  102.   
  103.   
  104.     // validating email id  
  105.   
  106.     private boolean isValidEmail(String email) {  
  107.         String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"  
  108.                 + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";  
  109.   
  110.         Pattern pattern = Pattern.compile(EMAIL_PATTERN);  
  111.         Matcher matcher = pattern.matcher(email);  
  112.         return matcher.matches();  
  113.     }  
  114.   
  115.   
  116. }  

Step 6

Now, either go to menu bar and click "Make project" or press ctrl+f9 to debug the error.

ANDROID

Step 7

Then, click "Run" button or press shift+f10 to run the project. Select the "Virtual Machine" option and click OK.

ANDROID

Conclusion

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

ANDROID

ANDROID

Up Next
    Ebook Download
    View all
    Learn
    View all