Contact_form

In this post, I will show you how to Create Android Contact Form in you android application in an easy way. If we create Android Contact form it will make our users reach us easily without any difficult and also increases our business in return. So if you create android contact form it will be a very good practice.

Watch Below Video for better understanding 

Procedure on how to create Android Contact Form:

Before creating a android contact form we have to design our contact form on paper. We have to know what are the fields and buttons necessary in android contact form. We use following views in android contact form like TextView, EditTextView and Button.

android contact form

 

Steps:

  • Create a new android Studio project with an empty activity and name the empty activity as activity_contact_form.xml.
  • After loading the project and below code to your activity_contact_form.xml by replacing with existing code in it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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:fitsSystemWindows="true"
    tools:context=".ContactActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="@dimen/activity_vertical_margin"
        android:layout_marginLeft="@dimen/activity_horizontal_margin"
        android:layout_marginRight="@dimen/activity_horizontal_margin"
        android:layout_marginTop="@dimen/activity_vertical_margin"
        android:background="@android:color/white"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="3dp"
                android:text="@string/contact_form_name"
                android:textAllCaps="true"
                android:textColor="@color/colorPrimary"
                android:textSize="12sp" />

            <EditText
                android:id="@+id/your_name"
                android:layout_width="fill_parent"
                android:layout_height="38dp"
                android:layout_marginBottom="20dp"
                android:inputType="text"
                android:singleLine="true"
                android:textSize="14sp" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="3dp"
                android:text="@string/contact_form_email"
                android:textAllCaps="true"
                android:textColor="@color/colorPrimary"
                android:textSize="12sp" />

            <EditText
                android:id="@+id/your_email"
                android:layout_width="fill_parent"
                android:layout_height="38dp"
                android:layout_marginBottom="20dp"
                android:inputType="textEmailAddress"
                android:singleLine="true"
                android:textSize="14sp" />


            <TextView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:paddingLeft="3dp"
                android:text="@string/contact_form_subject"
                android:textAllCaps="true"
                android:textColor="@color/colorPrimary"
                android:textSize="12sp" />

            <EditText
                android:id="@+id/your_subject"
                android:layout_width="fill_parent"
                android:layout_height="38dp"
                android:layout_marginBottom="20dp"
                android:inputType="text"
                android:singleLine="true"
                android:textSize="14sp" />

            <TextView
                android:layout_width="fill_parent"
                android:layout_height="32dp"
                android:paddingLeft="3dp"
                android:text="@string/contact_form_message"
                android:textAllCaps="true"
                android:textColor="@color/colorPrimary"
                android:textSize="12sp" />

            <EditText
                android:id="@+id/your_message"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dp"
                android:height="180dp"
                android:gravity="top"
                android:inputType="textMultiLine"
                android:textSize="14sp" />

            <Button
                android:id="@+id/post_message"
                android:layout_width="wrap_content"
                android:layout_height="32dp"
                android:layout_gravity="center"
                android:background="@color/colorPrimary"
                android:paddingBottom="1dp"
                android:paddingLeft="15dp"
                android:paddingRight="15dp"
                android:paddingTop="1dp"
                android:text="@string/contact_form_button"
                android:textAllCaps="true"
                android:textColor="@android:color/white"
                android:textSize="13sp" />
        </LinearLayout>
    </ScrollView>

</android.support.design.widget.CoordinatorLayout>
  • Now add below lines to strings.xml in values folder.
<!--Contact Form-->

    <string name="contact_form">Contact Us</string>
    <string name="contact_form_name">Your Name</string>
    <string name="contact_form_email">Your Email</string>
    <string name="contact_form_subject">Subject</string>
    <string name="contact_form_message">Message</string>
    <string name="contact_form_button">Send Message</string>
    <string name="contact_form_post_message">Please wait! We\'re sending your message to the support department...</string>
    <string name="contact_posted">Your message has been successfully delivered.</string>

 

  • Now run the code and app screen should look like below screenshot.

contact_form_android_output

  • Next open ContactActivity.java and add below code by replacing everything except first line. (Watch video on top for better understanding of Java code below).
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.androidmkab.contactform.R;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ContactActivity extends AppCompatActivity {
    private Activity activity;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_form);

        final EditText your_name        = (EditText) findViewById(R.id.your_name);
        final EditText your_email       = (EditText) findViewById(R.id.your_email);
        final EditText your_subject     = (EditText) findViewById(R.id.your_subject);
        final EditText your_message     = (EditText) findViewById(R.id.your_message);



        Button email = (Button) findViewById(R.id.post_message);
        email.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

             String name      = your_name.getText().toString();
             String email     = your_email.getText().toString();
             String subject   = your_subject.getText().toString();
             String message   = your_message.getText().toString();
                if (TextUtils.isEmpty(name)){
                    your_name.setError("Enter Your Name");
                    your_name.requestFocus();
                    return;
                }

                Boolean onError = false;
                if (!isValidEmail(email)) {
                    onError = true;
                    your_email.setError("Invalid Email");
                    return;
                }

                if (TextUtils.isEmpty(subject)){
                    your_subject.setError("Enter Your Subject");
                    your_subject.requestFocus();
                    return;
                }

                if (TextUtils.isEmpty(message)){
                    your_message.setError("Enter Your Message");
                    your_message.requestFocus();
                    return;
                }

                Intent sendEmail = new Intent(android.content.Intent.ACTION_SEND);

            /* Fill it with Data */
                sendEmail.setType("plain/text");
                sendEmail.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{"dr.m.karthiik@gmail.com"});
                sendEmail.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
                sendEmail.putExtra(android.content.Intent.EXTRA_TEXT,
                        "name:"+name+'\n'+"Email ID:"+email+'\n'+"Message:"+'\n'+message);

            /* Send it off to the Activity-Chooser */
                startActivity(Intent.createChooser(sendEmail, "Send mail..."));


            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        //Get a Tracker (should auto-report)


    }

    @Override
    protected void onStart() {
        super.onStart();

    }

    @Override
    protected void onStop() {
        super.onStop();

    }


    // validating email id

    private boolean isValidEmail(String email) {
        String EMAIL_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@"
                + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";

        Pattern pattern = Pattern.compile(EMAIL_PATTERN);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }


}

 

Now if you run the code you will get fully working Android Contact Form in your android application.

 

If you like this post do share it and support me.