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. <LinearLayout
  9. android:layout_width="match_parent"
  10. android:layout_height="wrap_content"
  11. android:orientation="vertical">
  12. <TextView
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:paddingLeft="3dp"
  16. android:text="@string/contact_form_name"
  17. android:textAllCaps="true"
  18. android:textColor="@color/colorPrimary"
  19. android:textSize="12sp" />
  20. <EditText
  21. android:id="@+id/your_name"
  22. android:layout_width="fill_parent"
  23. android:layout_height="38dp"
  24. android:layout_marginBottom="20dp"
  25. android:inputType="text"
  26. android:singleLine="true"
  27. android:textSize="14sp" />
  28. <TextView
  29. android:layout_width="fill_parent"
  30. android:layout_height="wrap_content"
  31. android:paddingLeft="3dp"
  32. android:text="@string/contact_form_email"
  33. android:textAllCaps="true"
  34. android:textColor="@color/colorPrimary"
  35. android:textSize="12sp" />
  36. <EditText
  37. android:id="@+id/your_email"
  38. android:layout_width="fill_parent"
  39. android:layout_height="38dp"
  40. android:layout_marginBottom="20dp"
  41. android:inputType="textEmailAddress"
  42. android:singleLine="true"
  43. android:textSize="14sp" />
  44. <TextView
  45. android:layout_width="fill_parent"
  46. android:layout_height="wrap_content"
  47. android:paddingLeft="3dp"
  48. android:text="@string/contact_form_subject"
  49. android:textAllCaps="true"
  50. android:textColor="@color/colorPrimary"
  51. android:textSize="12sp" />
  52. <EditText
  53. android:id="@+id/your_subject"
  54. android:layout_width="fill_parent"
  55. android:layout_height="38dp"
  56. android:layout_marginBottom="20dp"
  57. android:inputType="text"
  58. android:singleLine="true"
  59. android:textSize="14sp" />
  60. <TextView
  61. android:layout_width="fill_parent"
  62. android:layout_height="32dp"
  63. android:paddingLeft="3dp"
  64. android:text="@string/contact_form_message"
  65. android:textAllCaps="true"
  66. android:textColor="@color/colorPrimary"
  67. android:textSize="12sp" />
  68. <EditText
  69. android:id="@+id/your_message"
  70. android:layout_width="fill_parent"
  71. android:layout_height="wrap_content"
  72. android:layout_marginBottom="20dp"
  73. android:height="180dp"
  74. android:gravity="top"
  75. android:inputType="textMultiLine"
  76. android:textSize="14sp" />
  77. <Button
  78. android:id="@+id/post_message"
  79. android:layout_width="wrap_content"
  80. android:layout_height="32dp"
  81. android:layout_gravity="center"
  82. android:background="@color/colorPrimary"
  83. android:paddingBottom="1dp"
  84. android:paddingLeft="15dp"
  85. android:paddingRight="15dp"
  86. android:paddingTop="1dp"
  87. android:text="@string/contact_form_button"
  88. android:textAllCaps="true"
  89. android:textColor="@android:color/white"
  90. android:textSize="13sp" />
  91. </LinearLayout>
  92. </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. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.text.TextUtils;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.EditText;
  10. import abu.contactform.R;
  11. import java.util.regex.Matcher;
  12. import java.util.regex.Pattern;
  13. public class ContactActivity extends AppCompatActivity {
  14. private Activity activity;
  15. @Override
  16. protected void onCreate(Bundle savedInstanceState) {
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.activity_contact_form);
  19. final EditText your_name = (EditText) findViewById(R.id.your_name);
  20. final EditText your_email = (EditText) findViewById(R.id.your_email);
  21. final EditText your_subject = (EditText) findViewById(R.id.your_subject);
  22. final EditText your_message = (EditText) findViewById(R.id.your_message);
  23. Button email = (Button) findViewById(R.id.post_message);
  24. email.setOnClickListener(new View.OnClickListener() {
  25. @Override
  26. public void onClick(View v) {
  27. String name = your_name.getText().toString();
  28. String email = your_email.getText().toString();
  29. String subject = your_subject.getText().toString();
  30. String message = your_message.getText().toString();
  31. if (TextUtils.isEmpty(name)) {
  32. your_name.setError("Enter Your Name");
  33. your_name.requestFocus();
  34. return;
  35. }
  36. Boolean onError = false;
  37. if (!isValidEmail(email)) {
  38. onError = true;
  39. your_email.setError("Invalid Email");
  40. return;
  41. }
  42. if (TextUtils.isEmpty(subject)) {
  43. your_subject.setError("Enter Your Subject");
  44. your_subject.requestFocus();
  45. return;
  46. }
  47. if (TextUtils.isEmpty(message)) {
  48. your_message.setError("Enter Your Message");
  49. your_message.requestFocus();
  50. return;
  51. }
  52. Intent sendEmail = new Intent(android.content.Intent.ACTION_SEND);
  53. /* Fill it with Data */
  54. sendEmail.setType("plain/text");
  55. sendEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[] {
  56. "[email protected]"
  57. });
  58. sendEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
  59. sendEmail.putExtra(android.content.Intent.EXTRA_TEXT,
  60. "name:" + name + '\n' + "Email ID:" + email + '\n' + "Message:" + '\n' + message);
  61. /* Send it off to the Activity-Chooser */
  62. startActivity(Intent.createChooser(sendEmail, "Send mail..."));
  63. }
  64. });
  65. }
  66. @Override
  67. public void onResume() {
  68. super.onResume();
  69. //Get a Tracker (should auto-report)
  70. }
  71. @Override
  72. protected void onStart() {
  73. super.onStart();
  74. }
  75. @Override
  76. protected void onStop() {
  77. super.onStop();
  78. }
  79. // validating email id
  80. private boolean isValidEmail(String email) {
  81. String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" +
  82. "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
  83. Pattern pattern = Pattern.compile(EMAIL_PATTERN);
  84. Matcher matcher = pattern.matcher(email);
  85. return matcher.matches();
  86. }
  87. }
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