Friday, February 25, 2011

Sending Emails on Android, Take 2

A while back, I wrote a post about how to send specifically send only emails on Android. Unfortunately, the solution I posted does not work 100% of the time.

My previous solution was a hack and (as hacks are wont to do) it came back to bite my in the ass. It turns out that some email programs have buggy implementations of the mailto protocol. This led us to some users not getting the body content when using the mailto trick.

The correct solution is to use the normal ACTION_SEND intent, but setting the type to "message/rfc822", so only programs that implement emails will accept the intent.

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, "foo@bar.com");
intent.putExtra(Intent.EXTRA_SUBJECT, "A Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Here's my message.");
startActivity(intent);


Not only does this work more often than my hack, but it's far more elegant, too.

One other choice I've made recently is to just use startActivity() on the original intent instead of creating a chooser first. That way, the user can opt for a default email program to use instead of being asked each time which app should handle the intent.

Wednesday, February 23, 2011

Drop shadow text

Here's a little tip for creating a decent drop shadow effect in a TextView.

The issue I ran into initially when trying the shadow effects on TextView is that I could rarely get it to look right - instead of a clean, single pixel shadow, it'd always come out fuzzy. It turns out that the secret is that shadowRadius can be a float, and that you should make that float very small. Here's an example style you can apply to TextViews:

<style name="GreyEtchedText">
<item name="android:textColor">#2F3541</item>
<item name="android:shadowColor">#88FFFFFF</item>
<item name="android:shadowRadius">.1</item>
<item name="android:shadowDx">0</item>
<item name="android:shadowDy">1</item>
</style>

Of course, you can use whatever colors you want for the foreground/shadow.