SlideShare uma empresa Scribd logo
1 de 19
Baixar para ler offline
Beautiful Text
Spread your Wings with Spannables
by Stacy Devino
Prepare for the Puns and GIFs - SNS
Spannables
What are they?
Why would I need to use
them?
Why not just HTML?
Types
Editable
SpannableString / Builder
SpannableFactory
Priority
Spanned
Speciality
Language with
LocaleSpan
Making Pictures
DrawableSpan
ImageSpan
IconMarginSpan
DynamicSpans
MaskFilterSpan
BackgroundColorSpan
Accessibility
TtsSpan
TTsSpanBuilder
String / General Spans
TextAppearanceSpan
TypefaceSpan
BulletSpan
StrikethroughSpan
QuoteSpan….. etc.
StyleSpan
ReplacementSpan
ClickableSpan
RelativeSizeSpan
SubscriptSpan
SuggestionSpan
SuperscriptSpan
Spanning the Details
Android Encyclopedia of Spans
bit.ly/Spannablepedia
Some Examples
You can have multiple types of objects in a SINGLE LINE✓
✓
✓
✓
✓
Renders Quickly, more so than large Html.toText() content (OMG less data!)
Allows more programmatic control and simpler XML layouts
Perfect Scaling on every device - future proof *depending on use*
Use embedded resources like Fonts and Pictures
But Why?
Simplifying Views
You can have a single View
replace appx 4-6 Views /
Objects in even simple
examples.
Only Way
Sometimes there just isn’t a
good way to do it without
making your own custom view
object.
Use existing Android
Resources easily.
Better Views
Spannable
Base class
Supports All Span Types
Allows you to have a Span
without content in its
Constructor
Ideal for when an object will
be reassigned and reused
SpannableFactory
SpannableString
SpannableString.Builder
Immutable
Basically, it is Spannable +
String with or without options
in it’s Constructor
Supports everything that a
standard Spannable does
Editable
Dynamic
Edit-able <
Use for EditText input
Ideally, good for custom
chatbots
Supports all Span Types (but
be careful on some as the
span may not be edit-able)
Spannables Types
The EZ-E of Strings
Input: Simple String Example
Output: Simple String Example
String simpleString = "Simple String Example";
String textToBold = "Simple";
SpannableString content =new SpannableString(simpleString);
//Bold Italic Text
content.setSpan(new StyleSpan(Typeface.BOLD_ITALIC),
simpleString.indexOf(textToBold),
simpleString.indexOf(textToBold) + textToBold.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Spanned
myTextView.setText(content);
Simple Text Spannables & Spans
The Only-Way Strings
Input: Simple String Example
Continued New Output:
Simple String Example
String textToStrike = "String";
//Strikethrough - not possible using Html.toText()
content.setSpan(new StrikethroughSpan(),
simpleString.indexOf(textToStrike),
simpleString.indexOf(textToBold) + textToBold.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Spanned
//Change Font Color (since font is the foreground)
content.setSpan(new
ForegroundColorSpan(getColor(Color.PURPLE)),
simpleString.indexOf(textToStrike),
simpleString.indexOf(textToBold) + textToBold.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Spanned
Fancy Text Spans
The Only-Way Strings
Input: Simple String Example
Continued New Output:
Simple String Example
String textToClick= "Example";
ClickableSpan clickableSpan = new ClickableSpan() {
@Override
public void onClick(View textView) {
//Set Some Kind of Clickable Action
startActivity(new Intent(MyActivity.this, NextActivity.class));}
@Override
public void updateDrawState(TextPaint textPaint) {
super.updateDrawState(textPaint);
textPaint.setUnderlineText(true);
textPaint.setColor(getColor(Color.BLUE);}
};
Clicky Action Spans
Woah Canvas
ReplacementSpan has both
Canvas and Characters which is
good for Combining Objects on
top of each other without
Overdraw
public class RedRoundedBackgroundSpan extends ReplacementSpan {
private int CORNER_RADIUS = 5;
private int backgroundColor = 0;
private int textColor = 0;
public RedRoundedBackgroundSpan(Context context) {super();
backgroundColor = context.getColor(Color.RED;
textColor = context.getColor(Color.WHITE);}
@Override
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom,
Paint paint) {
RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom);
paint.setColor(backgroundColor);
canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint);
paint.setColor(textColor);
canvas.drawText(text, start, end, x, y, paint);}
@Override
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
return Math.round(paint.measureText(text, start, end)); }
private float measureText(Paint paint, CharSequence text, int start, int end) {
return paint.measureText(text, start, end);} } //End of class
Custom Replacement Spans
Canvas skillz always useful
Usual Way, not very malleable and can’t do fancy Locale / Language :
SpannableString content =
new SpannableString(String.format(Locale.US, "%.2f",
"Forced Locale String"));
Using LocaleSpan:
LocaleSpan localSpan = new LocaleSpan(new Locale("en", "US"));
Speciality Spans
Locale Pictures Accessibility
Adding in a Bitmap or Drawable
Dynamic / DrawableSpan, ImageSpan, IconMarginSpan
Drawable yao = getResources().getDrawable(R.drawable.reallyface);
yao.setBounds(0, 0, yao.getIntrinsicWidth(), yao.getIntrinsicHeight());
ImageSpan span = new ImageSpan(yao, ImageSpan.ALIGN_BASELINE);
YES!
Speciality Spans
Locale Pictures Accessibility
Playing with Color
BackgroundSpan / ForegroundSpan / MaskFilterSpan or like here with Replacement Span
Speciality Spans
Locale Pictures Accessibility
BlurMaskFilter blurFilter = new BlurMaskFilter(2.0f,
BlurMaskFilter.Blur.NORMAL);
MaskFilterSpan blurMask = new MaskFilterSpan(blurFilter);
//Now, you can make text Blurry! Any type of Mask can be used here.
Accessibility with Text-to-Speech
TTSSpan and all of its children are part of the Text-To-Speech parsing of your text boxes.
public static TtsSpan getPhoneTtsSpan(String phoneNumberString){
final TtsSpan.TelephoneBuilder builder=new TtsSpan.TelephoneBuilder();
if (phoneNumber == null) {
builder.setNumberParts(splitAtNonNumerics(phoneNumberString)); }
else {
if (phoneNumber.hasCountryCode()) {
builder.setCountryCode(Integer.toString(phoneNumber.getCountryCode()))}
builder.setNumberParts(Long.toString(phoneNumber.getNationalNumber()));
}
return builder.build();
}
Speciality Spans
Locale Pictures Accessibility
SpanWatcher - Essentially a listener for span removal /add / change✓
✓
✓
✓
✓
Animating Spans using SpanWatcher (usually in an Editable)
The MANY types of Spanned (Inclusive, Exclusive, etc)
Playing with making many types of Canvas backgrounds
GO BACK and REMOVE ALL Html.toText() references in ALL APPS!
Bonus Points
Special Thanks
Alkami Technology (my employer)
360|Andev
Lisa Wray (@lisawrayz)
Slides: bit.ly/Spannable
About Me:
• Native Apps Lead - Alkami Technology (FinTech)
• Intel Innovator, DMS Member, Vintage game
collector/restorer, 6Sigma Black Belt, Sneakerhead
• Google Developer Group Organizer / Women
Techmakers Lead for Dallas / GDG South Mentor
WEBSITES
www.stacydevino.com
www.ledgoes.com
www.openbrite.com
EMAIL
childofthehorn@gmail.com
nerds@openbrite.com
LinkedIn
.linkedin.com/in/stacy-devi
no-40ba6815/
TWITTER
@DoesitPew

Mais conteúdo relacionado

Semelhante a Beautiful text spread your wings with Spannables

Intro to JavaFX & Widget FX
Intro to JavaFX & Widget FXIntro to JavaFX & Widget FX
Intro to JavaFX & Widget FXStephen Chin
 
Css Complete Notes
Css Complete NotesCss Complete Notes
Css Complete NotesEPAM Systems
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2ppJ. C.
 
Jetpack Compose - Hands-on February 2020
Jetpack Compose - Hands-on February 2020Jetpack Compose - Hands-on February 2020
Jetpack Compose - Hands-on February 2020Pedro Veloso
 
Ebook Pembuatan Aplikasi tiket kapal 2012
Ebook Pembuatan Aplikasi tiket kapal 2012Ebook Pembuatan Aplikasi tiket kapal 2012
Ebook Pembuatan Aplikasi tiket kapal 2012yantoit2011
 
Tag presentation 1
Tag presentation 1Tag presentation 1
Tag presentation 1abdul1221
 
&lt;p>tag presentation
&lt;p>tag presentation&lt;p>tag presentation
&lt;p>tag presentationabdul1221
 
Ingo Muschenetz: Titanium Studio Deep Dive
Ingo Muschenetz: Titanium Studio Deep DiveIngo Muschenetz: Titanium Studio Deep Dive
Ingo Muschenetz: Titanium Studio Deep DiveAxway Appcelerator
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxfredharris32
 

Semelhante a Beautiful text spread your wings with Spannables (14)

Intro to JavaFX & Widget FX
Intro to JavaFX & Widget FXIntro to JavaFX & Widget FX
Intro to JavaFX & Widget FX
 
Css Complete Notes
Css Complete NotesCss Complete Notes
Css Complete Notes
 
XAML and WPF - Dinko Jakovljević
XAML and WPF - Dinko JakovljevićXAML and WPF - Dinko Jakovljević
XAML and WPF - Dinko Jakovljević
 
INPUT BOX- VBA
INPUT BOX- VBAINPUT BOX- VBA
INPUT BOX- VBA
 
Chapter2pp
Chapter2ppChapter2pp
Chapter2pp
 
Jetpack Compose - Hands-on February 2020
Jetpack Compose - Hands-on February 2020Jetpack Compose - Hands-on February 2020
Jetpack Compose - Hands-on February 2020
 
Sencha touch
Sencha touchSencha touch
Sencha touch
 
Ebook Pembuatan Aplikasi tiket kapal 2012
Ebook Pembuatan Aplikasi tiket kapal 2012Ebook Pembuatan Aplikasi tiket kapal 2012
Ebook Pembuatan Aplikasi tiket kapal 2012
 
Tag presentation 1
Tag presentation 1Tag presentation 1
Tag presentation 1
 
&lt;p>tag presentation
&lt;p>tag presentation&lt;p>tag presentation
&lt;p>tag presentation
 
CSS ppt
CSS pptCSS ppt
CSS ppt
 
Ingo Muschenetz: Titanium Studio Deep Dive
Ingo Muschenetz: Titanium Studio Deep DiveIngo Muschenetz: Titanium Studio Deep Dive
Ingo Muschenetz: Titanium Studio Deep Dive
 
Css1 properties
Css1 propertiesCss1 properties
Css1 properties
 
Article link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docxArticle link httpiveybusinessjournal.compublicationmanaging-.docx
Article link httpiveybusinessjournal.compublicationmanaging-.docx
 

Mais de Stacy Devino

IoT with Firebase : IoT DevFest Phoenix 2018
IoT with Firebase : IoT DevFest Phoenix 2018IoT with Firebase : IoT DevFest Phoenix 2018
IoT with Firebase : IoT DevFest Phoenix 2018Stacy Devino
 
Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!Stacy Devino
 
Intro to Android : Making your first App!
Intro to Android : Making your first App!Intro to Android : Making your first App!
Intro to Android : Making your first App!Stacy Devino
 
RetroFit by Square - GDG Dallas 06/09/16
RetroFit by Square - GDG Dallas 06/09/16RetroFit by Square - GDG Dallas 06/09/16
RetroFit by Square - GDG Dallas 06/09/16Stacy Devino
 
Big Trouble in Little Networks, new and improved
Big Trouble in Little Networks, new and improvedBig Trouble in Little Networks, new and improved
Big Trouble in Little Networks, new and improvedStacy Devino
 
Big Trouble in Little Networks
Big Trouble in Little Networks Big Trouble in Little Networks
Big Trouble in Little Networks Stacy Devino
 
WWC 3D printing basics with stacy devino
WWC 3D printing basics with stacy devinoWWC 3D printing basics with stacy devino
WWC 3D printing basics with stacy devinoStacy Devino
 
Timings of Init : Android Ramdisks for the Practical Hacker
Timings of Init : Android Ramdisks for the Practical HackerTimings of Init : Android Ramdisks for the Practical Hacker
Timings of Init : Android Ramdisks for the Practical HackerStacy Devino
 

Mais de Stacy Devino (8)

IoT with Firebase : IoT DevFest Phoenix 2018
IoT with Firebase : IoT DevFest Phoenix 2018IoT with Firebase : IoT DevFest Phoenix 2018
IoT with Firebase : IoT DevFest Phoenix 2018
 
Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!Async task, threads, pools, and executors oh my!
Async task, threads, pools, and executors oh my!
 
Intro to Android : Making your first App!
Intro to Android : Making your first App!Intro to Android : Making your first App!
Intro to Android : Making your first App!
 
RetroFit by Square - GDG Dallas 06/09/16
RetroFit by Square - GDG Dallas 06/09/16RetroFit by Square - GDG Dallas 06/09/16
RetroFit by Square - GDG Dallas 06/09/16
 
Big Trouble in Little Networks, new and improved
Big Trouble in Little Networks, new and improvedBig Trouble in Little Networks, new and improved
Big Trouble in Little Networks, new and improved
 
Big Trouble in Little Networks
Big Trouble in Little Networks Big Trouble in Little Networks
Big Trouble in Little Networks
 
WWC 3D printing basics with stacy devino
WWC 3D printing basics with stacy devinoWWC 3D printing basics with stacy devino
WWC 3D printing basics with stacy devino
 
Timings of Init : Android Ramdisks for the Practical Hacker
Timings of Init : Android Ramdisks for the Practical HackerTimings of Init : Android Ramdisks for the Practical Hacker
Timings of Init : Android Ramdisks for the Practical Hacker
 

Último

Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Standamitlee9823
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performancesivaprakash250
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf203318pmpc
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationBhangaleSonal
 

Último (20)

FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced LoadsFEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
FEA Based Level 3 Assessment of Deformed Tanks with Fluid Induced Loads
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar  ≼🔝 Delhi door step de...
Call Now ≽ 9953056974 ≼🔝 Call Girls In New Ashok Nagar ≼🔝 Delhi door step de...
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
UNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its PerformanceUNIT - IV - Air Compressors and its Performance
UNIT - IV - Air Compressors and its Performance
 
22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf22-prompt engineering noted slide shown.pdf
22-prompt engineering noted slide shown.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
DC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equationDC MACHINE-Motoring and generation, Armature circuit equation
DC MACHINE-Motoring and generation, Armature circuit equation
 

Beautiful text spread your wings with Spannables

  • 1. Beautiful Text Spread your Wings with Spannables by Stacy Devino
  • 2. Prepare for the Puns and GIFs - SNS
  • 3. Spannables What are they? Why would I need to use them? Why not just HTML? Types Editable SpannableString / Builder SpannableFactory Priority Spanned Speciality Language with LocaleSpan Making Pictures DrawableSpan ImageSpan IconMarginSpan DynamicSpans MaskFilterSpan BackgroundColorSpan Accessibility TtsSpan TTsSpanBuilder String / General Spans TextAppearanceSpan TypefaceSpan BulletSpan StrikethroughSpan QuoteSpan….. etc. StyleSpan ReplacementSpan ClickableSpan RelativeSizeSpan SubscriptSpan SuggestionSpan SuperscriptSpan Spanning the Details
  • 4. Android Encyclopedia of Spans bit.ly/Spannablepedia
  • 6. You can have multiple types of objects in a SINGLE LINE✓ ✓ ✓ ✓ ✓ Renders Quickly, more so than large Html.toText() content (OMG less data!) Allows more programmatic control and simpler XML layouts Perfect Scaling on every device - future proof *depending on use* Use embedded resources like Fonts and Pictures But Why?
  • 7. Simplifying Views You can have a single View replace appx 4-6 Views / Objects in even simple examples. Only Way Sometimes there just isn’t a good way to do it without making your own custom view object. Use existing Android Resources easily. Better Views
  • 8. Spannable Base class Supports All Span Types Allows you to have a Span without content in its Constructor Ideal for when an object will be reassigned and reused SpannableFactory SpannableString SpannableString.Builder Immutable Basically, it is Spannable + String with or without options in it’s Constructor Supports everything that a standard Spannable does Editable Dynamic Edit-able < Use for EditText input Ideally, good for custom chatbots Supports all Span Types (but be careful on some as the span may not be edit-able) Spannables Types
  • 9. The EZ-E of Strings Input: Simple String Example Output: Simple String Example String simpleString = "Simple String Example"; String textToBold = "Simple"; SpannableString content =new SpannableString(simpleString); //Bold Italic Text content.setSpan(new StyleSpan(Typeface.BOLD_ITALIC), simpleString.indexOf(textToBold), simpleString.indexOf(textToBold) + textToBold.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Spanned myTextView.setText(content); Simple Text Spannables & Spans
  • 10. The Only-Way Strings Input: Simple String Example Continued New Output: Simple String Example String textToStrike = "String"; //Strikethrough - not possible using Html.toText() content.setSpan(new StrikethroughSpan(), simpleString.indexOf(textToStrike), simpleString.indexOf(textToBold) + textToBold.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Spanned //Change Font Color (since font is the foreground) content.setSpan(new ForegroundColorSpan(getColor(Color.PURPLE)), simpleString.indexOf(textToStrike), simpleString.indexOf(textToBold) + textToBold.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); //Spanned Fancy Text Spans
  • 11. The Only-Way Strings Input: Simple String Example Continued New Output: Simple String Example String textToClick= "Example"; ClickableSpan clickableSpan = new ClickableSpan() { @Override public void onClick(View textView) { //Set Some Kind of Clickable Action startActivity(new Intent(MyActivity.this, NextActivity.class));} @Override public void updateDrawState(TextPaint textPaint) { super.updateDrawState(textPaint); textPaint.setUnderlineText(true); textPaint.setColor(getColor(Color.BLUE);} }; Clicky Action Spans
  • 12. Woah Canvas ReplacementSpan has both Canvas and Characters which is good for Combining Objects on top of each other without Overdraw public class RedRoundedBackgroundSpan extends ReplacementSpan { private int CORNER_RADIUS = 5; private int backgroundColor = 0; private int textColor = 0; public RedRoundedBackgroundSpan(Context context) {super(); backgroundColor = context.getColor(Color.RED; textColor = context.getColor(Color.WHITE);} @Override public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) { RectF rect = new RectF(x, top, x + measureText(paint, text, start, end), bottom); paint.setColor(backgroundColor); canvas.drawRoundRect(rect, CORNER_RADIUS, CORNER_RADIUS, paint); paint.setColor(textColor); canvas.drawText(text, start, end, x, y, paint);} @Override public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) { return Math.round(paint.measureText(text, start, end)); } private float measureText(Paint paint, CharSequence text, int start, int end) { return paint.measureText(text, start, end);} } //End of class Custom Replacement Spans Canvas skillz always useful
  • 13. Usual Way, not very malleable and can’t do fancy Locale / Language : SpannableString content = new SpannableString(String.format(Locale.US, "%.2f", "Forced Locale String")); Using LocaleSpan: LocaleSpan localSpan = new LocaleSpan(new Locale("en", "US")); Speciality Spans Locale Pictures Accessibility
  • 14. Adding in a Bitmap or Drawable Dynamic / DrawableSpan, ImageSpan, IconMarginSpan Drawable yao = getResources().getDrawable(R.drawable.reallyface); yao.setBounds(0, 0, yao.getIntrinsicWidth(), yao.getIntrinsicHeight()); ImageSpan span = new ImageSpan(yao, ImageSpan.ALIGN_BASELINE); YES! Speciality Spans Locale Pictures Accessibility
  • 15. Playing with Color BackgroundSpan / ForegroundSpan / MaskFilterSpan or like here with Replacement Span Speciality Spans Locale Pictures Accessibility BlurMaskFilter blurFilter = new BlurMaskFilter(2.0f, BlurMaskFilter.Blur.NORMAL); MaskFilterSpan blurMask = new MaskFilterSpan(blurFilter); //Now, you can make text Blurry! Any type of Mask can be used here.
  • 16. Accessibility with Text-to-Speech TTSSpan and all of its children are part of the Text-To-Speech parsing of your text boxes. public static TtsSpan getPhoneTtsSpan(String phoneNumberString){ final TtsSpan.TelephoneBuilder builder=new TtsSpan.TelephoneBuilder(); if (phoneNumber == null) { builder.setNumberParts(splitAtNonNumerics(phoneNumberString)); } else { if (phoneNumber.hasCountryCode()) { builder.setCountryCode(Integer.toString(phoneNumber.getCountryCode()))} builder.setNumberParts(Long.toString(phoneNumber.getNationalNumber())); } return builder.build(); } Speciality Spans Locale Pictures Accessibility
  • 17. SpanWatcher - Essentially a listener for span removal /add / change✓ ✓ ✓ ✓ ✓ Animating Spans using SpanWatcher (usually in an Editable) The MANY types of Spanned (Inclusive, Exclusive, etc) Playing with making many types of Canvas backgrounds GO BACK and REMOVE ALL Html.toText() references in ALL APPS! Bonus Points
  • 18. Special Thanks Alkami Technology (my employer) 360|Andev Lisa Wray (@lisawrayz)
  • 19. Slides: bit.ly/Spannable About Me: • Native Apps Lead - Alkami Technology (FinTech) • Intel Innovator, DMS Member, Vintage game collector/restorer, 6Sigma Black Belt, Sneakerhead • Google Developer Group Organizer / Women Techmakers Lead for Dallas / GDG South Mentor WEBSITES www.stacydevino.com www.ledgoes.com www.openbrite.com EMAIL childofthehorn@gmail.com nerds@openbrite.com LinkedIn .linkedin.com/in/stacy-devi no-40ba6815/ TWITTER @DoesitPew