1. ACTIVITY_MAIN.XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/sending_mail_example"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30sp"
tools:ignore="ObsoleteLayoutParam" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gmail"
android:textColor="#ff87ff09"
android:textSize="30sp"
android:layout_above="@+id/imageButton"
android:layout_alignEnd="@+id/imageButton"
tools:ignore="ObsoleteLayoutParam" />
<ImageButton
android:id="@+id/imageButton"
android:layout_width="157dp"
android:layout_height="149dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="@string/todo"
android:src="@drawable/mail"
tools:ignore="ObsoleteLayoutParam" />
<Button
android:id="@+id/sendEmail"
android:layout_width="246dp"
android:layout_height="wrap_content"
android:text="@string/compose_email" />
</LinearLayout>
3. ACTIVITY_MAIN.JAVA FILE:
package com.example.email;
import android.annotation.SuppressLint;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button startBtn = findViewById(R.id.sendEmail);
startBtn.setOnClickListener(view -> sendEmail());
}
@SuppressLint({"LongLogTag", "IntentReset"})
protected void sendEmail() {
Log.i("Send email", "");
String[] TO = {""};
String[] CC = {""};
Intent emailIntent = new Intent(Intent.ACTION_SEND);
emailIntent.setData(Uri.parse("mailto:"));
emailIntent.setType("text/plain");
emailIntent.putExtra(Intent.EXTRA_EMAIL, TO);
emailIntent.putExtra(Intent.EXTRA_CC, CC);
emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Your subject");
emailIntent.putExtra(Intent.EXTRA_TEXT, "Email message goes here");
try {
startActivity(Intent.createChooser(emailIntent, "Send mail..."));
finish();
Log.i("Finished sending email...", "");
} catch (android.content.ActivityNotFoundException ex) {
Toast.makeText(MainActivity.this, "There is no email client installed.",
Toast.LENGTH_SHORT).show();
}
}
}
0 Comments