0

Below is the java and xml file for sending email. Please verify the code. I have registered the button still it isn't working.

ERROR: Failed to set EGL_SWAP_BEHAVIOR on surface 0xe2d19220, error=EGL_SUCCESS.Skipped 37 frames! The application may be doing too much work on its main thread

MainActivity.java

public void onClick(View view) {         
    Intent intent = null,
    intent = new Intent(Intent.ACTION_SEND);    
    intent.setData(Uri.parse("mailto:"));   
    String[] to = {"honeysonwani88@gmail.com", ""};
    intent.putExtra(Intent.EXTRA_EMAIL, to);
    intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app");
    intent.putExtra(Intent.EXTRA_TEXT, "text inside email");
    intent.setType("message/rfc822");
    chooser = Intent.createChooser(intent, "Send email");
    startActivity(chooser);
}

activity_main.xml

<Button
    android:id="@+id/btn2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Send Email"
    />
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Honey
  • 19
  • 10
  • 1
    Please explain "in detail" what "isn't working" means. Please note that [`ACTION_SENDTO` is not documented to support any of those extras](https://developer.android.com/reference/android/content/Intent.html#ACTION_SENDTO), and that the `Uri` is supposed to identify the recipient. – CommonsWare Oct 28 '17 at 14:54
  • On the click of a Button, nothing happens.What am i doing wrong here? – Honey Oct 28 '17 at 15:44
  • Perhaps you have not wired this `onClick()` method to the button. Either it should start an activity, or you should crash with an `ActivityNotFoundException`. – CommonsWare Oct 28 '17 at 15:48
  • It worked. It was incorrect button id. Thanks @CommonsWare – Honey Oct 28 '17 at 16:55

1 Answers1

1

Here is how you can wire the button from xml (btn1) and from code (btn2).

MainActivity.java

package stackoverflow.com.saturday;

import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

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

        // thru code (btn2)
        Button button = (Button)findViewById(R.id.btn2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(Intent.ACTION_SEND);
                intent.setData(Uri.parse("mailto:"));
                String[] to = {"honeysonwani88@gmail.com", ""};
                intent.putExtra(Intent.EXTRA_EMAIL, to);
                intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app");
                intent.putExtra(Intent.EXTRA_TEXT, "text inside email");
                intent.setType("message/rfc822");
                Intent chooser = Intent.createChooser(intent, "Send email");
                startActivity(chooser);
            }
        });
    }

    // thru xml (btn1)
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        intent.setData(Uri.parse("mailto:"));
        String[] to = {"honeysonwani88@gmail.com", ""};
        intent.putExtra(Intent.EXTRA_EMAIL, to);
        intent.putExtra(Intent.EXTRA_SUBJECT, "subject to your app");
        intent.putExtra(Intent.EXTRA_TEXT, "text inside email");
        intent.setType("message/rfc822");
        Intent chooser = Intent.createChooser(intent, "Send email");
        startActivity(chooser);
    }
}

activity_main.xml

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="stackoverflow.com.saturday.MainActivity">

    <Button
        android:id="@+id/btn1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"
        android:text="Send Email from xml"
        android:onClick="onClick"/>

    <Button
        android:id="@+id/btn2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/btn1"
        android:layout_marginTop="10dp"
        android:text="Send Email from mainactivity"/>

</RelativeLayout>
JRG
  • 4,037
  • 3
  • 23
  • 34