Hi all,
long time I was looking for a solution to format text in an edittext field (e.g. font size , bold, italic, font color, background color). Now I found a solution in the Android developer pages: Spannable.
"Spans" are the format informations which are stored together with the plain text in an edittext field.
I want to give a small example for people who are also interested in that problem.
If you selected a part of the text in an edittext field, you can set its type e.g. to bold by pressing the "BOLD" button.
With the designer I created an edittext called RTF and two buttons called btnBold and btnRed.
At first we need the SelectionStart and the SelectionEnd position of the selected text. Unfortunately the edittext has no method to get the SelectionEnd. We need a reflection object for RTF:
Dim refRTF as Reflector
refRTF.Target = RTF
Now we can get start and end:
Dim selStart as Int = refRTF.RunMethod("getSelectionStart")
Dim selEnd as Int = refRTF.RunMethod("getSelectionEnd")
We need the whole text with all span informations of the edittext field (at first no format spans are available because the text is not formatted).
Dim SpannableString As JavaObject ' in Java this object is called SpannableStringBuilder
SpannableString = refRTF.RunMethod("getText") ' includes the text with all spans
Now set our selected text to bold:
' We create a span object from type StyleSpan
Dim boldSpan As JavaObject
boldSpan.InitializeNewInstance("android.text.style.StyleSpan", Array As Object(Typeface.STYLE_BOLD))
SpannableString.RunMethod("setSpan", Array As Object(boldSpan, selStart , selEnd, 33)
33 is a constant SPAN_EXCLUSIVE_EXCLUSIVE
A further example to set the font color to red:
' We create a span object from type ForegroundColorSpan
Dim redSpan As JavaObject
redSpan.InitializeNewInstance("android.text.style.ForegroundColorSpan", Array As Object(Colors.Red))
SpannableString.RunMethod("setSpan", Array As Object(redSpan, selStart , selEnd, 33)
These are only 2 examples, but now we can format our edittext field like we want. There are many other spans for background color, underline, strikethrough, subscript, superscript and paragraph styles ...
I hope, this is interesting for someone
Greetings Lutz
long time I was looking for a solution to format text in an edittext field (e.g. font size , bold, italic, font color, background color). Now I found a solution in the Android developer pages: Spannable.
"Spans" are the format informations which are stored together with the plain text in an edittext field.
I want to give a small example for people who are also interested in that problem.
If you selected a part of the text in an edittext field, you can set its type e.g. to bold by pressing the "BOLD" button.
With the designer I created an edittext called RTF and two buttons called btnBold and btnRed.
At first we need the SelectionStart and the SelectionEnd position of the selected text. Unfortunately the edittext has no method to get the SelectionEnd. We need a reflection object for RTF:
Dim refRTF as Reflector
refRTF.Target = RTF
Now we can get start and end:
Dim selStart as Int = refRTF.RunMethod("getSelectionStart")
Dim selEnd as Int = refRTF.RunMethod("getSelectionEnd")
We need the whole text with all span informations of the edittext field (at first no format spans are available because the text is not formatted).
Dim SpannableString As JavaObject ' in Java this object is called SpannableStringBuilder
SpannableString = refRTF.RunMethod("getText") ' includes the text with all spans
Now set our selected text to bold:
' We create a span object from type StyleSpan
Dim boldSpan As JavaObject
boldSpan.InitializeNewInstance("android.text.style.StyleSpan", Array As Object(Typeface.STYLE_BOLD))
SpannableString.RunMethod("setSpan", Array As Object(boldSpan, selStart , selEnd, 33)
33 is a constant SPAN_EXCLUSIVE_EXCLUSIVE
A further example to set the font color to red:
' We create a span object from type ForegroundColorSpan
Dim redSpan As JavaObject
redSpan.InitializeNewInstance("android.text.style.ForegroundColorSpan", Array As Object(Colors.Red))
SpannableString.RunMethod("setSpan", Array As Object(redSpan, selStart , selEnd, 33)
These are only 2 examples, but now we can format our edittext field like we want. There are many other spans for background color, underline, strikethrough, subscript, superscript and paragraph styles ...
I hope, this is interesting for someone
Greetings Lutz