Composing an email directly from an Android application enhances user experience by integrating email functionality seamlessly. This guide covers how to implement a feature in Android to compose a Gmail message on button click using both Java and Kotlin.
Prerequisites
- Android Studio installed
- Basic understanding of Android development
- Basic knowledge of Java or Kotlin
Step-by-Step Guide
1. Setting Up the Project
Open Android Studio and create a new project with an Empty Activity. Name your project and set the language to either Java or Kotlin.
2. Designing the User Interface
Open activity_main.xml
and add a Button that will trigger the email composition.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/composeEmailButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Compose Email"
android:layout_centerInParent="true"/>
</RelativeLayout>
3. Implementing the Button Click in Java
Open MainActivity.java
and implement the button click event to compose an email.
package com.example.composeemail;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button composeEmailButton = findViewById(R.id.composeEmailButton);
composeEmailButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
composeEmail(new String[]{"recipient@example.com"}, "Subject", "Email body text");
}
});
}
public void composeEmail(String[] addresses, String subject, String body) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
intent.setData(Uri.parse("mailto:")); // only email apps should handle this
intent.putExtra(Intent.EXTRA_EMAIL, addresses);
intent.putExtra(Intent.EXTRA_SUBJECT, subject);
intent.putExtra(Intent.EXTRA_TEXT, body);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
}
4. Implementing the Button Click in Kotlin
Open MainActivity.kt
and implement the button click event to compose an email.
package com.example.composeemail
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
composeEmailButton.setOnClickListener {
composeEmail(arrayOf("recipient@example.com"), "Subject", "Email body text")
}
}
private fun composeEmail(addresses: Array<String>, subject: String, body: String) {
val intent = Intent(Intent.ACTION_SENDTO).apply {
data = Uri.parse("mailto:") // only email apps should handle this
putExtra(Intent.EXTRA_EMAIL, addresses)
putExtra(Intent.EXTRA_SUBJECT, subject)
putExtra(Intent.EXTRA_TEXT, body)
}
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
}
}
5. Testing the Application
- Run the application on an Android device or emulator.
- Click the “Compose Email” button.
- Verify that the email composition screen appears with the recipient, subject, and body pre-filled.
Conclusion
By following these steps, you can implement a feature in your Android application that allows users to compose Gmail messages on a button click. This guide provided both Java and Kotlin implementations to suit your preference.