Mailto from preferences xml possible?
Asked Answered
D

2

15

I am trying to build out the preferences for my application and I was hoping to do a "Contact the developer" area where when clicked, it would open an email directed to me. Is this possible to do from the xml file alone or do I need to do stuff in the main class?

I searched here a bit but did not see anything about doing it from XML so maybe thats not possible? Thought I would throw this question out there.

Thanks!

EDIT: This is how I actually got it to work for anyone in the future looking for some code:

import android.content.Intent;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity;

public class Prefs extends PreferenceActivity {

@Override

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.layout.prefs);
    Preference mailTo = (Preference) findPreference("mailTo");


    mailTo.setOnPreferenceClickListener(new OnPreferenceClickListener() 
  {
   public boolean onPreferenceClick(Preference preference) 
   {
        // Preferences

        Intent mailto = new Intent(Intent.ACTION_SEND); 
        mailto.setType("message/rfc822") ; // use from live device
        mailto.putExtra(Intent.EXTRA_EMAIL, new String[]{"[email protected]"});
        mailto.putExtra(Intent.EXTRA_SUBJECT,"Subject Here");
        mailto.putExtra(Intent.EXTRA_TEXT,"Body Here");
        startActivity(Intent.createChooser(mailto, "Select email application."));
    return true;
   }
  });

}

}

Dispute answered 12/6, 2011 at 0:25 Comment(1)
I tried this SEND action multiple times now, but a lot of apps handle */* mime and hence a chooser shows up with totally irrelevant apps (aside the correct GMail and Mail): Skype, Google Drive, Total Commander all want to send emails :( Drive for example creates a file named as value of EXTRA_SUBJECT with value of EXTRA_TEXT as contents.Mellette
T
-1

It's not possible from xml. However it's not a lot of work because of the way android works. The only thing that needs to be done is sending an intent that notifies the system you want to send an email, with the details you provide. Apps that are able to do this will respond to this intent and handle the rest for you. Refer to http://snipplr.com/view/19973/send-email-from-android-using-intent/

Tribalism answered 12/6, 2011 at 0:42 Comment(0)
S
32

You can do it directly from preferences:

<Preference
        android:key="contactDevKey"
        android:title="Contact developer"
        android:summary="Tell me all about your problems">
    <intent android:action="android.intent.action.VIEW"
            android:data="@string/contact_developer_uri"/>
</Preference>

Where @string/contact_developer_uri is:

<string name="contact_developer_uri">mailto:[email protected]</string>

The limitation is no predefined subject/body which is possible using the Java method about along with extras. Adding extras to <intent>s are supported since 4.0 Ice Cream Sandwich thanks to this commit (see commit tags). It's a side effect of allowing extras for fragments. So you can provide a template as Andrew suggested in the comments:

<intent android:action="android.intent.action.VIEW"
        android:data="@string/contact_developer_uri">
    <extra android:name="android.intent.extra.TEXT"
           android:value="What can I help you with?" />
    <extra android:name="android.intent.extra.SUBJECT"
           android:value="Feedback about World Changing App" />
</intent>

Using resource references are encouraged, but not necessary for both data and values.

Sadly you can't use Intent.ACTION_SEND this way, because EXTRA_EMAIL needs to be a String[] and that's not supported as <extra android:value=.

Sogdiana answered 21/12, 2011 at 2:10 Comment(7)
Is there a way of doing this with a chooser?Hewitt
I'm not sure. You could try adding mimetype info to the intent (developer.android.com/guide/topics/ui/settings.html#Intents)Sogdiana
Actually, you can add subjects and text: <intent android:action="android.intent.action.VIEW" android:data="mailto:[email protected]"> <extra android:name="android.intent.extra.TEXT" android:value="This is a test" /> <extra android:name="android.intent.extra.SUBJECT" android:value="@string/email_subject" /> </intent>Arv
@KentonPrice it shows a chooser for me when using VIEW action and mailto: data Uri. It showed me Mail and GMail apps as it should. Probably because of later android version though (Galaxy S4 / 4.4).Mellette
how to handle if target class is not found?Bolognese
@Bolognese I guess you could override the onPreferenceClick or similar in you PreferenceActivity and catch that exception, however if you are delivering an application to a device that may not have an email client which accepts android.intent.action.VIEW you are better off not using this method or adding some logic to your PreferenceActivity to hide this preference if there is no package available to handle the action.Sogdiana
@Sogdiana i solved my problem as the way you've mentioned. I think it is not possible to handle using xml. Thanks.Bolognese
T
-1

It's not possible from xml. However it's not a lot of work because of the way android works. The only thing that needs to be done is sending an intent that notifies the system you want to send an email, with the details you provide. Apps that are able to do this will respond to this intent and handle the rest for you. Refer to http://snipplr.com/view/19973/send-email-from-android-using-intent/

Tribalism answered 12/6, 2011 at 0:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.