Skip to content

Instantly share code, notes, and snippets.

@FaizVisram
Last active January 4, 2022 15:20
Show Gist options
  • Save FaizVisram/9541052 to your computer and use it in GitHub Desktop.
Save FaizVisram/9541052 to your computer and use it in GitHub Desktop.
A custom Preference that opens a new email in the user's default email app.
package com.faizvisram.android.preference;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.preference.Preference;
import android.util.AttributeSet;
/**
* Created by Faiz Visram.
*
* A custom Preference that opens a new email in the user's default email app.
*
* @param to Comma-separated list of email addresses for 'to' field in the email.
* @param cc Comma-separated list of email addresses for 'cc' field in the email.
* @param bcc Comma-separated list of email addresses for 'bcc' field in the email.
* @param subject Subject line of the email.
* @param body Body of the email.
*
*/
public class EmailPreference extends Preference implements Preference.OnPreferenceClickListener {
private String mToAddress;
private String mCcAddress;
private String mBccAddress;
private String mSubject;
private String mBody;
/**
* Perform inflation from XML and apply a class-specific base style. This
* constructor of Preference allows subclasses to use their own base
* style when they are inflating. For example, a {@link CheckBoxPreference}
* constructor calls this version of the super class constructor and
* supplies {@code android.R.attr.checkBoxPreferenceStyle} for <var>defStyle</var>.
* This allows the theme's checkbox preference style to modify all of the base
* preference attributes as well as the {@link CheckBoxPreference} class's
* attributes.
*
* @param context The Context this is associated with, through which it can
* access the current theme, resources, {@link SharedPreferences},
* etc.
* @param attrs The attributes of the XML tag that is inflating the preference.
* @param defStyle The default style to apply to this preference. If 0, no style
* will be applied (beyond what is included in the theme). This
* may either be an attribute resource, whose value will be
* retrieved from the current theme, or an explicit style
* resource.
* @see #Preference(android.content.Context, android.util.AttributeSet)
*/
public EmailPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context, attrs);
}
/**
* Constructor that is called when inflating a Preference from XML. This is
* called when a Preference is being constructed from an XML file, supplying
* attributes that were specified in the XML file. This version uses a
* default style of 0, so the only attribute values applied are those in the
* Context's Theme and the given AttributeSet.
*
* @param context The Context this is associated with, through which it can
* access the current theme, resources, {@link SharedPreferences},
* etc.
* @param attrs The attributes of the XML tag that is inflating the
* preference.
* @see #Preference(android.content.Context, android.util.AttributeSet, int)
*/
public EmailPreference(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
/**
* Constructor to create a Preference.
*
* @param context The Context in which to store Preference values.
*/
public EmailPreference(Context context) {
super(context);
init(context, null);
}
private void init(Context context, AttributeSet attrs) {
setOnPreferenceClickListener(this);
if (attrs != null) {
mToAddress = attrs.getAttributeValue(null, "to");
mCcAddress = attrs.getAttributeValue(null, "cc");
mBccAddress = attrs.getAttributeValue(null, "bcc");
mSubject = attrs.getAttributeValue(null, "subject");
mBody = attrs.getAttributeValue(null, "body");
}
}
/**
* Called when a Preference has been clicked.
*
* @param preference The Preference that was clicked.
* @return True if the click was handled.
*/
@Override
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent(Intent.ACTION_SENDTO);
String uri = "mailto:";
if(mToAddress != null) {
uri += Uri.encode(mToAddress);
}
uri += "?";
if (mCcAddress != null) {
uri += "cc=" + Uri.encode(mCcAddress) + "&";
}
if (mBccAddress != null) {
uri += "bcc=" + Uri.encode(mBccAddress) + "&";
}
if (mSubject != null) {
uri += "subject=" + Uri.encode(mSubject) + "&";
}
if (mBody != null) {
uri += "body=" + Uri.encode(mBody);
}
intent.setData(Uri.parse(uri));
getContext().startActivity(Intent.createChooser(intent, "Send from..."));
return true;
}
}
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.faizvisram.android.preference.EmailPreference
android:title="Example EmailPreference"
to="example1@mail.com,example2@mail.com"
cc="cc.me@mail.com"
bcc="bcc.me@mail.com"
subject="Example Subject"
body="Example body.\n\nLook at me, I'm an email!" />
</PreferenceScreen>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment