Button that sends email to contact the developer

Asked

Viewed 252 times

3

How do I create a button that the user click sends an email to another preset email?

1 answer

4


You can create a Intent and insert into the method setOnClickListener() button. See comments. See 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 on documentation on Intent's.

  • How to link this link to my button?

  • @Paiva I edited for better understanding.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.