I want to create a TextView that contains programming language code. like the following picture
I think that I should use html for give color and also will use string functions such as indexof,substring .. firstly, will find "for" and if i find it i will replace it with
<h3 style="color:blue;margin-left:20px;">for</h3>
But I am not sure if its the best way or useful.. Do you have any idea to do it on android?
PS: I am not making a compiler, just want to show piece of code
PS2: I am not asking for webview or textview.. Just want to learn how can I do it except my html case. Doesnt matter webview or textview or editview
Try this
public static Spannable setFourDifferentFontStyle(final Context context, String original,
String firstWord, String color1, float size,
String secondWord, String color2, float size2,
String thirdWord, String color3, float size3,
String fourthWord, String color4, float size4)
{
// Create a new spannable with the two strings
Spannable spannable = new SpannableString(firstWord + secondWord + thirdWord + fourthWord);
spannable.setSpan(new RelativeSizeSpan(size), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color1)), 0, firstWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(size2), firstWord.length(), firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color2)), firstWord.length(),firstWord.length() + secondWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(size3), firstWord.length() + secondWord.length(), firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color3)), firstWord.length() + secondWord.length(),firstWord.length() + secondWord.length() + thirdWord.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new RelativeSizeSpan(size4), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new ForegroundColorSpan(Color.parseColor(color4)), firstWord.length() + secondWord.length() + thirdWord.length(), original.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}
Thank you for all attention, I've completly found a solution that I want.
I've used codemirror library, and found a sample for android.
you can download the project from code google
Related
I am trying to print a decimal number inside a textview with a special format: the part on the left of the comma in one particular size and the part on the right in a different size.
Any idea about how can I make this work?
you can make use of SpannableString
TextView tv = findViewById(R.id.textview);
String value = "10.3";
final SpannableString text = new SpannableString(value);
text.setSpan(new RelativeSizeSpan(1.5f), value.indexOf(".")+1, value.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
text.setSpan(new ForegroundColorSpan(Color.RED), value.indexOf(".")+1, value.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
tv.setText(text);
for more refer these
http://alexandroid.net/spannablestring-example/
https://developer.android.com/reference/android/text/SpannableString.html
https://takeoffandroid.com/android-spannable-text-view-to-change-color-size-style-and-adding-click-event-for-particular-word-8acd8a05ec61
Use SpannableString..
Example
String yourText = "20.00";
SpannableString ss = new SpannableString(yourText);
ss.setSpan(new RelativeSizeSpan(1.2f), youString.indexOf(".") + 1, youString.length(), 0);
yourTextview.setText(ss);
Here is an example doing this
double number = 422.343;
String completeNumber = String.valueOf(number);
int decimalPointIndex = completeNumber.indexOf('.');
SpannableString ss= new SpannableString(completeNumber);
ss.setSpan(new RelativeSizeSpan(2f), 0,decimalPointIndex, 0);
textView.setText(ss);
Convert the decimal number to String
Get index value of decimal point
Then use it and display the first part before decimal as bigger
I know how to take substring a normal string.
But how can I substring a spannable.
Consider following example.
This is my sample sentence.
now when I substring this i want to get the string "mple sente" just like this and with all this formatings.
In another words I want to substring a spannable not a string.
How can I do this?
Spannable is a CharSequence and you should be able to use CharSequence.subSequence(int start, int end) without losing any information about the spannable
Using below code you can get substring from spannable:
SpannableString ss1 = new SpannableString(s);
ss1.setSpan(new RelativeSizeSpan(1.1f), 0, 6, 0); // set size
ss1.setSpan(new ForegroundColorSpan(Color.RED), 0, 6, 0);
SpannableString ss = new SpannableString("Click here to learn more");
ss.setSpan(clickableSpan, 0, 10, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
textview.setText(ss);
Hope it helps you.
So, i'm having a message which contains text and SpannableStringBuilder which is initialized like this :
final SpannableStringBuilder builder = new SpannableStringBuilder(message.getMessage());
And by default, i'm setting font to BOLD :
builder.setSpan(new StyleSpan(Typeface.BOLD), 0, messageText.length(), 0);
But my message text will be changed, example :
Your question : {question}
Your answer : {answer}
{question} and {answer} are objects that will be replaced with text, replacing is happening like this:
final TextObject question = objects.getQuestion();
spannable = new SpannableString(question.getText());
spannable.setSpan(new StyleSpan(Typeface.NORMAL), 0, spannable.length(), 0);
builder.replace(start, end, spannable);
Text is replacing just fine, but NORMAL font type isn't applying to it, so i have all text BOLD, but i need this parts to be normal
Any help is highly appreciated
You have to add that Spanned.SPAN_INCLUSIVE_INCLUSIVE as a last parameter
spannable.setSpan(new StyleSpan(Typeface.NORMAL), 0, spannable.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE);
and if you want some part in bold and some not you have to be specific which part. The second and the third parameter of setSpan method is responsible for that
I need to have a text view displaying two texts - one of them left aligned, the other - right aligned. After reading about spans, I composed the following code:
private Spannable generateText(PageModel page) {
final DateFormat dateFormat = android.text.format.DateFormat.getDateFormat(context);
final String pageNumber = page.getNumber() + " ";
final String string = pageNumber + dateFormat.format(new Date(page.getLastModified())) ;
Spannable spannable = Spannable.Factory.getInstance().newSpannable(string);
spannable.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_NORMAL), 0, pageNumber.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannable.setSpan(new AlignmentSpan.Standard(Alignment.ALIGN_OPPOSITE), pageNumber.length() + 1, string.length() - 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable;
}
Spannable spannable = generateText(page);
textView.setText(spannable);
The result is that both are left(normal) aligned. Actually, no alignment happens at all, even if I use center align. The bounds of string parts do not cross over, why does it not work?
Do you have any idea why would this not work?
final StyleSpan bss = new StyleSpan(android.graphics.Typeface.BOLD);
final SpannableString ss = new SpannableString("How to " + text + " in " + type);
ss.setSpan(bss, 7, text.length() + 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(bss, 7 + text.length() + 4, ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss);
I want to make text and type BOLD. But only type is BOLD.
What did I miss?
According to the documentation :
setSpan(Object what, int start, int end, int flags)
Attach the specified markup object to the range start…end of the text, or move the object to that range if it was already attached elsewhere.
A StyleSpan can only be used once in a Spannable. You need create a StyleSpan for each text and type
You should create object like below.
ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 7, text.length() + 7, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
ss.setSpan(new StyleSpan(android.graphics.Typeface.BOLD), 7 + text.length() + 4, ss.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
textView.setText(ss)
A better solution is to use the static method instead of create several Objects:
CharacterStyle.wrap(CharacterStyle c)