Monday 2 January 2017

How to create Android Contact Form for Beginners




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.

 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 you activity_contact_form.xml by replacing with existing code in it.






1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?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.

1
2
3
4
5
6
7
8
9
10
<!--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.



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).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
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.






    Choose :
  • OR
  • To comment
4 comments:
Write comments
  1. Hello,

    I have tried to implement your code but I am encountering difficulties.
    I would like you to help me if you can.
    The error is as follows: https://zupimages.net/viewer.php?id=21/53/2fh0.jpg

    ReplyDelete
  2. very useful article, thanks for the explanation admin
    Visit UMA
    Visit P2MAL

    ReplyDelete
  3. I think that thanks for the valuabe information and insights you have so provided here. Best Coding Android Wallpapers

    ReplyDelete
  4. Thank you for taking the time to write such an informative post. Your blog is not only informative, but it is also very creative
    Android application Development Company

    ReplyDelete