Button that sends email to contact the developer

3

How do I create a button that the user clicks it sends an email to another pre-defined email?

    
asked by anonymous 24.03.2017 / 01:37

1 answer

4

You can create a Intent and insert within the setOnClickListener() method of the button. See comments. Here's an example:

Button btnSendEmail = (Button) findViewById(R.id.btnSendEmail);

btnSendEmail.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View view) {
         // cria um intent 
         Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
         // define o email especifico pre definido
         String[] recipients = new String[] {
             "[email protected]"
         };
         // insere o email no extra
         emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, recipients);
         // define um assunto 
         emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Aqui o assunto");
         // define o conteúdo do email
         emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "Conteúdo do email");
         // definido o tipo 
         emailIntent.setType("text/plain");
         // inicia o intent
         startActivity(Intent.createChooser(emailIntent, "Enviar email..."));
     }
 });

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <Button
        android:id="@+id/btnSendEmail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enviar email"/>

</RelativeLayout>

See more at documentation on Intent '.

    
24.03.2017 / 02:24