Search code examples
androidandroid-intentpreferencesmailto

Mailto from preferences xml possible?


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;
   }
  });

}

}


Solution

  • 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/