Rounded Corners Background Span

Additional

Language
Java
Version
N/A
Created
May 18, 2016
Updated
May 16, 2017 (Retired)
Owner
Alexander Vlasov (Iojjj)
Contributors
Alexander Vlasov (Iojjj)
iljaosintsev
2
Activity
Badge
Generate
Download
Source code
APK file

Advertisement

Rounded Corners Background Span

   

Setup and usage

Fork this project. Gradle dependency is outdated.

You can create spannable string using RoundedCornersBackgroundSpan.Builder. All methods have JavaDoc so check it to understand what each method is doing.

final Spannable spanned = new RoundedCornersBackgroundSpan.Builder(this)
    .setTextPadding(float)
    .setCornersRadius(float)
    .setTextPaddingRes(dimenId)
    .setCornersRadiusRes(dimenId)
    .setSpacingSize(float)
    .setSpacingSizeRes(dimenId)
    .setTextAlignment(alignment)
    .addTextPart(string, color)
    .addTextPart(stringId, colorId)
    .addTextPart(string)
    .addTextPart(stringId)
    .build()
textView.setText(spanned);

As text parts you can pass any CharSequence, even another Spanned strings with their own spans!

Restrictions:

  • Text with mixed layout directions is not supported (i.e. latin and arabic).
  • You must always apply padding, lines spacing and shadow to your TextView. See sample app for example.
  • Shadow radius must be equal to text padding.
  • Prefer transparent colors for shadow. You can set shadow via code TextView.setShadowLayer(padding, 0, 0, Color.TRANSPARENT);.
  • You can't set transparent shadow color via XML. Use non-zero colors, for example, #00000001.