-2

I am a newbie in android development. This is my code for my app. I have been trying to make the button to initialize and email app intent but it's not working. I have shared my files below. Please help.

Android Manifest

<activity android:name=".ordernow" android:label="Order Details">
    <intent-filter>
        <action android:name="android.intent.action.SENDTO" />
        <data android:scheme="mailto" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>

</activity>

activity_ordernow.xml LAYOUT

<Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textAllCaps="true"
    android:id="@+id/email_form"
    android:text="Get Quote"
    android:onClick="getQuote" />

Ordernow.java

public void getQuote (View view){

        String addresses = "omukiga@omukiga.com";
        String subject = "Get Quote Online";
        String body = "This is the body text for me";

        //Compose email to send to Intraline
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        //For only email apps to handle the information Also Experiment about using whatsapp
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_EMAIL, addresses);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, body);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }

}

Please help me find the qlitch. A good gist from github with the correct lines of code would be appreicated besides the explanations.

omukiguy
  • 1,401
  • 3
  • 17
  • 36
  • Please explain, **in detail**, what "it's not working" means. – CommonsWare Oct 15 '16 at 15:08
  • @commonsWare I am not getting the email apps to open up. The App breaks down. Error: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7f593eca4200, error-EGL_SUCCESS In fact the app just runs my activity over and over again without changing the status. – omukiguy Oct 15 '16 at 15:16

3 Answers3

0

The App breaks down.

If you mean that the app is crashing, use LogCat to examine the Java stack trace associated with the crash.

In fact the app just runs my activity over and over again without changing the status

First, get rid of the <intent-filter> from the ordernow <activity> element. That <intent-filter> would be appropriate if you were writing an email app, which does not appear to be the case.

At that point, you may find that nothing happens when you click the button. That would indicate that you have no app on your device that supports ACTION_SENDTO for a mailto: Uri. You may need to install or configure an email client to get that to work.

Community
  • 1
  • 1
CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • When I remove the the app crashes with message Unsupported action on the emulator screen. I am running android 5.1 FYI – omukiguy Oct 15 '16 at 15:30
  • Thanks removal worked like a charm. I had to just set up a mail box on the emulator – omukiguy Oct 15 '16 at 15:35
  • @omukiguy: Some mail apps will not enable activities like their `mailto:` ones until there is a valid account set up. After all, without the account, the `mailto:` activity cannot actually do anything. – CommonsWare Oct 15 '16 at 15:43
0

Final code should look like this.

activity_ordernow.xml LAYOUT

<Button
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:textAllCaps="true"
    android:id="@+id/email_form"
    android:text="Get Quote"
    android:onClick="getQuote" />

Ordernow.java

public void getQuote (View view){

        String addresses = "omukiga@omukiga.com";
        String subject = "Get Quote Online";
        String body = "This is the body text for me";

        //Compose email to send to Intraline
        Intent intent = new Intent(Intent.ACTION_SENDTO);
        //For only email apps to handle the information Also Experiment about using whatsapp
        intent.setData(Uri.parse("mailto:"));
        intent.putExtra(Intent.EXTRA_EMAIL, new String[] { "example@example.com" });
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, body);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }

}
omukiguy
  • 1,401
  • 3
  • 17
  • 36
0

Recomend you to use this library: https://github.com/cketti/EmailIntentBuilder