Skip to content

Instantly share code, notes, and snippets.

@tokudu
Created February 2, 2015 23:32
Show Gist options
  • Save tokudu/61dbb99b84ee13b63287 to your computer and use it in GitHub Desktop.
Save tokudu/61dbb99b84ee13b63287 to your computer and use it in GitHub Desktop.
int padding = 10; // in pixels
yourTextView.setShadowLayer(padding /* radius */, 0, 0, 0 /* transparent */);
yourTextView.setPadding(padding, padding, padding, padding);
// Now create the new span
String str = "Some really long string goes in here";
Spannable spannable = new SpannableString(str);
spannable.setSpan(new PaddingBackgroundColorSpan(
getResources().getColor(android.R.color.black),
padding
), 0, str.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
yourTextView.setText(spannable);
@steffandroid
Copy link

If padding is higher than 25, then add the following line before calling setShadowLayer:

yourTextView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

This prevents a crash from occurring on older versions of Android: https://issuetracker.google.com/issues/37000500

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment