Native Mobile App Development With Java

Button Click Open Gmail app and send email android java example

19-Oct-2024

Button Click Open Gmail app and send email android java example

1/  Manifest Internet permission .



<uses-permission android:name="android.permission.INTERNET"/>



2/  XML Desing .



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"

>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Email_Send"
android:gravity="center"
android:textSize="30dp"
android:textColor="@color/black"
android:layout_above="@+id/layout"
android:layout_marginBottom="30dp"
android:textStyle="bold"

/>

<LinearLayout
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"

>

<EditText
android:id="@+id/edSubject"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="5dp"
android:hint="Enter your subject"
android:layout_marginTop="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="@color/black"

/>

<EditText
android:id="@+id/edContent"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="5dp"
android:hint="Enter your Content"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:textColor="@color/black"
android:layout_marginRight="10dp"
android:inputType=""

/>

<EditText
android:id="@+id/edEmail"
android:layout_width="match_parent"
android:layout_height="50dp"
android:padding="5dp"
android:hint="Enter your Email"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:textColor="@color/black"
android:inputType="textEmailAddress"

/>

<Button
android:id="@+id/button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:text="Send"
android:textColor="@color/white"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:textSize="25sp"


/>

</LinearLayout>

</RelativeLayout>





3/  MainActivity.


package com.tutorialb.buttonclickopengmailappandsendemail;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

Button button;
EditText edSubject;
EditText edContent;
EditText edEmail;

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


button=findViewById(R.id.button);
edSubject=findViewById(R.id.edSubject);
edContent=findViewById(R.id.edContent);
edEmail=findViewById(R.id.edEmail);

final String[] subject = new String[1];
final String[] content = new String[1];
final String[] email = new String[1];

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

subject[0] =edSubject.getText().toString();
content[0] =edContent.getText().toString();
email[0] =edEmail.getText().toString();

if (subject[0].equals("") && content[0].equals("") && email[0].equals("")){
Toast.makeText(MainActivity.this, "all fields are required", Toast.LENGTH_SHORT).show();
}else {
Mail_send(subject[0], content[0], email[0]);
Toast.makeText(MainActivity.this, "Email_send", Toast.LENGTH_SHORT).show();
}
}
});

}




public void Mail_send (String subject, String content, String email){

Intent intent=new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_EMAIL,new String[]{email});
intent.putExtra(Intent.EXTRA_SUBJECT, (CharSequence) subject);
intent.putExtra(Intent.EXTRA_TEXT, content);
intent.setType("Massage/rfc822");
startActivity(Intent.createChooser(intent,"Choose email client : "));

}
}






Comments