Courses / Mobile Application Development (CDE-315) – BS CS 5th Semester
Week 11 Lab 11 – Open the SMS or Email Application with Prefilled Text
Week 11 – Messaging: SMS and Email · 120 min read · 7 MCQs to check yourself
Send an email via intent (Coding in Flow)
Objective
Build a Contact Teacher screen that opens the Messages app or the Email app with the recipient, subject and message already filled in, using implicit intents, with input validation and a live character counter. CLOs: CLO-2 (develop apps that communicate), CLO-3 (choose intents vs direct sending – no permission needed).
What you will build in CampusMate
ContactTeacherActivity:
| Field | Used for |
|---|---|
| Teacher phone | SMS recipient |
| Teacher email | Email recipient |
| Subject | Email subject |
| Message + live counter (e.g. "45 / 160") | SMS text and email body |
| Send by SMS / Send by Email | Open the right app with everything filled in |
The user still presses Send in Messages/Gmail – so the app needs no SMS permission.
Workflow
| Step | What you do | File |
|---|---|---|
| 1 | Create the activity | New → Activity |
| 2 | Design the form | activity_contact_teacher.xml |
| 3 | Connect views + live counter (TextWatcher) | ContactTeacherActivity.java |
| 4 | SMS button (ACTION_SENDTO + smsto:) | sendSms() |
| 5 | Email button (ACTION_SENDTO + mailto:) | sendEmail() |
| 6 | Run and test | Emulator / phone |
Step 1 – Create the activity
New → Activity → Empty Views Activity → ContactTeacherActivity (Java). Open it from the Lab 6 Contact screen or the Home tab.
Step 2 – Layout (activity_contact_teacher.xml)
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="24dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Contact your teacher"
android:textSize="22sp"
android:textStyle="bold" />
<EditText
android:id="@+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Teacher phone (03xxxxxxxxx)"
android:inputType="phone" />
<EditText
android:id="@+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Teacher email"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/etSubject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Subject (for email)" />
<EditText
android:id="@+id/etMessage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:hint="Your message"
android:inputType="textMultiLine"
android:minLines="3" />
<TextView
android:id="@+id/tvCounter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0 / 160" />
<Button
android:id="@+id/btnSms"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:text="Send by SMS" />
<Button
android:id="@+id/btnEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Send by Email" />
</LinearLayout>
</ScrollView>
Step 3 – Connect views and add the live counter
package com.example.campusmate;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Patterns;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
public class ContactTeacherActivity extends AppCompatActivity {
private EditText etPhone, etEmail, etSubject, etMessage;
private TextView tvCounter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contact_teacher);
etPhone = findViewById(R.id.etPhone);
etEmail = findViewById(R.id.etEmail);
etSubject = findViewById(R.id.etSubject);
etMessage = findViewById(R.id.etMessage);
tvCounter = findViewById(R.id.tvCounter);
// Live counter: runs every time the message text changes
etMessage.addTextChangedListener(new TextWatcher() {
@Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
@Override public void onTextChanged(CharSequence s, int start, int before, int count) { }
@Override
public void afterTextChanged(Editable s) {
int n = s.length();
if (n > 160) tvCounter.setText(n + " / 160 – will be sent as more than one SMS");
else tvCounter.setText(n + " / 160");
}
});
findViewById(R.id.btnSms).setOnClickListener(v -> sendSms());
findViewById(R.id.btnEmail).setOnClickListener(v -> sendEmail());
}
private void openIntent(Intent intent, String noAppMessage) {
try {
startActivity(intent);
Toast.makeText(this, "Opening app…", Toast.LENGTH_SHORT).show();
} catch (ActivityNotFoundException e) {
Toast.makeText(this, noAppMessage, Toast.LENGTH_SHORT).show();
}
}
// Steps 4 and 5 below
}
An SMS holds 160 characters; longer texts are split into several messages – the counter warns the student.
Step 4 – Open the SMS app
private void sendSms() {
String phone = etPhone.getText().toString().trim();
String message = etMessage.getText().toString().trim();
if (phone.length() != 11) { etPhone.setError("Enter an 11-digit number"); return; }
if (message.isEmpty()) { etMessage.setError("Write a message"); return; }
Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phone)); // only SMS apps
intent.putExtra("sms_body", message); // pre-filled text
openIntent(intent, "No SMS app found");
}
Step 5 – Open the email app
private void sendEmail() {
String email = etEmail.getText().toString().trim();
String subject = etSubject.getText().toString().trim();
String message = etMessage.getText().toString().trim();
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) { etEmail.setError("Invalid email"); return; }
if (message.isEmpty()) { etMessage.setError("Write a message"); return; }
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps answer
intent.putExtra(Intent.EXTRA_EMAIL, new String[]{email}); // MUST be a String array
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, message);
openIntent(intent, "No email app found");
}
| Extra | Value |
|---|---|
Intent.EXTRA_EMAIL |
String[] of To addresses |
Intent.EXTRA_CC / EXTRA_BCC |
String[] |
Intent.EXTRA_SUBJECT |
Subject |
Intent.EXTRA_TEXT |
Body |
Run and test
| Test | Expected result |
|---|---|
| Type a message | Counter updates live |
| Type 170 characters | Counter warns about more than one SMS |
| Send by SMS with valid data | Messages app opens with number and text |
| Phone empty | Error on phone |
| Send by Email | Gmail opens with To, Subject and body filled |
| Email "abc" | "Invalid email" |
| Emulator without email account | Toast "No email app found" or Gmail setup screen |
Common errors and fixes
| Problem | Fix |
|---|---|
| Gmail opens but To is empty | EXTRA_EMAIL must be new String[]{email}, not a plain String |
| Many apps (Drive, WhatsApp) appear for email | Use ACTION_SENDTO with mailto: – not ACTION_SEND |
| Crash on tap | Missing try/catch – use openIntent() |
Short and long questions
1. (Viva) Which extras did you use to prefill the email?
Model answer
Intent.EXTRA_EMAIL (array of recipient addresses), Intent.EXTRA_SUBJECT (subject) and Intent.EXTRA_TEXT (body), with data set to Uri.parse("mailto:") and action ACTION_SENDTO.
2. (Viva) What feedback did your app give the user?
Model answer
setError messages on empty or invalid fields, a live character counter warning about multipart SMS, a Toast "Opening app…" on success and a Toast "No SMS/email app found" when no app could handle the intent.
3. (Viva) Write the code of the "Send by SMS" button.
Model answer
Validate number and message, then Intent i = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phone)); i.putExtra("sms_body", msg); startActivity(i); inside try/catch for ActivityNotFoundException.
This lesson ends with a quiz of 7 MCQs. Open the lesson on PrepOasis to take it and save your progress.
Previous: Week 11 Theory – Sending SMS Messages; Getting Feedback; Sending E-mail · Next: Week 12 Theory – Displaying Maps; Consuming Web Services Using HTTP
All lessons in Mobile Application Development (CDE-315) – BS CS 5th Semester