SlideShare une entreprise Scribd logo
1  sur  19
Télécharger pour lire hors ligne
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

Contenu connexe

Similaire à 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
 

Similaire à 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
 

Plus 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
 

Plus 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
 

Dernier

Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating SystemRashmi Bhat
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communicationpanditadesh123
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...Erbil Polytechnic University
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxVelmuruganTECE
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectErbil Polytechnic University
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Sumanth A
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfChristianCDAM
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptxmohitesoham12
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substationstephanwindworld
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxNiranjanYadav41
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptSAURABHKUMAR892774
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Coursebim.edu.pl
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 

Dernier (20)

Designing pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptxDesigning pile caps according to ACI 318-19.pptx
Designing pile caps according to ACI 318-19.pptx
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
Virtual memory management in Operating System
Virtual memory management in Operating SystemVirtual memory management in Operating System
Virtual memory management in Operating System
 
multiple access in wireless communication
multiple access in wireless communicationmultiple access in wireless communication
multiple access in wireless communication
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
"Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ..."Exploring the Essential Functions and Design Considerations of Spillways in ...
"Exploring the Essential Functions and Design Considerations of Spillways in ...
 
Internet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptxInternet of things -Arshdeep Bahga .pptx
Internet of things -Arshdeep Bahga .pptx
 
Risk Management in Engineering Construction Project
Risk Management in Engineering Construction ProjectRisk Management in Engineering Construction Project
Risk Management in Engineering Construction Project
 
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
Robotics-Asimov's Laws, Mechanical Subsystems, Robot Kinematics, Robot Dynami...
 
Ch10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdfCh10-Global Supply Chain - Cadena de Suministro.pdf
Ch10-Global Supply Chain - Cadena de Suministro.pdf
 
young call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Serviceyoung call girls in Green Park🔝 9953056974 🔝 escort Service
young call girls in Green Park🔝 9953056974 🔝 escort Service
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 
Python Programming for basic beginners.pptx
Python Programming for basic beginners.pptxPython Programming for basic beginners.pptx
Python Programming for basic beginners.pptx
 
Earthing details of Electrical Substation
Earthing details of Electrical SubstationEarthing details of Electrical Substation
Earthing details of Electrical Substation
 
Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
BSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptxBSNL Internship Training presentation.pptx
BSNL Internship Training presentation.pptx
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Arduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.pptArduino_CSE ece ppt for working and principal of arduino.ppt
Arduino_CSE ece ppt for working and principal of arduino.ppt
 
Katarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School CourseKatarzyna Lipka-Sidor - BIM School Course
Katarzyna Lipka-Sidor - BIM School Course
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 

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